forked from kevmo314/canigraduate.uchicago.edu-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonce.js
160 lines (142 loc) · 3.96 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
/**
* angular-once - one time bindings for AngularJS
* @version v0.1.5 - 2013-12-11
* @link https://github.com/tadeuszwojcik/angular-once
* @author Tadeusz Wójcik <tadeuszwojcik@gmail.com>
* @license WTFPL License, https://github.com/tadeuszwojcik/angular-once/blob/master/LICENSE.txt
*/
(function (window, angular, undefined) {
'use strict';
function setOneTimeBinding($scope, element, attrs, bindingAttr, done) {
var bindingValue = $scope.$eval(attrs[bindingAttr]);
var canBeBoundImmediately = (attrs.onceWaitFor ? $scope.$eval(attrs.onceWaitFor) : bindingValue) !== undefined;
if (canBeBoundImmediately) return done(element, bindingValue);
var thingToWatch = attrs.onceWaitFor || attrs[bindingAttr];
var watcherRemover = $scope.$watch(thingToWatch, function (newValue) {
if (newValue == undefined) return;
removeWatcher();
return setOneTimeBinding($scope, element, attrs, bindingAttr, done);
});
function removeWatcher() {
if (watcherRemover) {
watcherRemover();
}
}
$scope.$on("$destroy", removeWatcher);
}
var once = angular.module('once', []);
function makeBindingDirective(definition) {
once.directive(definition.name, function () {
return function ($scope, element, attrs) {
setOneTimeBinding($scope, element, attrs, definition.name, definition.binding);
};
});
}
var bindingsDefinitions = [
{
name: 'onceText',
binding: function (element, value) {
element.text(value);
}
},
{
name: 'onceHtml',
binding: function (element, value) {
element.html(value);
}
},
{
name: 'onceSrc',
binding: function (element, value) {
element.attr('src', value);
}
},
{
name: 'onceHref',
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',
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', function () {
return function ($scope, element, attrs) {
angular.forEach(attrs, function (attr, attrName) {
if (!/^onceAttr[A-Z]/.test(attrName)) return;
var bind = 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, attrs, attrName, bind);
});
};
});
})(window, window.angular);