@@ 2-158 (lines=157) @@ | ||
1 | ||
2 | app.directive('message', ['$rootScope', |
|
3 | '$timeout', |
|
4 | 'Security', |
|
5 | 'MessageService', |
|
6 | 'ThemeService', |
|
7 | 'Core', function($rootScope, $timeout, Security, MessageService, ThemeService, Core) { |
|
8 | function MessageConstructor($scope, $element, $attrs) { |
|
9 | var constructor = this; |
|
10 | var $ctrl = $scope.msgCtrl; |
|
11 | var tagName = $element[0].tagName.toLowerCase(); |
|
12 | ||
13 | $scope.autoClose = false; |
|
14 | var DirectiveProperties = (function () { |
|
15 | var autoClose; |
|
16 | var type; |
|
17 | ||
18 | function findAutoClose(){ |
|
19 | var object = $attrs.autoClose; |
|
20 | if(typeof(object) != "undefined") |
|
21 | return true; |
|
22 | else |
|
23 | return false; |
|
24 | } |
|
25 | function findType(){ |
|
26 | var object = $attrs.type; |
|
27 | if(typeof(object) != "undefined") |
|
28 | return object; |
|
29 | else |
|
30 | return "alert"; |
|
31 | } |
|
32 | ||
33 | return { |
|
34 | getAutoClose: function () { |
|
35 | if (!autoClose) { |
|
36 | autoClose = findAutoClose(); |
|
37 | } |
|
38 | $scope.autoClose = autoClose; |
|
39 | return autoClose; |
|
40 | }, |
|
41 | getType: function () { |
|
42 | if (!type) { |
|
43 | type = findType(); |
|
44 | } |
|
45 | $scope.type = type; |
|
46 | return type; |
|
47 | } |
|
48 | }; |
|
49 | })(); |
|
50 | ||
51 | function TryToCallInitDirective(){ |
|
52 | if(typeof $scope.InitDirective == "function"){ |
|
53 | $scope.InitDirective($scope, $element, $attrs, $ctrl); |
|
54 | }else{ |
|
55 | $scope.DefaultInitDirective(); |
|
56 | } |
|
57 | } |
|
58 | $scope.DefaultInitDirective = function(){ |
|
59 | if(Core.GetConfig().debugLog.DirectiveFlow) |
|
60 | console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge"); |
|
61 | } |
|
62 | function InitializeMessage() { |
|
63 | DirectiveProperties.getAutoClose(); |
|
64 | DirectiveProperties.getType(); |
|
65 | ||
66 | $ctrl.ngModel = []; |
|
67 | ||
68 | $ctrl.ngModel = MessageService.getMsg(); |
|
69 | $scope.ngModel = $ctrl.ngModel; |
|
70 | } |
|
71 | $scope.Test = function(){ |
|
72 | console.dir($ctrl) |
|
73 | console.dir($ctrl.ngModel) |
|
74 | } |
|
75 | $scope.Initialize = function(){ |
|
76 | $scope.InitScope(); |
|
77 | TryToCallInitDirective(); |
|
78 | } |
|
79 | $scope.InitScope = function(){ |
|
80 | InitializeMessage(); |
|
81 | } |
|
82 | ||
83 | function InitDirective(){ |
|
84 | if(Core.GetConfig().debugLog.DirectiveFlow) |
|
85 | console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge"); |
|
86 | } |
|
87 | // $scope.Initialize(); |
|
88 | ||
89 | $scope.$watchCollection( |
|
90 | function() { return $ctrl.ngModel }, |
|
91 | function(newValue, oldValue) { |
|
92 | if(typeof(newValue) == "undefined" && typeof(oldValue) == "undefined") |
|
93 | return; |
|
94 | ||
95 | var newValueLength = newValue.length; |
|
96 | var oldValueLength = oldValue.length; |
|
97 | ||
98 | if ( newValueLength !== oldValueLength ) { |
|
99 | // if(newValueLength > oldValueLength){ |
|
100 | if($scope == "alert") |
|
101 | if($scope.autoClose) |
|
102 | $timeout(function(){ |
|
103 | MessageService.shiftMsg(); |
|
104 | }, 7000); // (milliseconds), 1s = 1000ms |
|
105 | } |
|
106 | } |
|
107 | ); |
|
108 | } |
|
109 | function templateFunction(tElement, tAttrs) { |
|
110 | console.log("Message - templateFunction()"); |
|
111 | var globalCriteria = $rootScope.globalCriteria; |
|
112 | ||
113 | var template = '' + |
|
114 | '<div class="custom-transclude"></div>'; |
|
115 | template = '<div class="" ng-if="msgCtrl.ngModel.length > 0"><div ng-repeat="dspMsg in msgCtrl.ngModel track by $index" ng-bind="dspMsg"></div></div>'; |
|
116 | return template; |
|
117 | } |
|
118 | function templateUrlFunction(tElement, tAttrs) { |
|
119 | var directiveName = tElement[0].tagName; |
|
120 | ||
121 | directiveName = directiveName.toLowerCase(); |
|
122 | var templateURL = ThemeService.GetTemplateURL(directiveName); |
|
123 | return templateURL; |
|
124 | } |
|
125 | ||
126 | return { |
|
127 | require: ['ngModel'], |
|
128 | restrict: 'E', //'EA', //Default in 1.3+ |
|
129 | transclude: true, |
|
130 | scope: true, |
|
131 | ||
132 | controller: MessageConstructor, |
|
133 | controllerAs: 'msgCtrl', |
|
134 | ||
135 | //If both bindToController and scope are defined and have object hashes, bindToController overrides scope. |
|
136 | bindToController: { |
|
137 | ngModel: '=', |
|
138 | }, |
|
139 | // template: templateFunction, |
|
140 | templateUrl: templateUrlFunction, |
|
141 | compile: function compile(tElement, tAttrs, transclude) { |
|
142 | return { |
|
143 | pre: function preLink(scope, iElement, iAttrs, controller) { |
|
144 | // console.log("Message - compile preLink()"); |
|
145 | }, |
|
146 | post: function postLink(scope, iElement, iAttrs, controller) { |
|
147 | // console.log("Message - compile postLink()"); |
|
148 | ||
149 | transclude(scope, function (clone, scope) { |
|
150 | iElement.find('.custom-transclude').append(clone); |
|
151 | }) |
|
152 | ||
153 | scope.Initialize(); |
|
154 | } |
|
155 | } |
|
156 | }, |
|
157 | }; |
|
158 | }]); |
|
159 |
@@ 2-158 (lines=157) @@ | ||
1 | ||
2 | app.directive('message', ['$rootScope', |
|
3 | '$timeout', |
|
4 | 'Security', |
|
5 | 'MessageService', |
|
6 | 'ThemeService', |
|
7 | 'Core', function($rootScope, $timeout, Security, MessageService, ThemeService, Core) { |
|
8 | function MessageConstructor($scope, $element, $attrs) { |
|
9 | var constructor = this; |
|
10 | var $ctrl = $scope.msgCtrl; |
|
11 | var tagName = $element[0].tagName.toLowerCase(); |
|
12 | ||
13 | $scope.autoClose = false; |
|
14 | var DirectiveProperties = (function () { |
|
15 | var autoClose; |
|
16 | var type; |
|
17 | ||
18 | function findAutoClose(){ |
|
19 | var object = $attrs.autoClose; |
|
20 | if(typeof(object) != "undefined") |
|
21 | return true; |
|
22 | else |
|
23 | return false; |
|
24 | } |
|
25 | function findType(){ |
|
26 | var object = $attrs.type; |
|
27 | if(typeof(object) != "undefined") |
|
28 | return object; |
|
29 | else |
|
30 | return "alert"; |
|
31 | } |
|
32 | ||
33 | return { |
|
34 | getAutoClose: function () { |
|
35 | if (!autoClose) { |
|
36 | autoClose = findAutoClose(); |
|
37 | } |
|
38 | $scope.autoClose = autoClose; |
|
39 | return autoClose; |
|
40 | }, |
|
41 | getType: function () { |
|
42 | if (!type) { |
|
43 | type = findType(); |
|
44 | } |
|
45 | $scope.type = type; |
|
46 | return type; |
|
47 | } |
|
48 | }; |
|
49 | })(); |
|
50 | ||
51 | function TryToCallInitDirective(){ |
|
52 | if(typeof $scope.InitDirective == "function"){ |
|
53 | $scope.InitDirective($scope, $element, $attrs, $ctrl); |
|
54 | }else{ |
|
55 | $scope.DefaultInitDirective(); |
|
56 | } |
|
57 | } |
|
58 | $scope.DefaultInitDirective = function(){ |
|
59 | if(Core.GetConfig().debugLog.DirectiveFlow) |
|
60 | console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge"); |
|
61 | } |
|
62 | function InitializeMessage() { |
|
63 | DirectiveProperties.getAutoClose(); |
|
64 | DirectiveProperties.getType(); |
|
65 | ||
66 | $ctrl.ngModel = []; |
|
67 | ||
68 | $ctrl.ngModel = MessageService.getMsg(); |
|
69 | $scope.ngModel = $ctrl.ngModel; |
|
70 | } |
|
71 | $scope.Test = function(){ |
|
72 | console.dir($ctrl) |
|
73 | console.dir($ctrl.ngModel) |
|
74 | } |
|
75 | $scope.Initialize = function(){ |
|
76 | $scope.InitScope(); |
|
77 | TryToCallInitDirective(); |
|
78 | } |
|
79 | $scope.InitScope = function(){ |
|
80 | InitializeMessage(); |
|
81 | } |
|
82 | ||
83 | function InitDirective(){ |
|
84 | if(Core.GetConfig().debugLog.DirectiveFlow) |
|
85 | console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge"); |
|
86 | } |
|
87 | // $scope.Initialize(); |
|
88 | ||
89 | $scope.$watchCollection( |
|
90 | function() { return $ctrl.ngModel }, |
|
91 | function(newValue, oldValue) { |
|
92 | if(typeof(newValue) == "undefined" && typeof(oldValue) == "undefined") |
|
93 | return; |
|
94 | ||
95 | var newValueLength = newValue.length; |
|
96 | var oldValueLength = oldValue.length; |
|
97 | ||
98 | if ( newValueLength !== oldValueLength ) { |
|
99 | // if(newValueLength > oldValueLength){ |
|
100 | if($scope == "alert") |
|
101 | if($scope.autoClose) |
|
102 | $timeout(function(){ |
|
103 | MessageService.shiftMsg(); |
|
104 | }, 7000); // (milliseconds), 1s = 1000ms |
|
105 | } |
|
106 | } |
|
107 | ); |
|
108 | } |
|
109 | function templateFunction(tElement, tAttrs) { |
|
110 | console.log("Message - templateFunction()"); |
|
111 | var globalCriteria = $rootScope.globalCriteria; |
|
112 | ||
113 | var template = '' + |
|
114 | '<div class="custom-transclude"></div>'; |
|
115 | template = '<div class="" ng-if="msgCtrl.ngModel.length > 0"><div ng-repeat="dspMsg in msgCtrl.ngModel track by $index" ng-bind="dspMsg"></div></div>'; |
|
116 | return template; |
|
117 | } |
|
118 | function templateUrlFunction(tElement, tAttrs) { |
|
119 | var directiveName = tElement[0].tagName; |
|
120 | ||
121 | directiveName = directiveName.toLowerCase(); |
|
122 | var templateURL = ThemeService.GetTemplateURL(directiveName); |
|
123 | return templateURL; |
|
124 | } |
|
125 | ||
126 | return { |
|
127 | require: ['ngModel'], |
|
128 | restrict: 'E', //'EA', //Default in 1.3+ |
|
129 | transclude: true, |
|
130 | scope: true, |
|
131 | ||
132 | controller: MessageConstructor, |
|
133 | controllerAs: 'msgCtrl', |
|
134 | ||
135 | //If both bindToController and scope are defined and have object hashes, bindToController overrides scope. |
|
136 | bindToController: { |
|
137 | ngModel: '=', |
|
138 | }, |
|
139 | // template: templateFunction, |
|
140 | templateUrl: templateUrlFunction, |
|
141 | compile: function compile(tElement, tAttrs, transclude) { |
|
142 | return { |
|
143 | pre: function preLink(scope, iElement, iAttrs, controller) { |
|
144 | // console.log("Message - compile preLink()"); |
|
145 | }, |
|
146 | post: function postLink(scope, iElement, iAttrs, controller) { |
|
147 | // console.log("Message - compile postLink()"); |
|
148 | ||
149 | transclude(scope, function (clone, scope) { |
|
150 | iElement.find('.custom-transclude').append(clone); |
|
151 | }) |
|
152 | ||
153 | scope.Initialize(); |
|
154 | } |
|
155 | } |
|
156 | }, |
|
157 | }; |
|
158 | }]); |
|
159 |