This repository has been archived by the owner on Apr 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
once.js
173 lines (161 loc) · 4.97 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
/**
* 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) {
// get value to watch
var watchingValue = watcherParser($scope);
// if we have a valid value, render the binding's value
if (watchingValue !== undefined) {
// if watching and binding $parsers are the same, use watching's value, else $parse the new value
return done(element, watcherParser == bindingParser ? watchingValue : bindingParser($scope));
}
// we do not have a valid value, so we register a $watch
var watcherRemover = $scope.$watch(watch, function (newValue) {
// 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));
});
function removeWatcher() {
if (watcherRemover) {
watcherRemover();
}
}
$scope.$on("$destroy", removeWatcher);
}
var once = angular.module('once', []);
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 watcherParser = $parse(watch);
var bindingParser = attrs.onceWaitFor ? $parse(attrs[definition.name]) : watcherParser;
setOneTimeBinding($scope, element, watch, watcherParser, bindingParser, definition.binding);
}
};
}]);
}
var bindingsDefinitions = [
{
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) {
if (angular.isObject(value) && !angular.isArray(value)) {
var results = [];
angular.forEach(value, function (val, index) {
if (val) results.push(index);
});
value = results;
}
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);