Repository navigation
Expand file tree
/
Copy pathview.js
More file actions
76 lines (68 loc) · 1.88 KB
/
Copy pathview.js
File metadata and controls
76 lines (68 loc) · 1.88 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
define(function(require) {
'use strict';
var Emitter = require('emitter');
var priv = new WeakMap();
var elementMap = new WeakMap();
/**
* A View is simply a wrapper around an element.
*
* @constructor
* @param {HTMLElement} element The element that will be wrapped by this view.
*/
function View(element) {
if (!(this instanceof View)) {
throw new Error('View must be called as a constructor');
}
elementMap.set(element, this);
Object.defineProperties(this, {
id: { value: element.id },
element: { value: element }
});
priv.set(this, {
visible: !element.classList.contains('hidden')
});
}
/**
* Find or create a view instance for an element.
*
* @param {HTMLElement} element The element that will be wrapped by the view.
* @param {Function} ctor The constructor method for the view, defaults to View.
*/
View.instance = function(element, ctor = View) {
if (elementMap.has(element)) {
return elementMap.get(element);
}
return new ctor(element);
};
View.prototype = Object.create(Emitter.prototype);
Object.defineProperties(View.prototype, {
/**
* View.prototype.visible - set to true or false to toggle the "hidden" class
* on the element.
*
* Also emits a 'visibilitychange' event passing either true or false to show
* the new visible state. The event happens before the class is changed to
* allow time to modify the DOM before something becomes visible.
*/
visible: {
get: function() {
return priv.get(this).visible;
},
set: function(value) {
var state = priv.get(this);
value = !!value;
if (state.visible !== value) {
state.visible = value;
this.emit('visibilitychange', value);
if (!value) {
this.element.classList.add('hidden');
} else {
this.element.classList.remove('hidden');
}
}
return value;
}
}
});
return View;
});