-
Notifications
You must be signed in to change notification settings - Fork 14
/
windowEvents.js
78 lines (63 loc) · 2.13 KB
/
windowEvents.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
var domComponent = require('./domComponent')
var rendering = require('./rendering')
var VText = require('virtual-dom/vnode/vtext.js')
function WindowWidget (attributes) {
this.attributes = attributes
this.vdom = new VText('')
this.component = domComponent.create()
var self = this
this.cache = {}
Object.keys(this.attributes).forEach(function (key) {
self.cache[key] = rendering.html.refreshify(self.attributes[key])
})
}
WindowWidget.prototype.type = 'Widget'
WindowWidget.prototype.init = function () {
applyPropertyDiffs(window, {}, this.attributes, {}, this.cache)
return (this.element = document.createTextNode(''))
}
function uniq (array) {
var sortedArray = array.slice()
sortedArray.sort()
var last
for (var n = 0; n < sortedArray.length;) {
var current = sortedArray[n]
if (last === current) {
sortedArray.splice(n, 1)
} else {
n++
}
last = current
}
return sortedArray
}
function applyPropertyDiffs (element, previous, current, previousCache, currentCache) {
uniq(Object.keys(previous).concat(Object.keys(current))).forEach(function (key) {
if (/^on/.test(key)) {
var event = key.slice(2)
var prev = previous[key]
var curr = current[key]
var refreshPrev = previousCache[key]
var refreshCurr = currentCache[key]
if (prev !== undefined && curr === undefined) {
element.removeEventListener(event, refreshPrev)
} else if (prev !== undefined && curr !== undefined && prev !== curr) {
element.removeEventListener(event, refreshPrev)
element.addEventListener(event, refreshCurr)
} else if (prev === undefined && curr !== undefined) {
element.addEventListener(event, refreshCurr)
}
}
})
}
WindowWidget.prototype.update = function (previous) {
applyPropertyDiffs(window, previous.attributes, this.attributes, previous.cache, this.cache)
this.component = previous.component
return this.element
}
WindowWidget.prototype.destroy = function () {
applyPropertyDiffs(window, this.attributes, {}, this.cache, {})
}
module.exports = function (attributes) {
return new WindowWidget(attributes)
}