Repository navigation
Expand file tree
/
Copy pathview.js
More file actions
247 lines (205 loc) · 6.17 KB
/
Copy pathview.js
File metadata and controls
247 lines (205 loc) · 6.17 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
(function(window) {
var DEFAULT_ERROR_ID = 'error-default';
/**
* Very simple base class for views.
* Provides functionality for active/inactive.
*
* The first time the view is activated
* the onactive function/event will fire.
*
* The .seen property is added to each object
* with view in its prototype. .seen can be used
* to detect if the view has ever been activated.
*
* @param {String|Object} options options or a selector for element.
*/
function View(options) {
if (typeof(options) === 'undefined') {
options = {};
}
if (typeof(options) === 'string') {
this.selectors = { element: options };
} else {
var key;
if (typeof(options) === 'undefined') {
options = {};
}
for (key in options) {
if (options.hasOwnProperty(key)) {
this[key] = options[key];
}
}
}
this.hideErrors = this.hideErrors.bind(this);
}
const INVALID_CSS = /([^a-zA-Z\-\_0-9])/g;
View.ACTIVE = 'active';
View.prototype = {
seen: false,
activeClass: View.ACTIVE,
errorVisible: false,
get element() {
return this._findElement('element');
},
get status() {
return this._findElement('status');
},
get errors() {
return this._findElement('errors');
},
/**
* Creates a string id for a given model.
*
* view.idForModel('foo-', { _id: 1 }); // => foo-1
* view.idForModel('foo-', '2'); // => foo-2
*
* @param {String} prefix of string.
* @param {Object|String|Numeric} objectOrString representation of model.
*/
idForModel: function(prefix, objectOrString) {
prefix += (typeof(objectOrString) === 'object') ?
objectOrString._id :
objectOrString;
return prefix;
},
calendarId: function(input) {
if (typeof(input) !== 'string') {
input = input.calendarId;
}
input = this.cssClean(input);
return 'calendar-id-' + input;
},
/**
* Delegate pattern event listener.
*
* @param {HTMLElement} element parent element.
* @param {String} type type of dom event.
* @param {String} selector css selector element should match
* _note_ there is no magic here this
* is determined from the root of the document.
* @param {Function|Object} handler event handler.
* first argument is the raw
* event second is the element
* matching the pattern.
*/
delegate: function(element, type, selector, handler) {
if (typeof(handler) === 'object') {
var context = handler;
handler = function() {
context.handleEvent.apply(context, arguments);
};
}
element.addEventListener(type, function(e) {
var target = e.target;
while (target !== element) {
if ('mozMatchesSelector' in target &&
target.mozMatchesSelector(selector)) {
return handler(e, target);
}
target = target.parentNode;
}
});
},
/**
* Clean a string for use with css.
* Converts illegal chars to legal ones.
*/
cssClean: function(string) {
if (typeof(string) !== 'string')
return string;
//TODO: I am worried about the performance
//of using this all over the place =/
//consider sanitizing all keys to ensure
//they don't blow up when used as a selector?
return string.replace(INVALID_CSS, '-');
},
/**
* Finds a caches a element defined
* by selectors
*
* @param {String} selector name as defined in selectors.
* @param {Boolean} all true when to find all elements. (default false).
*/
_findElement: function(name, all, element) {
if (typeof(all) === 'object') {
element = all;
all = false;
}
element = element || document;
var cacheName;
var selector;
if (typeof(all) === 'undefined') {
all = false;
}
if (name in this.selectors) {
cacheName = '_' + name + 'Element';
selector = this.selectors[name];
if (!this[cacheName]) {
if (all) {
this[cacheName] = element.querySelectorAll(selector);
} else {
this[cacheName] = element.querySelector(selector);
}
}
return this[cacheName];
}
return null;
},
/**
* Displays a list of errors
*
* @param {Array} list error list
* (see Event.validaitonErrors) or Error object.
*/
showErrors: function(list) {
var _ = navigator.mozL10n.get;
var errors = '';
// We can pass Error objects or
// Array of {name: foo} objects
if (!Array.isArray(list)) {
list = [list];
}
var i = 0;
var len = list.length;
for (; i < len; i++) {
var name = list[i].l10nID || list[i].name;
errors += _('error-' + name) || _(DEFAULT_ERROR_ID);
}
// populate error and display it.
this.errors.textContent = errors;
this.errorVisible = true;
this.status.classList.add(this.activeClass);
this.status.addEventListener('animationend', this.hideErrors);
},
hideErrors: function() {
this.status.classList.remove(this.activeClass);
this.status.removeEventListener('animationend', this.hideErrors);
this.errorVisible = false;
},
onactive: function() {
if (this.errorVisible) {
this.hideErrors();
}
//seen can be set to anything other
//then false to override this behaviour
if (this.seen === false) {
this.onfirstseen();
}
// intentionally using 'in'
if ('dispatch' in this) {
this.dispatch.apply(this, arguments);
}
this.seen = true;
if (this.element) {
this.element.classList.add(this.activeClass);
}
},
oninactive: function() {
if (this.element) {
this.element.classList.remove(this.activeClass);
}
},
onfirstseen: function() {}
};
Calendar.View = View;
}(this));