PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/tether-drop/0.5.8/js/drop.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 387 lines | 358 code | 29 blank | 0 comment | 70 complexity | b3187dec1e476f0978bf998fd5422ec4 MD5 | raw file
  1. (function() {
  2. var Evented, MIRROR_ATTACH, addClass, allDrops, clickEvents, createContext, end, extend, hasClass, name, removeClass, removeFromArray, sortAttach, tempEl, touchDevice, transitionEndEvent, transitionEndEvents, _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. _ref = Tether.Utils, extend = _ref.extend, addClass = _ref.addClass, removeClass = _ref.removeClass, hasClass = _ref.hasClass, Evented = _ref.Evented;
  7. touchDevice = 'ontouchstart' in document.documentElement;
  8. clickEvents = ['click'];
  9. if (touchDevice) {
  10. clickEvents.push('touchstart');
  11. }
  12. transitionEndEvents = {
  13. 'WebkitTransition': 'webkitTransitionEnd',
  14. 'MozTransition': 'transitionend',
  15. 'OTransition': 'otransitionend',
  16. 'transition': 'transitionend'
  17. };
  18. transitionEndEvent = '';
  19. for (name in transitionEndEvents) {
  20. end = transitionEndEvents[name];
  21. tempEl = document.createElement('p');
  22. if (tempEl.style[name] !== void 0) {
  23. transitionEndEvent = end;
  24. }
  25. }
  26. sortAttach = function(str) {
  27. var first, second, _ref1, _ref2;
  28. _ref1 = str.split(' '), first = _ref1[0], second = _ref1[1];
  29. if (first === 'left' || first === 'right') {
  30. _ref2 = [second, first], first = _ref2[0], second = _ref2[1];
  31. }
  32. return [first, second].join(' ');
  33. };
  34. MIRROR_ATTACH = {
  35. left: 'right',
  36. right: 'left',
  37. top: 'bottom',
  38. bottom: 'top',
  39. middle: 'middle',
  40. center: 'center'
  41. };
  42. allDrops = {};
  43. removeFromArray = function(arr, item) {
  44. var index, _results;
  45. _results = [];
  46. while ((index = arr.indexOf(item)) !== -1) {
  47. _results.push(arr.splice(index, 1));
  48. }
  49. return _results;
  50. };
  51. createContext = function(options) {
  52. var DropInstance, defaultOptions, drop, _name;
  53. if (options == null) {
  54. options = {};
  55. }
  56. drop = function() {
  57. return (function(func, args, ctor) {
  58. ctor.prototype = func.prototype;
  59. var child = new ctor, result = func.apply(child, args);
  60. return Object(result) === result ? result : child;
  61. })(DropInstance, arguments, function(){});
  62. };
  63. extend(drop, {
  64. createContext: createContext,
  65. drops: [],
  66. defaults: {}
  67. });
  68. defaultOptions = {
  69. classPrefix: 'drop',
  70. defaults: {
  71. position: 'bottom left',
  72. openOn: 'click',
  73. constrainToScrollParent: true,
  74. constrainToWindow: true,
  75. classes: '',
  76. remove: false,
  77. tetherOptions: {}
  78. }
  79. };
  80. extend(drop, defaultOptions, options);
  81. extend(drop.defaults, defaultOptions.defaults, options.defaults);
  82. if (allDrops[_name = drop.classPrefix] == null) {
  83. allDrops[_name] = [];
  84. }
  85. drop.updateBodyClasses = function() {
  86. var anyOpen, _drop, _i, _len, _ref1;
  87. anyOpen = false;
  88. _ref1 = allDrops[drop.classPrefix];
  89. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  90. _drop = _ref1[_i];
  91. if (!(_drop.isOpened())) {
  92. continue;
  93. }
  94. anyOpen = true;
  95. break;
  96. }
  97. if (anyOpen) {
  98. return addClass(document.body, "" + drop.classPrefix + "-open");
  99. } else {
  100. return removeClass(document.body, "" + drop.classPrefix + "-open");
  101. }
  102. };
  103. DropInstance = (function(_super) {
  104. __extends(DropInstance, _super);
  105. function DropInstance(options) {
  106. this.options = options;
  107. this.options = extend({}, drop.defaults, this.options);
  108. this.target = this.options.target;
  109. if (this.target == null) {
  110. throw new Error('Drop Error: You must provide a target.');
  111. }
  112. if (this.options.classes && this.options.addTargetClasses !== false) {
  113. addClass(this.target, this.options.classes);
  114. }
  115. drop.drops.push(this);
  116. allDrops[drop.classPrefix].push(this);
  117. this._boundEvents = [];
  118. this.setupElements();
  119. this.setupEvents();
  120. this.setupTether();
  121. }
  122. DropInstance.prototype._on = function(element, event, handler) {
  123. this._boundEvents.push({
  124. element: element,
  125. event: event,
  126. handler: handler
  127. });
  128. return element.addEventListener(event, handler);
  129. };
  130. DropInstance.prototype.setupElements = function() {
  131. var generateAndSetContent,
  132. _this = this;
  133. this.drop = document.createElement('div');
  134. addClass(this.drop, drop.classPrefix);
  135. if (this.options.classes) {
  136. addClass(this.drop, this.options.classes);
  137. }
  138. this.content = document.createElement('div');
  139. addClass(this.content, "" + drop.classPrefix + "-content");
  140. if (typeof this.options.content === 'function') {
  141. generateAndSetContent = function() {
  142. var contentElementOrHTML;
  143. contentElementOrHTML = _this.options.content.call(_this, _this);
  144. if (typeof contentElementOrHTML === 'string') {
  145. return _this.content.innerHTML = contentElementOrHTML;
  146. } else if (typeof contentElementOrHTML === 'object') {
  147. _this.content.innerHTML = "";
  148. return _this.content.appendChild(contentElementOrHTML);
  149. } else {
  150. throw new Error('Drop Error: Content function should return a string or HTMLElement.');
  151. }
  152. };
  153. generateAndSetContent();
  154. this.on('open', generateAndSetContent.bind(this));
  155. } else if (typeof this.options.content === 'object') {
  156. this.content.appendChild(this.options.content);
  157. } else {
  158. this.content.innerHTML = this.options.content;
  159. }
  160. return this.drop.appendChild(this.content);
  161. };
  162. DropInstance.prototype.setupTether = function() {
  163. var constraints, dropAttach;
  164. dropAttach = this.options.position.split(' ');
  165. dropAttach[0] = MIRROR_ATTACH[dropAttach[0]];
  166. dropAttach = dropAttach.join(' ');
  167. constraints = [];
  168. if (this.options.constrainToScrollParent) {
  169. constraints.push({
  170. to: 'scrollParent',
  171. pin: 'top, bottom',
  172. attachment: 'together none'
  173. });
  174. } else {
  175. constraints.push({
  176. to: 'scrollParent'
  177. });
  178. }
  179. if (this.options.constrainToWindow !== false) {
  180. constraints.push({
  181. to: 'window',
  182. attachment: 'together'
  183. });
  184. } else {
  185. constraints.push({
  186. to: 'window'
  187. });
  188. }
  189. options = {
  190. element: this.drop,
  191. target: this.target,
  192. attachment: sortAttach(dropAttach),
  193. targetAttachment: sortAttach(this.options.position),
  194. classPrefix: drop.classPrefix,
  195. offset: '0 0',
  196. targetOffset: '0 0',
  197. enabled: false,
  198. constraints: constraints,
  199. addTargetClasses: this.options.addTargetClasses
  200. };
  201. if (this.options.tetherOptions !== false) {
  202. return this.tether = new Tether(extend({}, options, this.options.tetherOptions));
  203. }
  204. };
  205. DropInstance.prototype.setupEvents = function() {
  206. var clickEvent, closeHandler, events, onUs, openHandler, out, outTimeout, over, _i, _len,
  207. _this = this;
  208. if (!this.options.openOn) {
  209. return;
  210. }
  211. if (this.options.openOn === 'always') {
  212. setTimeout(this.open.bind(this));
  213. return;
  214. }
  215. events = this.options.openOn.split(' ');
  216. if (__indexOf.call(events, 'click') >= 0) {
  217. openHandler = function(event) {
  218. _this.toggle();
  219. return event.preventDefault();
  220. };
  221. closeHandler = function(event) {
  222. if (!_this.isOpened()) {
  223. return;
  224. }
  225. if (event.target === _this.drop || _this.drop.contains(event.target)) {
  226. return;
  227. }
  228. if (event.target === _this.target || _this.target.contains(event.target)) {
  229. return;
  230. }
  231. return _this.close();
  232. };
  233. for (_i = 0, _len = clickEvents.length; _i < _len; _i++) {
  234. clickEvent = clickEvents[_i];
  235. this._on(this.target, clickEvent, openHandler);
  236. this._on(document, clickEvent, closeHandler);
  237. }
  238. }
  239. if (__indexOf.call(events, 'hover') >= 0) {
  240. onUs = false;
  241. over = function() {
  242. onUs = true;
  243. return _this.open();
  244. };
  245. outTimeout = null;
  246. out = function() {
  247. onUs = false;
  248. if (outTimeout != null) {
  249. clearTimeout(outTimeout);
  250. }
  251. return outTimeout = setTimeout(function() {
  252. if (!onUs) {
  253. _this.close();
  254. }
  255. return outTimeout = null;
  256. }, 50);
  257. };
  258. this._on(this.target, 'mouseover', over);
  259. this._on(this.drop, 'mouseover', over);
  260. this._on(this.target, 'mouseout', out);
  261. return this._on(this.drop, 'mouseout', out);
  262. }
  263. };
  264. DropInstance.prototype.isOpened = function() {
  265. return hasClass(this.drop, "" + drop.classPrefix + "-open");
  266. };
  267. DropInstance.prototype.toggle = function() {
  268. if (this.isOpened()) {
  269. return this.close();
  270. } else {
  271. return this.open();
  272. }
  273. };
  274. DropInstance.prototype.open = function() {
  275. var _ref1, _ref2,
  276. _this = this;
  277. if (this.isOpened()) {
  278. return;
  279. }
  280. if (!this.drop.parentNode) {
  281. document.body.appendChild(this.drop);
  282. }
  283. if ((_ref1 = this.tether) != null) {
  284. _ref1.enable();
  285. }
  286. addClass(this.drop, "" + drop.classPrefix + "-open");
  287. addClass(this.drop, "" + drop.classPrefix + "-open-transitionend");
  288. setTimeout(function() {
  289. return addClass(_this.drop, "" + drop.classPrefix + "-after-open");
  290. });
  291. if ((_ref2 = this.tether) != null) {
  292. _ref2.position();
  293. }
  294. this.trigger('open');
  295. return drop.updateBodyClasses();
  296. };
  297. DropInstance.prototype.close = function() {
  298. var handler, _ref1,
  299. _this = this;
  300. if (!this.isOpened()) {
  301. return;
  302. }
  303. removeClass(this.drop, "" + drop.classPrefix + "-open");
  304. removeClass(this.drop, "" + drop.classPrefix + "-after-open");
  305. this.drop.addEventListener(transitionEndEvent, handler = function() {
  306. if (!hasClass(_this.drop, "" + drop.classPrefix + "-open")) {
  307. removeClass(_this.drop, "" + drop.classPrefix + "-open-transitionend");
  308. }
  309. return _this.drop.removeEventListener(transitionEndEvent, handler);
  310. });
  311. this.trigger('close');
  312. if ((_ref1 = this.tether) != null) {
  313. _ref1.disable();
  314. }
  315. drop.updateBodyClasses();
  316. if (this.options.remove) {
  317. return this.remove();
  318. }
  319. };
  320. DropInstance.prototype.remove = function() {
  321. var _ref1;
  322. this.close();
  323. return (_ref1 = this.drop.parentNode) != null ? _ref1.removeChild(this.drop) : void 0;
  324. };
  325. DropInstance.prototype.position = function() {
  326. var _ref1;
  327. if (this.isOpened()) {
  328. return (_ref1 = this.tether) != null ? _ref1.position() : void 0;
  329. }
  330. };
  331. DropInstance.prototype.destroy = function() {
  332. var element, event, handler, _i, _len, _ref1, _ref2, _ref3;
  333. this.remove();
  334. if ((_ref1 = this.tether) != null) {
  335. _ref1.destroy();
  336. }
  337. _ref2 = this._boundEvents;
  338. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  339. _ref3 = _ref2[_i], element = _ref3.element, event = _ref3.event, handler = _ref3.handler;
  340. element.removeEventListener(event, handler);
  341. }
  342. this._boundEvents = [];
  343. this.tether = null;
  344. this.drop = null;
  345. this.content = null;
  346. this.target = null;
  347. removeFromArray(allDrops[drop.classPrefix], this);
  348. return removeFromArray(drop.drops, this);
  349. };
  350. return DropInstance;
  351. })(Evented);
  352. return drop;
  353. };
  354. window.Drop = createContext();
  355. document.addEventListener('DOMContentLoaded', function() {
  356. return Drop.updateBodyClasses();
  357. });
  358. }).call(this);