Update JSON API
[src/app-framework-demo.git] / afm-client / bower_components / tether / docs / welcome / js / drop.js
1 (function() {
2   var Evented, MIRROR_ATTACH, addClass, allDrops, clickEvent, createContext, extend, hasClass, removeClass, sortAttach, touchDevice, _ref,
3     __hasProp = {}.hasOwnProperty,
4     __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
5     __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; };
6
7   _ref = Tether.Utils, extend = _ref.extend, addClass = _ref.addClass, removeClass = _ref.removeClass, hasClass = _ref.hasClass, Evented = _ref.Evented;
8
9   touchDevice = 'ontouchstart' in document.documentElement;
10
11   clickEvent = touchDevice ? 'touchstart' : 'click';
12
13   sortAttach = function(str) {
14     var first, second, _ref1, _ref2;
15     _ref1 = str.split(' '), first = _ref1[0], second = _ref1[1];
16     if (first === 'left' || first === 'right') {
17       _ref2 = [second, first], first = _ref2[0], second = _ref2[1];
18     }
19     return [first, second].join(' ');
20   };
21
22   MIRROR_ATTACH = {
23     left: 'right',
24     right: 'left',
25     top: 'bottom',
26     bottom: 'top',
27     middle: 'middle',
28     center: 'center'
29   };
30
31   allDrops = {};
32
33   createContext = function(options) {
34     var DropInstance, defaultOptions, drop, _name;
35     if (options == null) {
36       options = {};
37     }
38     drop = function() {
39       return (function(func, args, ctor) {
40         ctor.prototype = func.prototype;
41         var child = new ctor, result = func.apply(child, args);
42         return Object(result) === result ? result : child;
43       })(DropInstance, arguments, function(){});
44     };
45     extend(drop, {
46       createContext: createContext,
47       drops: [],
48       defaults: {}
49     });
50     defaultOptions = {
51       classPrefix: 'drop',
52       defaults: {
53         attach: 'bottom left',
54         openOn: 'click',
55         constrainToScrollParent: true,
56         constrainToWindow: true,
57         classes: '',
58         tetherOptions: {}
59       }
60     };
61     extend(drop, defaultOptions, options);
62     extend(drop.defaults, defaultOptions.defaults, options.defaults);
63     if (allDrops[_name = drop.classPrefix] == null) {
64       allDrops[_name] = [];
65     }
66     drop.updateBodyClasses = function() {
67       var anyOpen, _drop, _i, _len, _ref1;
68       anyOpen = false;
69       _ref1 = allDrops[drop.classPrefix];
70       for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
71         _drop = _ref1[_i];
72         if (!(_drop.isOpened())) {
73           continue;
74         }
75         anyOpen = true;
76         break;
77       }
78       if (anyOpen) {
79         return addClass(document.body, "" + drop.classPrefix + "-open");
80       } else {
81         return removeClass(document.body, "" + drop.classPrefix + "-open");
82       }
83     };
84     DropInstance = (function(_super) {
85       __extends(DropInstance, _super);
86
87       function DropInstance(options) {
88         this.options = options;
89         this.options = extend({}, drop.defaults, this.options);
90         this.target = this.options.target;
91         if (this.target == null) {
92           throw new Error('Drop Error: You must provide a target.');
93         }
94         drop.drops.push(this);
95         allDrops[drop.classPrefix].push(this);
96         this.setupElements();
97         this.setupEvents();
98         this.setupTether();
99       }
100
101       DropInstance.prototype.setupElements = function() {
102         this.drop = document.createElement('div');
103         addClass(this.drop, drop.classPrefix);
104         if (this.options.classes) {
105           addClass(this.drop, this.options.classes);
106         }
107         this.dropContent = document.createElement('div');
108         addClass(this.dropContent, "" + drop.classPrefix + "-content");
109         if (typeof this.options.content === 'object') {
110           this.dropContent.appendChild(this.options.content);
111         } else {
112           this.dropContent.innerHTML = this.options.content;
113         }
114         return this.drop.appendChild(this.dropContent);
115       };
116
117       DropInstance.prototype.setupTether = function() {
118         var constraints, dropAttach;
119         dropAttach = this.options.position.split(' ');
120         dropAttach[0] = MIRROR_ATTACH[dropAttach[0]];
121         dropAttach = dropAttach.join(' ');
122         constraints = [];
123         if (this.options.constrainToScrollParent) {
124           constraints.push({
125             to: 'scrollParent',
126             pin: 'top, bottom',
127             attachment: 'together none'
128           });
129         }
130         if (this.options.constrainToWindow !== false) {
131           constraints.push({
132             to: 'window',
133             pin: true,
134             attachment: 'together'
135           });
136         }
137         constraints.push({
138           to: 'scrollParent'
139         });
140         options = {
141           element: this.drop,
142           target: this.target,
143           attachment: sortAttach(dropAttach),
144           targetAttachment: sortAttach(this.options.position),
145           classPrefix: drop.classPrefix,
146           offset: '0 0',
147           targetOffset: '0 0',
148           enabled: false,
149           constraints: constraints
150         };
151         if (this.options.tether !== false) {
152           return this.tether = new Tether(extend({}, options, this.options.tether));
153         }
154       };
155
156       DropInstance.prototype.setupEvents = function() {
157         var events,
158           _this = this;
159         if (!this.options.openOn) {
160           return;
161         }
162         events = this.options.openOn.split(' ');
163         if (__indexOf.call(events, 'click') >= 0) {
164           this.target.addEventListener(clickEvent, function() {
165             return _this.toggle();
166           });
167           document.addEventListener(clickEvent, function(event) {
168             if (!_this.isOpened()) {
169               return;
170             }
171             if (event.target === _this.drop || _this.drop.contains(event.target)) {
172               return;
173             }
174             if (event.target === _this.target || _this.target.contains(event.target)) {
175               return;
176             }
177             return _this.close();
178           });
179         }
180         if (__indexOf.call(events, 'hover') >= 0) {
181           this.target.addEventListener('mouseover', function() {
182             return _this.open();
183           });
184           return this.target.addEventListener('mouseout', function() {
185             return _this.close();
186           });
187         }
188       };
189
190       DropInstance.prototype.isOpened = function() {
191         return hasClass(this.drop, "" + drop.classPrefix + "-open");
192       };
193
194       DropInstance.prototype.toggle = function() {
195         if (this.isOpened()) {
196           return this.close();
197         } else {
198           return this.open();
199         }
200       };
201
202       DropInstance.prototype.open = function() {
203         var _ref1;
204         if (!this.drop.parentNode) {
205           document.body.appendChild(this.drop);
206         }
207         addClass(this.target, "" + drop.classPrefix + "-open");
208         addClass(this.drop, "" + drop.classPrefix + "-open");
209         if ((_ref1 = this.tether) != null) {
210           _ref1.enable();
211         }
212         this.trigger('open');
213         return drop.updateBodyClasses();
214       };
215
216       DropInstance.prototype.close = function() {
217         var _ref1;
218         removeClass(this.target, "" + drop.classPrefix + "-open");
219         removeClass(this.drop, "" + drop.classPrefix + "-open");
220         this.trigger('close');
221         if ((_ref1 = this.tether) != null) {
222           _ref1.disable();
223         }
224         return drop.updateBodyClasses();
225       };
226
227       return DropInstance;
228
229     })(Evented);
230     return drop;
231   };
232
233   window.Drop = createContext();
234
235   document.addEventListener('DOMContentLoaded', function() {
236     return Drop.updateBodyClasses();
237   });
238
239 }).call(this);