|
| 1 | +(function(undefined) { |
| 2 | + window.fuhyohigai = window.fuhyohigai || {}; |
| 3 | + |
| 4 | + var Asserts = { |
| 5 | + isNotEmptyString: function(source) { |
| 6 | + if (!source || typeof source !== 'string') { |
| 7 | + throw new Error('え、それは…(困惑): source -> ' + source); |
| 8 | + } |
| 9 | + } |
| 10 | + , isFunction: function(source) { |
| 11 | + if (!source || typeof source !== 'function') { |
| 12 | + throw new Error('え、それは…(困惑): source -> ' + source); |
| 13 | + } |
| 14 | + } |
| 15 | + }; |
| 16 | + |
| 17 | + var Entry = (function() { |
| 18 | + function Entry(key, value) { |
| 19 | + this.key = key; |
| 20 | + this.value = value; |
| 21 | + } |
| 22 | + return Entry; |
| 23 | + })(); |
| 24 | + |
| 25 | + var Holder = (function() { |
| 26 | + function Holder() { |
| 27 | + this._entries = []; |
| 28 | + } |
| 29 | + |
| 30 | + Holder.prototype.put = function(key, value) { |
| 31 | + this._entries.push(new Entry(key, value)); |
| 32 | + }; |
| 33 | + |
| 34 | + Holder.prototype.contains = function(key, value) { |
| 35 | + var entriesLength = this._entries.length |
| 36 | + , i = 0 |
| 37 | + , entry = null; |
| 38 | + for (; i < entriesLength; i++) { |
| 39 | + entry = this._entries[i]; |
| 40 | + if (entry.key === key && entry.value === value) { |
| 41 | + return true; |
| 42 | + } |
| 43 | + } |
| 44 | + return false; |
| 45 | + }; |
| 46 | + |
| 47 | + Holder.prototype.containsKey = function(key) { |
| 48 | + var entriesLength = this._entries.length |
| 49 | + , i = 0 |
| 50 | + , entry = null; |
| 51 | + for (; i < entriesLength; i++) { |
| 52 | + entry = this._entries[i]; |
| 53 | + if (entry.key === key) { |
| 54 | + return true; |
| 55 | + } |
| 56 | + } |
| 57 | + return false; |
| 58 | + }; |
| 59 | + |
| 60 | + Holder.prototype.containsValue = function(value) { |
| 61 | + var entriesLength = this._entries.length |
| 62 | + , i = 0 |
| 63 | + , entry = null; |
| 64 | + for (; i < entriesLength; i++) { |
| 65 | + entry = this._entries[i]; |
| 66 | + if (entry.value === value) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + } |
| 70 | + return false; |
| 71 | + }; |
| 72 | + |
| 73 | + Holder.prototype.remove = function(key, value) { |
| 74 | + var entriesLength = this._entries.length |
| 75 | + , i = entriesLength - 1 |
| 76 | + , entry = null; |
| 77 | + for (; i >= 0; i--) { |
| 78 | + entry = this._entries[i]; |
| 79 | + if (entry.key === key && entry.value === value) { |
| 80 | + this._entries.splice(i, 1); |
| 81 | + return; |
| 82 | + } |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + Holder.prototype.findValuesByKey = function(key) { |
| 87 | + var values = [] |
| 88 | + , entriesLength = this._entries.length |
| 89 | + , i = 0 |
| 90 | + , entry = null; |
| 91 | + for (; i < entriesLength; i++) { |
| 92 | + entry = this._entries[i]; |
| 93 | + if (entry.key === key) { |
| 94 | + values.push(entry.value); |
| 95 | + } |
| 96 | + } |
| 97 | + return values; |
| 98 | + }; |
| 99 | + |
| 100 | + return Holder; |
| 101 | + })(); |
| 102 | + |
| 103 | + var Core = (function() { |
| 104 | + |
| 105 | + function Core() { |
| 106 | + this.interval = 60; |
| 107 | + this._timer = null; |
| 108 | + this._hostRegExps = []; |
| 109 | + this._listenerHolder = new Holder(); |
| 110 | + } |
| 111 | + |
| 112 | + Core.prototype._dispatch = function() { |
| 113 | + var currentHost = window.location.host |
| 114 | + , hostsMatched = [] |
| 115 | + , hostRegExpsLength = this._hostRegExps.length |
| 116 | + , i = 0 |
| 117 | + , hostRegExp = null; |
| 118 | + for (; i < hostRegExpsLength; i++) { |
| 119 | + hostRegExp = this._hostRegExps[i]; |
| 120 | + if (hostRegExp.test(currentHost)) { |
| 121 | + hostsMatched.push(hostRegExp.source); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + i = 0; |
| 126 | + var listeners = [] |
| 127 | + , hostsMatchedLength = hostsMatched.length |
| 128 | + , hostMatched = null; |
| 129 | + for (; i < hostsMatchedLength; i++) { |
| 130 | + hostsMatched = hostsMatched[i]; |
| 131 | + var lists = this._listenerHolder.findValuesByKey(hostsMatched); |
| 132 | + Array.prototype.push.apply(listeners, lists); |
| 133 | + } |
| 134 | + |
| 135 | + i = 0; |
| 136 | + var listenersLength = listeners.length |
| 137 | + , listener = null; |
| 138 | + for (; i < listenersLength; i++) { |
| 139 | + listener = listeners[i]; |
| 140 | + listener.call(); |
| 141 | + } |
| 142 | + }; |
| 143 | + |
| 144 | + Core.prototype._onInsertDomNode = function(event) { |
| 145 | + if (this._timer) { return; } |
| 146 | + this._timer = setTimeout((function() { |
| 147 | + this._dispatch(); |
| 148 | + this._timer = null; |
| 149 | + }).bind(this), this.interval); |
| 150 | + } |
| 151 | + |
| 152 | + Core.prototype.start = function() { |
| 153 | + window.document.addEventListener('DOMNodeInserted', this._onInsertDomNode.bind(this)); |
| 154 | + }; |
| 155 | + |
| 156 | + Core.prototype.stop = function() { |
| 157 | + window.document.removeEventListener('DOMNodeInserted', this._onInsertDomNode.bind(this)); |
| 158 | + if (this._timer) { |
| 159 | + clearTimeout(this._timer); |
| 160 | + this._timer = null; |
| 161 | + } |
| 162 | + }; |
| 163 | + |
| 164 | + Core.prototype.addListener = function(host, callback) { |
| 165 | + Asserts.isNotEmptyString(host); |
| 166 | + Asserts.isFunction(callback); |
| 167 | + if (this._listenerHolder.contains(host, callback)) { |
| 168 | + return; |
| 169 | + } |
| 170 | + this._listenerHolder.put(host, callback); |
| 171 | + |
| 172 | + var hostRegExpsLength = this._hostRegExps.length |
| 173 | + , i = 0 |
| 174 | + , hostRegExp = null; |
| 175 | + for (; i < hostRegExpsLength; i++) { |
| 176 | + hostRegExp = this._hostRegExps[i]; |
| 177 | + if (hostRegExp.source === host) { |
| 178 | + return; |
| 179 | + } |
| 180 | + } |
| 181 | + this._hostRegExps.push(new RegExp(host)); |
| 182 | + }; |
| 183 | + |
| 184 | + Core.prototype.contains = function(host, callback) { |
| 185 | + return this._listenerHolder.contains(host, callback); |
| 186 | + }; |
| 187 | + |
| 188 | + Core.prototype.removeListener = function(host, callback) { |
| 189 | + this._listenerHolder.remove(host, callback); |
| 190 | + if (this._listenerHolder.findValuesByKey(host).length) { |
| 191 | + return; |
| 192 | + } |
| 193 | + var hostRegExpsLength = this._hostRegExps.length |
| 194 | + , i = hostRegExpsLength - 1 |
| 195 | + , hostRegExp = null; |
| 196 | + for (; i >= 0; i--) { |
| 197 | + var hostRegExp = this._hostRegExps[i]; |
| 198 | + if (hostRegExp.source === host) { |
| 199 | + this._hostRegExps.splice(i, 1); |
| 200 | + } |
| 201 | + } |
| 202 | + }; |
| 203 | + |
| 204 | + return Core; |
| 205 | + })(); |
| 206 | + |
| 207 | + window.fuhyohigai.core = new Core(); |
| 208 | +}).call(this); |
0 commit comments