forked from tadeuszwojcik/angular-once
-
Notifications
You must be signed in to change notification settings - Fork 1
/
once.js
218 lines (199 loc) · 6.28 KB
/
once.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* angular-once - one time bindings for AngularJS
* @version v0.1.7
* @link https://github.com/tadeuszwojcik/angular-once
* @author Tadeusz Wójcik <[email protected]>
* @license WTFPL License, https://github.com/tadeuszwojcik/angular-once/blob/master/LICENSE.txt
*/
(function (window, angular, undefined) {
'use strict';
function setOneTimeBinding($scope, element, watch, watcherParser, bindingParser, done, useOldValue) {
// get value to watch
var watchingValue = watcherParser($scope);
// if we have a valid value, render the binding's value
if (watchingValue !== undefined && !angular.equals($scope['prev' + watch], watchingValue)) {
// if watching and binding $parsers are the same, use watching's value, else $parse the new value
done(element, watcherParser == bindingParser ? watchingValue : bindingParser($scope), $scope['prev' + watch]);
if (useOldValue) {
$scope['prev' + watch] = watchingValue;
}
return;
}
// we do not have a valid value, so we register a $watch
var watcherRemover = $scope.$watch(watch, function (newValue, oldValue) {
// wait until we have a valid value
if (newValue == undefined) return;
// remove this $watch
removeWatcher();
// if watching and binding $parsers are the same, use watching's value, else $parse the new value
return done(element, watcherParser == bindingParser ? newValue : bindingParser($scope), useOldValue ? oldValue : null);
});
function removeWatcher() {
if (watcherRemover) {
watcherRemover();
}
}
$scope.$on("$destroy", removeWatcher);
}
var once = angular.module('once', []);
var onRefresh = ['onceChange'];
function makeBindingDirective(definition) {
once.directive(definition.name, ['$parse', function ($parse) {
return {
priority: definition.priority || 0,
link: function ($scope, element, attrs) {
var watch = attrs.onceWaitFor || attrs[definition.name];
var firstBind = false;
var useOldValue = false;
var _bind = function (w, n, b) {
var watcherParser = $parse(watch);
var bindingParser = attrs.onceWaitFor ? $parse(attrs[n]) : watcherParser;
setOneTimeBinding($scope, element, watch, watcherParser, bindingParser, b, useOldValue);
firstBind = true;
};
if (onRefresh.indexOf(definition.name) !== -1) {
return;
}
if ('onceChange' in attrs) {
useOldValue = true;
$scope.$on(attrs.onceChange, function () {
if (!firstBind) {
return;
}
_bind(watch, definition.name, definition.binding);
});
}
_bind(watch, definition.name, definition.binding);
}
};
}]);
}
var bindingsDefinitions = [
{
name: 'onceChange',
binding: function (element, value) {
}
},
{
name: 'onceText',
binding: function (element, value) {
element.text(value !== null ? value : "");
}
},
{
name: 'onceHtml',
binding: function (element, value) {
element.html(value);
}
},
{
name: 'onceSrc',
priority: 99,
binding: function (element, value) {
element.attr('src', value);
}
},
{
name: 'onceHref',
priority: 99,
binding: function (element, value) {
element.attr('href', value);
}
},
{
name: 'onceTitle',
binding: function (element, value) {
element.attr('title', value);
}
},
{
name: 'onceAlt',
binding: function (element, value) {
element.attr('alt', value);
}
},
{
name: 'onceId',
binding: function (element, value) {
element.attr('id', value);
}
},
{
name: 'onceIf',
priority: 600,
binding: function (element, value) {
if (!value) {
element.remove();
}
}
},
{
name: 'onceClass',
binding: function (element, value, oValue) {
if (angular.isObject(value) && !angular.isArray(value)) {
var results = [];
angular.forEach(value, function (val, index) {
if (val) results.push(index);
});
value = results;
}
if (oValue) {
if (angular.isObject(oValue) && !angular.isArray(oValue)) {
var results = [];
angular.forEach(oValue, function (val, index) {
if (val) results.push(index);
});
oValue = results;
}
element.removeClass(angular.isArray(oValue) ? oValue.join(' ') : oValue);
}
if (value) {
element.addClass(angular.isArray(value) ? value.join(' ') : value);
}
}
},
{
name: 'onceStyle',
binding: function (element, value) {
element.css(value);
}
},
{
name: 'onceShow',
binding: function (element, value) {
if (value) {
element.css('display', '');
} else {
element.css('display', 'none');
}
}
},
{
name: 'onceHide',
binding: function (element, value) {
if (value) {
element.css('display', 'none');
} else {
element.css('display', '');
}
}
}
];
angular.forEach(bindingsDefinitions, makeBindingDirective);
once.directive('once', ['$parse', function ($parse) {
return function ($scope, element, attrs) {
angular.forEach(attrs, function (attr, attrName) {
if (!/^onceAttr[A-Z]/.test(attrName)) return;
var watch = attrs.onceWaitFor || attrs[attrName];
var watcherParser = $parse(watch);
var bindingParser = attrs.onceWaitFor ? $parse(attrs[attrName]) : watcherParser;
var binding = function (element,value) {
var dashedName = attrName.replace(/[A-Z]/g, function (match) { return '-' + match.toLowerCase(); });
var name = dashedName.substr(10);
element.attr(name, value);
}
setOneTimeBinding($scope, element, watch, watcherParser, bindingParser, binding);
});
};
}]);
})(window, window.angular);