Implemented URL query parsing for initial token /opa/?token=abcde
[src/app-framework-demo.git] / afb-client / bower_components / tether / js / utils.js
1 (function() {
2   var Evented, addClass, defer, deferred, extend, flush, getBounds, getOffsetParent, getOrigin, getScrollBarSize, getScrollParent, hasClass, node, removeClass, uniqueId, updateClasses, zeroPosCache,
3     __hasProp = {}.hasOwnProperty,
4     __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
5     __slice = [].slice;
6
7   if (this.Tether == null) {
8     this.Tether = {
9       modules: []
10     };
11   }
12
13   getScrollParent = function(el) {
14     var parent, position, scrollParent, style, _ref;
15     position = getComputedStyle(el).position;
16     if (position === 'fixed') {
17       return el;
18     }
19     scrollParent = void 0;
20     parent = el;
21     while (parent = parent.parentNode) {
22       try {
23         style = getComputedStyle(parent);
24       } catch (_error) {}
25       if (style == null) {
26         return parent;
27       }
28       if (/(auto|scroll)/.test(style['overflow'] + style['overflow-y'] + style['overflow-x'])) {
29         if (position !== 'absolute' || ((_ref = style['position']) === 'relative' || _ref === 'absolute' || _ref === 'fixed')) {
30           return parent;
31         }
32       }
33     }
34     return document.body;
35   };
36
37   uniqueId = (function() {
38     var id;
39     id = 0;
40     return function() {
41       return id++;
42     };
43   })();
44
45   zeroPosCache = {};
46
47   getOrigin = function(doc) {
48     var id, k, node, v, _ref;
49     node = doc._tetherZeroElement;
50     if (node == null) {
51       node = doc.createElement('div');
52       node.setAttribute('data-tether-id', uniqueId());
53       extend(node.style, {
54         top: 0,
55         left: 0,
56         position: 'absolute'
57       });
58       doc.body.appendChild(node);
59       doc._tetherZeroElement = node;
60     }
61     id = node.getAttribute('data-tether-id');
62     if (zeroPosCache[id] == null) {
63       zeroPosCache[id] = {};
64       _ref = node.getBoundingClientRect();
65       for (k in _ref) {
66         v = _ref[k];
67         zeroPosCache[id][k] = v;
68       }
69       defer(function() {
70         return zeroPosCache[id] = void 0;
71       });
72     }
73     return zeroPosCache[id];
74   };
75
76   node = null;
77
78   getBounds = function(el) {
79     var box, doc, docEl, k, origin, v, _ref;
80     if (el === document) {
81       doc = document;
82       el = document.documentElement;
83     } else {
84       doc = el.ownerDocument;
85     }
86     docEl = doc.documentElement;
87     box = {};
88     _ref = el.getBoundingClientRect();
89     for (k in _ref) {
90       v = _ref[k];
91       box[k] = v;
92     }
93     origin = getOrigin(doc);
94     box.top -= origin.top;
95     box.left -= origin.left;
96     if (box.width == null) {
97       box.width = document.body.scrollWidth - box.left - box.right;
98     }
99     if (box.height == null) {
100       box.height = document.body.scrollHeight - box.top - box.bottom;
101     }
102     box.top = box.top - docEl.clientTop;
103     box.left = box.left - docEl.clientLeft;
104     box.right = doc.body.clientWidth - box.width - box.left;
105     box.bottom = doc.body.clientHeight - box.height - box.top;
106     return box;
107   };
108
109   getOffsetParent = function(el) {
110     return el.offsetParent || document.documentElement;
111   };
112
113   getScrollBarSize = function() {
114     var inner, outer, width, widthContained, widthScroll;
115     inner = document.createElement('div');
116     inner.style.width = '100%';
117     inner.style.height = '200px';
118     outer = document.createElement('div');
119     extend(outer.style, {
120       position: 'absolute',
121       top: 0,
122       left: 0,
123       pointerEvents: 'none',
124       visibility: 'hidden',
125       width: '200px',
126       height: '150px',
127       overflow: 'hidden'
128     });
129     outer.appendChild(inner);
130     document.body.appendChild(outer);
131     widthContained = inner.offsetWidth;
132     outer.style.overflow = 'scroll';
133     widthScroll = inner.offsetWidth;
134     if (widthContained === widthScroll) {
135       widthScroll = outer.clientWidth;
136     }
137     document.body.removeChild(outer);
138     width = widthContained - widthScroll;
139     return {
140       width: width,
141       height: width
142     };
143   };
144
145   extend = function(out) {
146     var args, key, obj, val, _i, _len, _ref;
147     if (out == null) {
148       out = {};
149     }
150     args = [];
151     Array.prototype.push.apply(args, arguments);
152     _ref = args.slice(1);
153     for (_i = 0, _len = _ref.length; _i < _len; _i++) {
154       obj = _ref[_i];
155       if (obj) {
156         for (key in obj) {
157           if (!__hasProp.call(obj, key)) continue;
158           val = obj[key];
159           out[key] = val;
160         }
161       }
162     }
163     return out;
164   };
165
166   removeClass = function(el, name) {
167     var cls, _i, _len, _ref, _results;
168     if (el.classList != null) {
169       _ref = name.split(' ');
170       _results = [];
171       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
172         cls = _ref[_i];
173         if (cls.trim()) {
174           _results.push(el.classList.remove(cls));
175         }
176       }
177       return _results;
178     } else {
179       return el.className = el.className.replace(new RegExp("(^| )" + (name.split(' ').join('|')) + "( |$)", 'gi'), ' ');
180     }
181   };
182
183   addClass = function(el, name) {
184     var cls, _i, _len, _ref, _results;
185     if (el.classList != null) {
186       _ref = name.split(' ');
187       _results = [];
188       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
189         cls = _ref[_i];
190         if (cls.trim()) {
191           _results.push(el.classList.add(cls));
192         }
193       }
194       return _results;
195     } else {
196       removeClass(el, name);
197       return el.className += " " + name;
198     }
199   };
200
201   hasClass = function(el, name) {
202     if (el.classList != null) {
203       return el.classList.contains(name);
204     } else {
205       return new RegExp("(^| )" + name + "( |$)", 'gi').test(el.className);
206     }
207   };
208
209   updateClasses = function(el, add, all) {
210     var cls, _i, _j, _len, _len1, _results;
211     for (_i = 0, _len = all.length; _i < _len; _i++) {
212       cls = all[_i];
213       if (__indexOf.call(add, cls) < 0) {
214         if (hasClass(el, cls)) {
215           removeClass(el, cls);
216         }
217       }
218     }
219     _results = [];
220     for (_j = 0, _len1 = add.length; _j < _len1; _j++) {
221       cls = add[_j];
222       if (!hasClass(el, cls)) {
223         _results.push(addClass(el, cls));
224       } else {
225         _results.push(void 0);
226       }
227     }
228     return _results;
229   };
230
231   deferred = [];
232
233   defer = function(fn) {
234     return deferred.push(fn);
235   };
236
237   flush = function() {
238     var fn, _results;
239     _results = [];
240     while (fn = deferred.pop()) {
241       _results.push(fn());
242     }
243     return _results;
244   };
245
246   Evented = (function() {
247     function Evented() {}
248
249     Evented.prototype.on = function(event, handler, ctx, once) {
250       var _base;
251       if (once == null) {
252         once = false;
253       }
254       if (this.bindings == null) {
255         this.bindings = {};
256       }
257       if ((_base = this.bindings)[event] == null) {
258         _base[event] = [];
259       }
260       return this.bindings[event].push({
261         handler: handler,
262         ctx: ctx,
263         once: once
264       });
265     };
266
267     Evented.prototype.once = function(event, handler, ctx) {
268       return this.on(event, handler, ctx, true);
269     };
270
271     Evented.prototype.off = function(event, handler) {
272       var i, _ref, _results;
273       if (((_ref = this.bindings) != null ? _ref[event] : void 0) == null) {
274         return;
275       }
276       if (handler == null) {
277         return delete this.bindings[event];
278       } else {
279         i = 0;
280         _results = [];
281         while (i < this.bindings[event].length) {
282           if (this.bindings[event][i].handler === handler) {
283             _results.push(this.bindings[event].splice(i, 1));
284           } else {
285             _results.push(i++);
286           }
287         }
288         return _results;
289       }
290     };
291
292     Evented.prototype.trigger = function() {
293       var args, ctx, event, handler, i, once, _ref, _ref1, _results;
294       event = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
295       if ((_ref = this.bindings) != null ? _ref[event] : void 0) {
296         i = 0;
297         _results = [];
298         while (i < this.bindings[event].length) {
299           _ref1 = this.bindings[event][i], handler = _ref1.handler, ctx = _ref1.ctx, once = _ref1.once;
300           handler.apply(ctx != null ? ctx : this, args);
301           if (once) {
302             _results.push(this.bindings[event].splice(i, 1));
303           } else {
304             _results.push(i++);
305           }
306         }
307         return _results;
308       }
309     };
310
311     return Evented;
312
313   })();
314
315   this.Tether.Utils = {
316     getScrollParent: getScrollParent,
317     getBounds: getBounds,
318     getOffsetParent: getOffsetParent,
319     extend: extend,
320     addClass: addClass,
321     removeClass: removeClass,
322     hasClass: hasClass,
323     updateClasses: updateClasses,
324     defer: defer,
325     flush: flush,
326     uniqueId: uniqueId,
327     Evented: Evented,
328     getScrollBarSize: getScrollBarSize
329   };
330
331 }).call(this);