gridstack.js 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842
  1. /**
  2. * gridstack.js 0.4.0
  3. * http://troolee.github.io/gridstack.js/
  4. * (c) 2014-2018 Pavel Reznikov, Dylan Weiss
  5. * gridstack.js may be freely distributed under the MIT license.
  6. * @preserve
  7. */
  8. (function(factory) {
  9. if (typeof define === 'function' && define.amd) {
  10. define(['jquery', 'lodash'], factory);
  11. } else if (typeof exports !== 'undefined') {
  12. try { jQuery = require('jquery'); } catch (e) {}
  13. try { _ = require('lodash'); } catch (e) {}
  14. factory(jQuery, _);
  15. } else {
  16. factory(jQuery, _);
  17. }
  18. })(function($, _) {
  19. var scope = window;
  20. var obsolete = function(f, oldName, newName) {
  21. var wrapper = function() {
  22. console.warn('gridstack.js: Function `' + oldName + '` is deprecated as of v0.2.5 and has been replaced ' +
  23. 'with `' + newName + '`. It will be **completely** removed in v1.0.');
  24. return f.apply(this, arguments);
  25. };
  26. wrapper.prototype = f.prototype;
  27. return wrapper;
  28. };
  29. var obsoleteOpts = function(oldName, newName) {
  30. console.warn('gridstack.js: Option `' + oldName + '` is deprecated as of v0.2.5 and has been replaced with `' +
  31. newName + '`. It will be **completely** removed in v1.0.');
  32. };
  33. var Utils = {
  34. isIntercepted: function(a, b) {
  35. return !(a.x + a.width <= b.x || b.x + b.width <= a.x || a.y + a.height <= b.y || b.y + b.height <= a.y);
  36. },
  37. sort: function(nodes, dir, width) {
  38. width = width || _.chain(nodes).map(function(node) { return node.x + node.width; }).max().value();
  39. dir = dir != -1 ? 1 : -1;
  40. return _.sortBy(nodes, [function(n) { return dir * (n.x + n.y * width); }]);
  41. },
  42. createStylesheet: function(id) {
  43. var style = document.createElement('style');
  44. style.setAttribute('type', 'text/css');
  45. style.setAttribute('data-gs-style-id', id);
  46. if (style.styleSheet) {
  47. style.styleSheet.cssText = '';
  48. } else {
  49. style.appendChild(document.createTextNode(''));
  50. }
  51. document.getElementsByTagName('head')[0].appendChild(style);
  52. return style.sheet;
  53. },
  54. removeStylesheet: function(id) {
  55. $('STYLE[data-gs-style-id=' + id + ']').remove();
  56. },
  57. insertCSSRule: function(sheet, selector, rules, index) {
  58. if (typeof sheet.insertRule === 'function') {
  59. sheet.insertRule(selector + '{' + rules + '}', index);
  60. } else if (typeof sheet.addRule === 'function') {
  61. sheet.addRule(selector, rules, index);
  62. }
  63. },
  64. toBool: function(v) {
  65. if (typeof v == 'boolean') {
  66. return v;
  67. }
  68. if (typeof v == 'string') {
  69. v = v.toLowerCase();
  70. return !(v === '' || v == 'no' || v == 'false' || v == '0');
  71. }
  72. return Boolean(v);
  73. },
  74. _collisionNodeCheck: function(n) {
  75. return n != this.node && Utils.isIntercepted(n, this.nn);
  76. },
  77. _didCollide: function(bn) {
  78. return Utils.isIntercepted({x: this.n.x, y: this.newY, width: this.n.width, height: this.n.height}, bn);
  79. },
  80. _isAddNodeIntercepted: function(n) {
  81. return Utils.isIntercepted({x: this.x, y: this.y, width: this.node.width, height: this.node.height}, n);
  82. },
  83. parseHeight: function(val) {
  84. var height = val;
  85. var heightUnit = 'px';
  86. if (height && _.isString(height)) {
  87. var match = height.match(/^(-[0-9]+\.[0-9]+|[0-9]*\.[0-9]+|-[0-9]+|[0-9]+)(px|em|rem|vh|vw)?$/);
  88. if (!match) {
  89. throw new Error('Invalid height');
  90. }
  91. heightUnit = match[2] || 'px';
  92. height = parseFloat(match[1]);
  93. }
  94. return {height: height, unit: heightUnit};
  95. },
  96. removePositioningStyles: function(el) {
  97. var style = el[0].style;
  98. if (style.position) {
  99. style.removeProperty('position');
  100. }
  101. if (style.left) {
  102. style.removeProperty('left');
  103. }
  104. if (style.top) {
  105. style.removeProperty('top');
  106. }
  107. if (style.width) {
  108. style.removeProperty('width');
  109. }
  110. if (style.height) {
  111. style.removeProperty('height');
  112. }
  113. },
  114. getScrollParent: function(el) {
  115. var returnEl;
  116. if (el == null) {
  117. returnEl = null;
  118. } else if (el.scrollHeight > el.clientHeight) {
  119. returnEl = el;
  120. } else {
  121. returnEl = Utils.getScrollParent(el.parentNode);
  122. }
  123. return returnEl;
  124. },
  125. updateScrollPosition: function(el, ui, distance) {
  126. // is widget in view?
  127. var rect = el.getBoundingClientRect();
  128. var innerHeightOrClientHeight = (window.innerHeight || document.documentElement.clientHeight);
  129. if (rect.top < 0 ||
  130. rect.bottom > innerHeightOrClientHeight
  131. ) {
  132. // set scrollTop of first parent that scrolls
  133. // if parent is larger than el, set as low as possible
  134. // to get entire widget on screen
  135. var offsetDiffDown = rect.bottom - innerHeightOrClientHeight;
  136. var offsetDiffUp = rect.top;
  137. var scrollEl = Utils.getScrollParent(el);
  138. var prevScroll = scrollEl.scrollTop;
  139. if (scrollEl != null) {
  140. if (rect.top < 0 && distance < 0) {
  141. // moving up
  142. if (el.offsetHeight > innerHeightOrClientHeight) {
  143. scrollEl.scrollTop += distance;
  144. } else {
  145. scrollEl.scrollTop += Math.abs(offsetDiffUp) > Math.abs(distance) ? distance : offsetDiffUp;
  146. }
  147. } else if (distance > 0) {
  148. // moving down
  149. if (el.offsetHeight > innerHeightOrClientHeight) {
  150. scrollEl.scrollTop += distance;
  151. } else {
  152. scrollEl.scrollTop += offsetDiffDown > distance ? distance : offsetDiffDown;
  153. }
  154. }
  155. // move widget y by amount scrolled
  156. ui.position.top += scrollEl.scrollTop - prevScroll;
  157. }
  158. }
  159. }
  160. };
  161. // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
  162. Utils.is_intercepted = obsolete(Utils.isIntercepted, 'is_intercepted', 'isIntercepted');
  163. Utils.create_stylesheet = obsolete(Utils.createStylesheet, 'create_stylesheet', 'createStylesheet');
  164. Utils.remove_stylesheet = obsolete(Utils.removeStylesheet, 'remove_stylesheet', 'removeStylesheet');
  165. Utils.insert_css_rule = obsolete(Utils.insertCSSRule, 'insert_css_rule', 'insertCSSRule');
  166. // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
  167. /**
  168. * @class GridStackDragDropPlugin
  169. * Base class for drag'n'drop plugin.
  170. */
  171. function GridStackDragDropPlugin(grid) {
  172. this.grid = grid;
  173. }
  174. GridStackDragDropPlugin.registeredPlugins = [];
  175. GridStackDragDropPlugin.registerPlugin = function(pluginClass) {
  176. GridStackDragDropPlugin.registeredPlugins.push(pluginClass);
  177. };
  178. GridStackDragDropPlugin.prototype.resizable = function(el, opts) {
  179. return this;
  180. };
  181. GridStackDragDropPlugin.prototype.draggable = function(el, opts) {
  182. return this;
  183. };
  184. GridStackDragDropPlugin.prototype.droppable = function(el, opts) {
  185. return this;
  186. };
  187. GridStackDragDropPlugin.prototype.isDroppable = function(el) {
  188. return false;
  189. };
  190. GridStackDragDropPlugin.prototype.on = function(el, eventName, callback) {
  191. return this;
  192. };
  193. var idSeq = 0;
  194. var GridStackEngine = function(width, onchange, floatMode, height, items) {
  195. this.width = width;
  196. this.float = floatMode || false;
  197. this.height = height || 0;
  198. this.nodes = items || [];
  199. this.onchange = onchange || function() {};
  200. this._updateCounter = 0;
  201. this._float = this.float;
  202. this._addedNodes = [];
  203. this._removedNodes = [];
  204. };
  205. GridStackEngine.prototype.batchUpdate = function() {
  206. this._updateCounter = 1;
  207. this.float = true;
  208. };
  209. GridStackEngine.prototype.commit = function() {
  210. if (this._updateCounter !== 0) {
  211. this._updateCounter = 0;
  212. this.float = this._float;
  213. this._packNodes();
  214. this._notify();
  215. }
  216. };
  217. // For Meteor support: https://github.com/troolee/gridstack.js/pull/272
  218. GridStackEngine.prototype.getNodeDataByDOMEl = function(el) {
  219. return _.find(this.nodes, function(n) { return el.get(0) === n.el.get(0); });
  220. };
  221. GridStackEngine.prototype._fixCollisions = function(node) {
  222. var self = this;
  223. this._sortNodes(-1);
  224. var nn = node;
  225. var hasLocked = Boolean(_.find(this.nodes, function(n) { return n.locked; }));
  226. if (!this.float && !hasLocked) {
  227. nn = {x: 0, y: node.y, width: this.width, height: node.height};
  228. }
  229. while (true) {
  230. var collisionNode = _.find(this.nodes, _.bind(Utils._collisionNodeCheck, {node: node, nn: nn}));
  231. if (typeof collisionNode == 'undefined') {
  232. return;
  233. }
  234. this.moveNode(collisionNode, collisionNode.x, node.y + node.height,
  235. collisionNode.width, collisionNode.height, true);
  236. }
  237. };
  238. GridStackEngine.prototype.isAreaEmpty = function(x, y, width, height) {
  239. var nn = {x: x || 0, y: y || 0, width: width || 1, height: height || 1};
  240. var collisionNode = _.find(this.nodes, _.bind(function(n) {
  241. return Utils.isIntercepted(n, nn);
  242. }, this));
  243. return collisionNode === null || typeof collisionNode === 'undefined';
  244. };
  245. GridStackEngine.prototype._sortNodes = function(dir) {
  246. this.nodes = Utils.sort(this.nodes, dir, this.width);
  247. };
  248. GridStackEngine.prototype._packNodes = function() {
  249. this._sortNodes();
  250. if (this.float) {
  251. _.each(this.nodes, _.bind(function(n, i) {
  252. if (n._updating || typeof n._origY == 'undefined' || n.y == n._origY) {
  253. return;
  254. }
  255. var newY = n.y;
  256. while (newY >= n._origY) {
  257. var collisionNode = _.chain(this.nodes)
  258. .take(i)
  259. .find(_.bind(Utils._didCollide, {n: n, newY: newY}))
  260. .value();
  261. if (!collisionNode) {
  262. n._dirty = true;
  263. n.y = newY;
  264. }
  265. --newY;
  266. }
  267. }, this));
  268. } else {
  269. _.each(this.nodes, _.bind(function(n, i) {
  270. if (n.locked) {
  271. return;
  272. }
  273. while (n.y > 0) {
  274. var newY = n.y - 1;
  275. var canBeMoved = i === 0;
  276. if (i > 0) {
  277. var collisionNode = _.chain(this.nodes)
  278. .take(i)
  279. .find(_.bind(Utils._didCollide, {n: n, newY: newY}))
  280. .value();
  281. canBeMoved = typeof collisionNode == 'undefined';
  282. }
  283. if (!canBeMoved) {
  284. break;
  285. }
  286. n._dirty = n.y != newY;
  287. n.y = newY;
  288. }
  289. }, this));
  290. }
  291. };
  292. GridStackEngine.prototype._prepareNode = function(node, resizing) {
  293. node = _.defaults(node || {}, {width: 1, height: 1, x: 0, y: 0});
  294. node.x = parseInt('' + node.x);
  295. node.y = parseInt('' + node.y);
  296. node.width = parseInt('' + node.width);
  297. node.height = parseInt('' + node.height);
  298. node.autoPosition = node.autoPosition || false;
  299. node.noResize = node.noResize || false;
  300. node.noMove = node.noMove || false;
  301. if (node.width > this.width) {
  302. node.width = this.width;
  303. } else if (node.width < 1) {
  304. node.width = 1;
  305. }
  306. if (node.height < 1) {
  307. node.height = 1;
  308. }
  309. if (node.x < 0) {
  310. node.x = 0;
  311. }
  312. if (node.x + node.width > this.width) {
  313. if (resizing) {
  314. node.width = this.width - node.x;
  315. } else {
  316. node.x = this.width - node.width;
  317. }
  318. }
  319. if (node.y < 0) {
  320. node.y = 0;
  321. }
  322. return node;
  323. };
  324. GridStackEngine.prototype._notify = function() {
  325. var args = Array.prototype.slice.call(arguments, 0);
  326. args[0] = typeof args[0] === 'undefined' ? [] : [args[0]];
  327. args[1] = typeof args[1] === 'undefined' ? true : args[1];
  328. if (this._updateCounter) {
  329. return;
  330. }
  331. var deletedNodes = args[0].concat(this.getDirtyNodes());
  332. this.onchange(deletedNodes, args[1]);
  333. };
  334. GridStackEngine.prototype.cleanNodes = function() {
  335. if (this._updateCounter) {
  336. return;
  337. }
  338. _.each(this.nodes, function(n) {n._dirty = false; });
  339. };
  340. GridStackEngine.prototype.getDirtyNodes = function() {
  341. return _.filter(this.nodes, function(n) { return n._dirty; });
  342. };
  343. GridStackEngine.prototype.addNode = function(node, triggerAddEvent) {
  344. node = this._prepareNode(node);
  345. if (typeof node.maxWidth != 'undefined') { node.width = Math.min(node.width, node.maxWidth); }
  346. if (typeof node.maxHeight != 'undefined') { node.height = Math.min(node.height, node.maxHeight); }
  347. if (typeof node.minWidth != 'undefined') { node.width = Math.max(node.width, node.minWidth); }
  348. if (typeof node.minHeight != 'undefined') { node.height = Math.max(node.height, node.minHeight); }
  349. node._id = ++idSeq;
  350. node._dirty = true;
  351. if (node.autoPosition) {
  352. this._sortNodes();
  353. for (var i = 0;; ++i) {
  354. var x = i % this.width;
  355. var y = Math.floor(i / this.width);
  356. if (x + node.width > this.width) {
  357. continue;
  358. }
  359. if (!_.find(this.nodes, _.bind(Utils._isAddNodeIntercepted, {x: x, y: y, node: node}))) {
  360. node.x = x;
  361. node.y = y;
  362. break;
  363. }
  364. }
  365. }
  366. this.nodes.push(node);
  367. if (typeof triggerAddEvent != 'undefined' && triggerAddEvent) {
  368. this._addedNodes.push(_.clone(node));
  369. }
  370. this._fixCollisions(node);
  371. this._packNodes();
  372. this._notify();
  373. return node;
  374. };
  375. GridStackEngine.prototype.removeNode = function(node, detachNode) {
  376. detachNode = typeof detachNode === 'undefined' ? true : detachNode;
  377. this._removedNodes.push(_.clone(node));
  378. node._id = null;
  379. this.nodes = _.without(this.nodes, node);
  380. this._packNodes();
  381. this._notify(node, detachNode);
  382. };
  383. GridStackEngine.prototype.canMoveNode = function(node, x, y, width, height) {
  384. if (!this.isNodeChangedPosition(node, x, y, width, height)) {
  385. return false;
  386. }
  387. var hasLocked = Boolean(_.find(this.nodes, function(n) { return n.locked; }));
  388. if (!this.height && !hasLocked) {
  389. return true;
  390. }
  391. var clonedNode;
  392. var clone = new GridStackEngine(
  393. this.width,
  394. null,
  395. this.float,
  396. 0,
  397. _.map(this.nodes, function(n) {
  398. if (n == node) {
  399. clonedNode = $.extend({}, n);
  400. return clonedNode;
  401. }
  402. return $.extend({}, n);
  403. }));
  404. if (typeof clonedNode === 'undefined') {
  405. return true;
  406. }
  407. clone.moveNode(clonedNode, x, y, width, height);
  408. var res = true;
  409. if (hasLocked) {
  410. res &= !Boolean(_.find(clone.nodes, function(n) {
  411. return n != clonedNode && Boolean(n.locked) && Boolean(n._dirty);
  412. }));
  413. }
  414. if (this.height) {
  415. res &= clone.getGridHeight() <= this.height;
  416. }
  417. return res;
  418. };
  419. GridStackEngine.prototype.canBePlacedWithRespectToHeight = function(node) {
  420. if (!this.height) {
  421. return true;
  422. }
  423. var clone = new GridStackEngine(
  424. this.width,
  425. null,
  426. this.float,
  427. 0,
  428. _.map(this.nodes, function(n) { return $.extend({}, n); }));
  429. clone.addNode(node);
  430. return clone.getGridHeight() <= this.height;
  431. };
  432. GridStackEngine.prototype.isNodeChangedPosition = function(node, x, y, width, height) {
  433. if (typeof x != 'number') { x = node.x; }
  434. if (typeof y != 'number') { y = node.y; }
  435. if (typeof width != 'number') { width = node.width; }
  436. if (typeof height != 'number') { height = node.height; }
  437. if (typeof node.maxWidth != 'undefined') { width = Math.min(width, node.maxWidth); }
  438. if (typeof node.maxHeight != 'undefined') { height = Math.min(height, node.maxHeight); }
  439. if (typeof node.minWidth != 'undefined') { width = Math.max(width, node.minWidth); }
  440. if (typeof node.minHeight != 'undefined') { height = Math.max(height, node.minHeight); }
  441. if (node.x == x && node.y == y && node.width == width && node.height == height) {
  442. return false;
  443. }
  444. return true;
  445. };
  446. GridStackEngine.prototype.moveNode = function(node, x, y, width, height, noPack) {
  447. if (!this.isNodeChangedPosition(node, x, y, width, height)) {
  448. return node;
  449. }
  450. if (typeof x != 'number') { x = node.x; }
  451. if (typeof y != 'number') { y = node.y; }
  452. if (typeof width != 'number') { width = node.width; }
  453. if (typeof height != 'number') { height = node.height; }
  454. if (typeof node.maxWidth != 'undefined') { width = Math.min(width, node.maxWidth); }
  455. if (typeof node.maxHeight != 'undefined') { height = Math.min(height, node.maxHeight); }
  456. if (typeof node.minWidth != 'undefined') { width = Math.max(width, node.minWidth); }
  457. if (typeof node.minHeight != 'undefined') { height = Math.max(height, node.minHeight); }
  458. if (node.x == x && node.y == y && node.width == width && node.height == height) {
  459. return node;
  460. }
  461. var resizing = node.width != width;
  462. node._dirty = true;
  463. node.x = x;
  464. node.y = y;
  465. node.width = width;
  466. node.height = height;
  467. node.lastTriedX = x;
  468. node.lastTriedY = y;
  469. node.lastTriedWidth = width;
  470. node.lastTriedHeight = height;
  471. node = this._prepareNode(node, resizing);
  472. this._fixCollisions(node);
  473. if (!noPack) {
  474. this._packNodes();
  475. this._notify();
  476. }
  477. return node;
  478. };
  479. GridStackEngine.prototype.getGridHeight = function() {
  480. return _.reduce(this.nodes, function(memo, n) { return Math.max(memo, n.y + n.height); }, 0);
  481. };
  482. GridStackEngine.prototype.beginUpdate = function(node) {
  483. _.each(this.nodes, function(n) {
  484. n._origY = n.y;
  485. });
  486. node._updating = true;
  487. };
  488. GridStackEngine.prototype.endUpdate = function() {
  489. _.each(this.nodes, function(n) {
  490. n._origY = n.y;
  491. });
  492. var n = _.find(this.nodes, function(n) { return n._updating; });
  493. if (n) {
  494. n._updating = false;
  495. }
  496. };
  497. var GridStack = function(el, opts) {
  498. var self = this;
  499. var oneColumnMode, isAutoCellHeight;
  500. opts = opts || {};
  501. this.container = $(el);
  502. // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
  503. if (typeof opts.handle_class !== 'undefined') {
  504. opts.handleClass = opts.handle_class;
  505. obsoleteOpts('handle_class', 'handleClass');
  506. }
  507. if (typeof opts.item_class !== 'undefined') {
  508. opts.itemClass = opts.item_class;
  509. obsoleteOpts('item_class', 'itemClass');
  510. }
  511. if (typeof opts.placeholder_class !== 'undefined') {
  512. opts.placeholderClass = opts.placeholder_class;
  513. obsoleteOpts('placeholder_class', 'placeholderClass');
  514. }
  515. if (typeof opts.placeholder_text !== 'undefined') {
  516. opts.placeholderText = opts.placeholder_text;
  517. obsoleteOpts('placeholder_text', 'placeholderText');
  518. }
  519. if (typeof opts.cell_height !== 'undefined') {
  520. opts.cellHeight = opts.cell_height;
  521. obsoleteOpts('cell_height', 'cellHeight');
  522. }
  523. if (typeof opts.vertical_margin !== 'undefined') {
  524. opts.verticalMargin = opts.vertical_margin;
  525. obsoleteOpts('vertical_margin', 'verticalMargin');
  526. }
  527. if (typeof opts.min_width !== 'undefined') {
  528. opts.minWidth = opts.min_width;
  529. obsoleteOpts('min_width', 'minWidth');
  530. }
  531. if (typeof opts.static_grid !== 'undefined') {
  532. opts.staticGrid = opts.static_grid;
  533. obsoleteOpts('static_grid', 'staticGrid');
  534. }
  535. if (typeof opts.is_nested !== 'undefined') {
  536. opts.isNested = opts.is_nested;
  537. obsoleteOpts('is_nested', 'isNested');
  538. }
  539. if (typeof opts.always_show_resize_handle !== 'undefined') {
  540. opts.alwaysShowResizeHandle = opts.always_show_resize_handle;
  541. obsoleteOpts('always_show_resize_handle', 'alwaysShowResizeHandle');
  542. }
  543. // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
  544. opts.itemClass = opts.itemClass || 'grid-stack-item';
  545. var isNested = this.container.closest('.' + opts.itemClass).length > 0;
  546. this.opts = _.defaults(opts || {}, {
  547. width: parseInt(this.container.attr('data-gs-width')) || 12,
  548. height: parseInt(this.container.attr('data-gs-height')) || 0,
  549. itemClass: 'grid-stack-item',
  550. placeholderClass: 'grid-stack-placeholder',
  551. placeholderText: '',
  552. handle: '.grid-stack-item-content',
  553. handleClass: null,
  554. cellHeight: 60,
  555. verticalMargin: 20,
  556. auto: true,
  557. minWidth: 768,
  558. float: false,
  559. staticGrid: false,
  560. _class: 'grid-stack-instance-' + (Math.random() * 10000).toFixed(0),
  561. animate: Boolean(this.container.attr('data-gs-animate')) || false,
  562. alwaysShowResizeHandle: opts.alwaysShowResizeHandle || false,
  563. resizable: _.defaults(opts.resizable || {}, {
  564. autoHide: !(opts.alwaysShowResizeHandle || false),
  565. handles: 'se'
  566. }),
  567. draggable: _.defaults(opts.draggable || {}, {
  568. handle: (opts.handleClass ? '.' + opts.handleClass : (opts.handle ? opts.handle : '')) ||
  569. '.grid-stack-item-content',
  570. scroll: false,
  571. appendTo: 'body'
  572. }),
  573. disableDrag: opts.disableDrag || false,
  574. disableResize: opts.disableResize || false,
  575. rtl: 'auto',
  576. removable: false,
  577. removableOptions: _.defaults(opts.removableOptions || {}, {
  578. accept: '.' + opts.itemClass
  579. }),
  580. removeTimeout: 2000,
  581. verticalMarginUnit: 'px',
  582. cellHeightUnit: 'px',
  583. disableOneColumnMode: opts.disableOneColumnMode || false,
  584. oneColumnModeClass: opts.oneColumnModeClass || 'grid-stack-one-column-mode',
  585. ddPlugin: null
  586. });
  587. if (this.opts.ddPlugin === false) {
  588. this.opts.ddPlugin = GridStackDragDropPlugin;
  589. } else if (this.opts.ddPlugin === null) {
  590. this.opts.ddPlugin = _.first(GridStackDragDropPlugin.registeredPlugins) || GridStackDragDropPlugin;
  591. }
  592. this.dd = new this.opts.ddPlugin(this);
  593. if (this.opts.rtl === 'auto') {
  594. this.opts.rtl = this.container.css('direction') === 'rtl';
  595. }
  596. if (this.opts.rtl) {
  597. this.container.addClass('grid-stack-rtl');
  598. }
  599. this.opts.isNested = isNested;
  600. isAutoCellHeight = this.opts.cellHeight === 'auto';
  601. if (isAutoCellHeight) {
  602. self.cellHeight(self.cellWidth(), true);
  603. } else {
  604. this.cellHeight(this.opts.cellHeight, true);
  605. }
  606. this.verticalMargin(this.opts.verticalMargin, true);
  607. this.container.addClass(this.opts._class);
  608. this._setStaticClass();
  609. if (isNested) {
  610. this.container.addClass('grid-stack-nested');
  611. }
  612. this._initStyles();
  613. this.grid = new GridStackEngine(this.opts.width, function(nodes, detachNode) {
  614. detachNode = typeof detachNode === 'undefined' ? true : detachNode;
  615. var maxHeight = 0;
  616. _.each(this.nodes, function(n) {
  617. maxHeight = Math.max(maxHeight, n.y + n.height);
  618. });
  619. _.each(nodes, function(n) {
  620. if (detachNode && n._id === null) {
  621. if (n.el) {
  622. n.el.remove();
  623. }
  624. } else {
  625. n.el
  626. .attr('data-gs-x', n.x)
  627. .attr('data-gs-y', n.y)
  628. .attr('data-gs-width', n.width)
  629. .attr('data-gs-height', n.height);
  630. }
  631. });
  632. self._updateStyles(maxHeight + 10);
  633. }, this.opts.float, this.opts.height);
  634. if (this.opts.auto) {
  635. var elements = [];
  636. var _this = this;
  637. this.container.children('.' + this.opts.itemClass + ':not(.' + this.opts.placeholderClass + ')')
  638. .each(function(index, el) {
  639. el = $(el);
  640. elements.push({
  641. el: el,
  642. i: parseInt(el.attr('data-gs-x')) + parseInt(el.attr('data-gs-y')) * _this.opts.width
  643. });
  644. });
  645. _.chain(elements).sortBy(function(x) { return x.i; }).each(function(i) {
  646. self._prepareElement(i.el);
  647. }).value();
  648. }
  649. this.setAnimation(this.opts.animate);
  650. this.placeholder = $(
  651. '<div class="' + this.opts.placeholderClass + ' ' + this.opts.itemClass + '">' +
  652. '<div class="placeholder-content">' + this.opts.placeholderText + '</div></div>').hide();
  653. this._updateContainerHeight();
  654. this._updateHeightsOnResize = _.throttle(function() {
  655. self.cellHeight(self.cellWidth(), false);
  656. }, 100);
  657. this.onResizeHandler = function() {
  658. if (isAutoCellHeight) {
  659. self._updateHeightsOnResize();
  660. }
  661. if (self._isOneColumnMode() && !self.opts.disableOneColumnMode) {
  662. if (oneColumnMode) {
  663. return;
  664. }
  665. self.container.addClass(self.opts.oneColumnModeClass);
  666. oneColumnMode = true;
  667. self.grid._sortNodes();
  668. _.each(self.grid.nodes, function(node) {
  669. self.container.append(node.el);
  670. if (self.opts.staticGrid) {
  671. return;
  672. }
  673. self.dd.draggable(node.el, 'disable');
  674. self.dd.resizable(node.el, 'disable');
  675. node.el.trigger('resize');
  676. });
  677. } else {
  678. if (!oneColumnMode) {
  679. return;
  680. }
  681. self.container.removeClass(self.opts.oneColumnModeClass);
  682. oneColumnMode = false;
  683. if (self.opts.staticGrid) {
  684. return;
  685. }
  686. _.each(self.grid.nodes, function(node) {
  687. if (!node.noMove && !self.opts.disableDrag) {
  688. self.dd.draggable(node.el, 'enable');
  689. }
  690. if (!node.noResize && !self.opts.disableResize) {
  691. self.dd.resizable(node.el, 'enable');
  692. }
  693. node.el.trigger('resize');
  694. });
  695. }
  696. };
  697. $(window).resize(this.onResizeHandler);
  698. this.onResizeHandler();
  699. if (!self.opts.staticGrid && typeof self.opts.removable === 'string') {
  700. var trashZone = $(self.opts.removable);
  701. if (!this.dd.isDroppable(trashZone)) {
  702. this.dd.droppable(trashZone, self.opts.removableOptions);
  703. }
  704. this.dd
  705. .on(trashZone, 'dropover', function(event, ui) {
  706. var el = $(ui.draggable);
  707. var node = el.data('_gridstack_node');
  708. if (node._grid !== self) {
  709. return;
  710. }
  711. el.data('inTrashZone', true);
  712. self._setupRemovingTimeout(el);
  713. })
  714. .on(trashZone, 'dropout', function(event, ui) {
  715. var el = $(ui.draggable);
  716. var node = el.data('_gridstack_node');
  717. if (node._grid !== self) {
  718. return;
  719. }
  720. el.data('inTrashZone', false);
  721. self._clearRemovingTimeout(el);
  722. });
  723. }
  724. if (!self.opts.staticGrid && self.opts.acceptWidgets) {
  725. var draggingElement = null;
  726. var onDrag = function(event, ui) {
  727. var el = draggingElement;
  728. var node = el.data('_gridstack_node');
  729. var pos = self.getCellFromPixel({left: event.pageX, top: event.pageY}, true);
  730. var x = Math.max(0, pos.x);
  731. var y = Math.max(0, pos.y);
  732. if (!node._added) {
  733. node._added = true;
  734. node.el = el;
  735. node.autoPosition = true;
  736. node.x = x;
  737. node.y = y;
  738. self.grid.cleanNodes();
  739. self.grid.beginUpdate(node);
  740. self.grid.addNode(node);
  741. self.container.append(self.placeholder);
  742. self.placeholder
  743. .attr('data-gs-x', node.x)
  744. .attr('data-gs-y', node.y)
  745. .attr('data-gs-width', node.width)
  746. .attr('data-gs-height', node.height)
  747. .show();
  748. node.el = self.placeholder;
  749. node._beforeDragX = node.x;
  750. node._beforeDragY = node.y;
  751. self._updateContainerHeight();
  752. }
  753. if (!self.grid.canMoveNode(node, x, y)) {
  754. return;
  755. }
  756. self.grid.moveNode(node, x, y);
  757. self._updateContainerHeight();
  758. };
  759. this.dd
  760. .droppable(self.container, {
  761. accept: function(el) {
  762. el = $(el);
  763. var node = el.data('_gridstack_node');
  764. if (node && node._grid === self) {
  765. return false;
  766. }
  767. return el.is(self.opts.acceptWidgets === true ? '.grid-stack-item' : self.opts.acceptWidgets);
  768. }
  769. })
  770. .on(self.container, 'dropover', function(event, ui) {
  771. var offset = self.container.offset();
  772. var el = $(ui.draggable);
  773. var cellWidth = self.cellWidth();
  774. var cellHeight = self.cellHeight();
  775. var origNode = el.data('_gridstack_node');
  776. var width = origNode ? origNode.width : (Math.ceil(el.outerWidth() / cellWidth));
  777. var height = origNode ? origNode.height : (Math.ceil(el.outerHeight() / cellHeight));
  778. draggingElement = el;
  779. var node = self.grid._prepareNode({width: width, height: height, _added: false, _temporary: true});
  780. el.data('_gridstack_node', node);
  781. el.data('_gridstack_node_orig', origNode);
  782. el.on('drag', onDrag);
  783. })
  784. .on(self.container, 'dropout', function(event, ui) {
  785. var el = $(ui.draggable);
  786. if (!el.data('_gridstack_node')) {
  787. return;
  788. }
  789. el.unbind('drag', onDrag);
  790. var node = el.data('_gridstack_node');
  791. node.el = null;
  792. self.grid.removeNode(node);
  793. self.placeholder.detach();
  794. self._updateContainerHeight();
  795. el.data('_gridstack_node', el.data('_gridstack_node_orig'));
  796. })
  797. .on(self.container, 'drop', function(event, ui) {
  798. self.placeholder.detach();
  799. var node = $(ui.draggable).data('_gridstack_node');
  800. node._grid = self;
  801. var el = $(ui.draggable).clone(false);
  802. el.data('_gridstack_node', node);
  803. var originalNode = $(ui.draggable).data('_gridstack_node_orig');
  804. if (typeof originalNode !== 'undefined' && typeof originalNode._grid !== 'undefined') {
  805. originalNode._grid._triggerRemoveEvent();
  806. }
  807. $(ui.helper).remove();
  808. node.el = el;
  809. self.placeholder.hide();
  810. Utils.removePositioningStyles(el);
  811. el.find('div.ui-resizable-handle').remove();
  812. el
  813. .attr('data-gs-x', node.x)
  814. .attr('data-gs-y', node.y)
  815. .attr('data-gs-width', node.width)
  816. .attr('data-gs-height', node.height)
  817. .addClass(self.opts.itemClass)
  818. .enableSelection()
  819. .removeData('draggable')
  820. .removeClass('ui-draggable ui-draggable-dragging ui-draggable-disabled')
  821. .unbind('drag', onDrag);
  822. self.container.append(el);
  823. self._prepareElementsByNode(el, node);
  824. self._updateContainerHeight();
  825. self.grid._addedNodes.push(node);
  826. self._triggerAddEvent();
  827. self._triggerChangeEvent();
  828. self.grid.endUpdate();
  829. $(ui.draggable).unbind('drag', onDrag);
  830. $(ui.draggable).removeData('_gridstack_node');
  831. $(ui.draggable).removeData('_gridstack_node_orig');
  832. });
  833. }
  834. };
  835. GridStack.prototype._triggerChangeEvent = function(forceTrigger) {
  836. var elements = this.grid.getDirtyNodes();
  837. var hasChanges = false;
  838. var eventParams = [];
  839. if (elements && elements.length) {
  840. eventParams.push(elements);
  841. hasChanges = true;
  842. }
  843. if (hasChanges || forceTrigger === true) {
  844. this.container.trigger('change', eventParams);
  845. }
  846. };
  847. GridStack.prototype._triggerAddEvent = function() {
  848. if (this.grid._addedNodes && this.grid._addedNodes.length > 0) {
  849. this.container.trigger('added', [_.map(this.grid._addedNodes, _.clone)]);
  850. this.grid._addedNodes = [];
  851. }
  852. };
  853. GridStack.prototype._triggerRemoveEvent = function() {
  854. if (this.grid._removedNodes && this.grid._removedNodes.length > 0) {
  855. this.container.trigger('removed', [_.map(this.grid._removedNodes, _.clone)]);
  856. this.grid._removedNodes = [];
  857. }
  858. };
  859. GridStack.prototype._initStyles = function() {
  860. if (this._stylesId) {
  861. Utils.removeStylesheet(this._stylesId);
  862. }
  863. this._stylesId = 'gridstack-style-' + (Math.random() * 100000).toFixed();
  864. this._styles = Utils.createStylesheet(this._stylesId);
  865. if (this._styles !== null) {
  866. this._styles._max = 0;
  867. }
  868. };
  869. GridStack.prototype._updateStyles = function(maxHeight) {
  870. if (this._styles === null || typeof this._styles === 'undefined') {
  871. return;
  872. }
  873. var prefix = '.' + this.opts._class + ' .' + this.opts.itemClass;
  874. var self = this;
  875. var getHeight;
  876. if (typeof maxHeight == 'undefined') {
  877. maxHeight = this._styles._max;
  878. }
  879. this._initStyles();
  880. this._updateContainerHeight();
  881. if (!this.opts.cellHeight) { // The rest will be handled by CSS
  882. return ;
  883. }
  884. if (this._styles._max !== 0 && maxHeight <= this._styles._max) { // Keep this._styles._max increasing
  885. return ;
  886. }
  887. if (!this.opts.verticalMargin || this.opts.cellHeightUnit === this.opts.verticalMarginUnit) {
  888. getHeight = function(nbRows, nbMargins) {
  889. return (self.opts.cellHeight * nbRows + self.opts.verticalMargin * nbMargins) +
  890. self.opts.cellHeightUnit;
  891. };
  892. } else {
  893. getHeight = function(nbRows, nbMargins) {
  894. if (!nbRows || !nbMargins) {
  895. return (self.opts.cellHeight * nbRows + self.opts.verticalMargin * nbMargins) +
  896. self.opts.cellHeightUnit;
  897. }
  898. return 'calc(' + ((self.opts.cellHeight * nbRows) + self.opts.cellHeightUnit) + ' + ' +
  899. ((self.opts.verticalMargin * nbMargins) + self.opts.verticalMarginUnit) + ')';
  900. };
  901. }
  902. if (this._styles._max === 0) {
  903. Utils.insertCSSRule(this._styles, prefix, 'min-height: ' + getHeight(1, 0) + ';', 0);
  904. }
  905. if (maxHeight > this._styles._max) {
  906. for (var i = this._styles._max; i < maxHeight; ++i) {
  907. Utils.insertCSSRule(this._styles,
  908. prefix + '[data-gs-height="' + (i + 1) + '"]',
  909. 'height: ' + getHeight(i + 1, i) + ';',
  910. i
  911. );
  912. Utils.insertCSSRule(this._styles,
  913. prefix + '[data-gs-min-height="' + (i + 1) + '"]',
  914. 'min-height: ' + getHeight(i + 1, i) + ';',
  915. i
  916. );
  917. Utils.insertCSSRule(this._styles,
  918. prefix + '[data-gs-max-height="' + (i + 1) + '"]',
  919. 'max-height: ' + getHeight(i + 1, i) + ';',
  920. i
  921. );
  922. Utils.insertCSSRule(this._styles,
  923. prefix + '[data-gs-y="' + i + '"]',
  924. 'top: ' + getHeight(i, i) + ';',
  925. i
  926. );
  927. }
  928. this._styles._max = maxHeight;
  929. }
  930. };
  931. GridStack.prototype._updateContainerHeight = function() {
  932. if (this.grid._updateCounter) {
  933. return;
  934. }
  935. var height = this.grid.getGridHeight();
  936. // check for css min height. Each row is cellHeight + verticalMargin, until last one which has no margin below
  937. var cssMinHeight = parseInt(this.container.css('min-height'));
  938. if (cssMinHeight > 0) {
  939. var minHeight = (cssMinHeight + this.opts.verticalMargin) / (this.cellHeight() + this.opts.verticalMargin);
  940. if (height < minHeight) {
  941. height = minHeight;
  942. }
  943. }
  944. this.container.attr('data-gs-current-height', height);
  945. if (!this.opts.cellHeight) {
  946. return ;
  947. }
  948. if (!this.opts.verticalMargin) {
  949. this.container.css('height', (height * (this.opts.cellHeight)) + this.opts.cellHeightUnit);
  950. } else if (this.opts.cellHeightUnit === this.opts.verticalMarginUnit) {
  951. this.container.css('height', (height * (this.opts.cellHeight + this.opts.verticalMargin) -
  952. this.opts.verticalMargin) + this.opts.cellHeightUnit);
  953. } else {
  954. this.container.css('height', 'calc(' + ((height * (this.opts.cellHeight)) + this.opts.cellHeightUnit) +
  955. ' + ' + ((height * (this.opts.verticalMargin - 1)) + this.opts.verticalMarginUnit) + ')');
  956. }
  957. };
  958. GridStack.prototype._isOneColumnMode = function() {
  959. return (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth) <=
  960. this.opts.minWidth;
  961. };
  962. GridStack.prototype._setupRemovingTimeout = function(el) {
  963. var self = this;
  964. var node = $(el).data('_gridstack_node');
  965. if (node._removeTimeout || !self.opts.removable) {
  966. return;
  967. }
  968. node._removeTimeout = setTimeout(function() {
  969. el.addClass('grid-stack-item-removing');
  970. node._isAboutToRemove = true;
  971. }, self.opts.removeTimeout);
  972. };
  973. GridStack.prototype._clearRemovingTimeout = function(el) {
  974. var node = $(el).data('_gridstack_node');
  975. if (!node._removeTimeout) {
  976. return;
  977. }
  978. clearTimeout(node._removeTimeout);
  979. node._removeTimeout = null;
  980. el.removeClass('grid-stack-item-removing');
  981. node._isAboutToRemove = false;
  982. };
  983. GridStack.prototype._prepareElementsByNode = function(el, node) {
  984. var self = this;
  985. var cellWidth;
  986. var cellHeight;
  987. var dragOrResize = function(event, ui) {
  988. var x = Math.round(ui.position.left / cellWidth);
  989. var y = Math.floor((ui.position.top + cellHeight / 2) / cellHeight);
  990. var width;
  991. var height;
  992. if (event.type != 'drag') {
  993. width = Math.round(ui.size.width / cellWidth);
  994. height = Math.round(ui.size.height / cellHeight);
  995. }
  996. if (event.type == 'drag') {
  997. var distance = ui.position.top - node._prevYPix;
  998. node._prevYPix = ui.position.top;
  999. Utils.updateScrollPosition(el[0], ui, distance);
  1000. if (el.data('inTrashZone') || x < 0 || x >= self.grid.width || y < 0 ||
  1001. (!self.grid.float && y > self.grid.getGridHeight())) {
  1002. if (!node._temporaryRemoved) {
  1003. if (self.opts.removable === true) {
  1004. self._setupRemovingTimeout(el);
  1005. }
  1006. x = node._beforeDragX;
  1007. y = node._beforeDragY;
  1008. self.placeholder.detach();
  1009. self.placeholder.hide();
  1010. self.grid.removeNode(node);
  1011. self._updateContainerHeight();
  1012. node._temporaryRemoved = true;
  1013. }
  1014. } else {
  1015. self._clearRemovingTimeout(el);
  1016. if (node._temporaryRemoved) {
  1017. self.grid.addNode(node);
  1018. self.placeholder
  1019. .attr('data-gs-x', x)
  1020. .attr('data-gs-y', y)
  1021. .attr('data-gs-width', width)
  1022. .attr('data-gs-height', height)
  1023. .show();
  1024. self.container.append(self.placeholder);
  1025. node.el = self.placeholder;
  1026. node._temporaryRemoved = false;
  1027. }
  1028. }
  1029. } else if (event.type == 'resize') {
  1030. if (x < 0) {
  1031. return;
  1032. }
  1033. }
  1034. // width and height are undefined if not resizing
  1035. var lastTriedWidth = typeof width !== 'undefined' ? width : node.lastTriedWidth;
  1036. var lastTriedHeight = typeof height !== 'undefined' ? height : node.lastTriedHeight;
  1037. if (!self.grid.canMoveNode(node, x, y, width, height) ||
  1038. (node.lastTriedX === x && node.lastTriedY === y &&
  1039. node.lastTriedWidth === lastTriedWidth && node.lastTriedHeight === lastTriedHeight)) {
  1040. return;
  1041. }
  1042. node.lastTriedX = x;
  1043. node.lastTriedY = y;
  1044. node.lastTriedWidth = width;
  1045. node.lastTriedHeight = height;
  1046. self.grid.moveNode(node, x, y, width, height);
  1047. self._updateContainerHeight();
  1048. };
  1049. var onStartMoving = function(event, ui) {
  1050. self.container.append(self.placeholder);
  1051. var o = $(this);
  1052. self.grid.cleanNodes();
  1053. self.grid.beginUpdate(node);
  1054. cellWidth = self.cellWidth();
  1055. var strictCellHeight = Math.ceil(o.outerHeight() / o.attr('data-gs-height'));
  1056. cellHeight = self.container.height() / parseInt(self.container.attr('data-gs-current-height'));
  1057. self.placeholder
  1058. .attr('data-gs-x', o.attr('data-gs-x'))
  1059. .attr('data-gs-y', o.attr('data-gs-y'))
  1060. .attr('data-gs-width', o.attr('data-gs-width'))
  1061. .attr('data-gs-height', o.attr('data-gs-height'))
  1062. .show();
  1063. node.el = self.placeholder;
  1064. node._beforeDragX = node.x;
  1065. node._beforeDragY = node.y;
  1066. node._prevYPix = ui.position.top;
  1067. self.dd.resizable(el, 'option', 'minWidth', cellWidth * (node.minWidth || 1));
  1068. self.dd.resizable(el, 'option', 'minHeight', strictCellHeight * (node.minHeight || 1));
  1069. if (event.type == 'resizestart') {
  1070. o.find('.grid-stack-item').trigger('resizestart');
  1071. }
  1072. };
  1073. var onEndMoving = function(event, ui) {
  1074. var o = $(this);
  1075. if (!o.data('_gridstack_node')) {
  1076. return;
  1077. }
  1078. var forceNotify = false;
  1079. self.placeholder.detach();
  1080. node.el = o;
  1081. self.placeholder.hide();
  1082. if (node._isAboutToRemove) {
  1083. forceNotify = true;
  1084. var gridToNotify = el.data('_gridstack_node')._grid;
  1085. gridToNotify._triggerRemoveEvent();
  1086. el.removeData('_gridstack_node');
  1087. el.remove();
  1088. } else {
  1089. self._clearRemovingTimeout(el);
  1090. if (!node._temporaryRemoved) {
  1091. Utils.removePositioningStyles(o);
  1092. o
  1093. .attr('data-gs-x', node.x)
  1094. .attr('data-gs-y', node.y)
  1095. .attr('data-gs-width', node.width)
  1096. .attr('data-gs-height', node.height);
  1097. } else {
  1098. Utils.removePositioningStyles(o);
  1099. o
  1100. .attr('data-gs-x', node._beforeDragX)
  1101. .attr('data-gs-y', node._beforeDragY)
  1102. .attr('data-gs-width', node.width)
  1103. .attr('data-gs-height', node.height);
  1104. node.x = node._beforeDragX;
  1105. node.y = node._beforeDragY;
  1106. node._temporaryRemoved = false;
  1107. self.grid.addNode(node);
  1108. }
  1109. }
  1110. self._updateContainerHeight();
  1111. self._triggerChangeEvent(forceNotify);
  1112. self.grid.endUpdate();
  1113. var nestedGrids = o.find('.grid-stack');
  1114. if (nestedGrids.length && event.type == 'resizestop') {
  1115. nestedGrids.each(function(index, el) {
  1116. $(el).data('gridstack').onResizeHandler();
  1117. });
  1118. o.find('.grid-stack-item').trigger('resizestop');
  1119. o.find('.grid-stack-item').trigger('gsresizestop');
  1120. }
  1121. if (event.type == 'resizestop') {
  1122. self.container.trigger('gsresizestop', o);
  1123. }
  1124. };
  1125. this.dd
  1126. .draggable(el, {
  1127. start: onStartMoving,
  1128. stop: onEndMoving,
  1129. drag: dragOrResize
  1130. })
  1131. .resizable(el, {
  1132. start: onStartMoving,
  1133. stop: onEndMoving,
  1134. resize: dragOrResize
  1135. });
  1136. if (node.noMove || (this._isOneColumnMode() && !self.opts.disableOneColumnMode) || this.opts.disableDrag ||
  1137. this.opts.staticGrid) {
  1138. this.dd.draggable(el, 'disable');
  1139. }
  1140. if (node.noResize || (this._isOneColumnMode() && !self.opts.disableOneColumnMode) || this.opts.disableResize ||
  1141. this.opts.staticGrid) {
  1142. this.dd.resizable(el, 'disable');
  1143. }
  1144. el.attr('data-gs-locked', node.locked ? 'yes' : null);
  1145. };
  1146. GridStack.prototype._prepareElement = function(el, triggerAddEvent) {
  1147. triggerAddEvent = typeof triggerAddEvent != 'undefined' ? triggerAddEvent : false;
  1148. var self = this;
  1149. el = $(el);
  1150. el.addClass(this.opts.itemClass);
  1151. var node = self.grid.addNode({
  1152. x: parseInt(el.attr('data-gs-x'), 10),
  1153. y: parseInt(el.attr('data-gs-y'), 10),
  1154. width: el.attr('data-gs-width'),
  1155. height: el.attr('data-gs-height'),
  1156. maxWidth: el.attr('data-gs-max-width'),
  1157. minWidth: el.attr('data-gs-min-width'),
  1158. maxHeight: el.attr('data-gs-max-height'),
  1159. minHeight: el.attr('data-gs-min-height'),
  1160. autoPosition: Utils.toBool(el.attr('data-gs-auto-position')),
  1161. noResize: Utils.toBool(el.attr('data-gs-no-resize')),
  1162. noMove: Utils.toBool(el.attr('data-gs-no-move')),
  1163. locked: Utils.toBool(el.attr('data-gs-locked')),
  1164. resizeHandles: el.attr('data-gs-resize-handles'),
  1165. el: el,
  1166. id: el.attr('data-gs-id'),
  1167. _grid: self
  1168. }, triggerAddEvent);
  1169. el.data('_gridstack_node', node);
  1170. this._prepareElementsByNode(el, node);
  1171. };
  1172. GridStack.prototype.setAnimation = function(enable) {
  1173. if (enable) {
  1174. this.container.addClass('grid-stack-animate');
  1175. } else {
  1176. this.container.removeClass('grid-stack-animate');
  1177. }
  1178. };
  1179. GridStack.prototype.addWidget = function(el, x, y, width, height, autoPosition, minWidth, maxWidth,
  1180. minHeight, maxHeight, id) {
  1181. el = $(el);
  1182. if (typeof x != 'undefined') { el.attr('data-gs-x', x); }
  1183. if (typeof y != 'undefined') { el.attr('data-gs-y', y); }
  1184. if (typeof width != 'undefined') { el.attr('data-gs-width', width); }
  1185. if (typeof height != 'undefined') { el.attr('data-gs-height', height); }
  1186. if (typeof autoPosition != 'undefined') { el.attr('data-gs-auto-position', autoPosition ? 'yes' : null); }
  1187. if (typeof minWidth != 'undefined') { el.attr('data-gs-min-width', minWidth); }
  1188. if (typeof maxWidth != 'undefined') { el.attr('data-gs-max-width', maxWidth); }
  1189. if (typeof minHeight != 'undefined') { el.attr('data-gs-min-height', minHeight); }
  1190. if (typeof maxHeight != 'undefined') { el.attr('data-gs-max-height', maxHeight); }
  1191. if (typeof id != 'undefined') { el.attr('data-gs-id', id); }
  1192. this.container.append(el);
  1193. this._prepareElement(el, true);
  1194. this._triggerAddEvent();
  1195. this._updateContainerHeight();
  1196. this._triggerChangeEvent(true);
  1197. return el;
  1198. };
  1199. GridStack.prototype.makeWidget = function(el) {
  1200. el = $(el);
  1201. this._prepareElement(el, true);
  1202. this._triggerAddEvent();
  1203. this._updateContainerHeight();
  1204. this._triggerChangeEvent(true);
  1205. return el;
  1206. };
  1207. GridStack.prototype.willItFit = function(x, y, width, height, autoPosition) {
  1208. var node = {x: x, y: y, width: width, height: height, autoPosition: autoPosition};
  1209. return this.grid.canBePlacedWithRespectToHeight(node);
  1210. };
  1211. GridStack.prototype.removeWidget = function(el, detachNode) {
  1212. detachNode = typeof detachNode === 'undefined' ? true : detachNode;
  1213. el = $(el);
  1214. var node = el.data('_gridstack_node');
  1215. // For Meteor support: https://github.com/troolee/gridstack.js/pull/272
  1216. if (!node) {
  1217. node = this.grid.getNodeDataByDOMEl(el);
  1218. }
  1219. this.grid.removeNode(node, detachNode);
  1220. el.removeData('_gridstack_node');
  1221. this._updateContainerHeight();
  1222. if (detachNode) {
  1223. el.remove();
  1224. }
  1225. this._triggerChangeEvent(true);
  1226. this._triggerRemoveEvent();
  1227. };
  1228. GridStack.prototype.removeAll = function(detachNode) {
  1229. _.each(this.grid.nodes, _.bind(function(node) {
  1230. this.removeWidget(node.el, detachNode);
  1231. }, this));
  1232. this.grid.nodes = [];
  1233. this._updateContainerHeight();
  1234. };
  1235. GridStack.prototype.destroy = function(detachGrid) {
  1236. $(window).off('resize', this.onResizeHandler);
  1237. this.disable();
  1238. if (typeof detachGrid != 'undefined' && !detachGrid) {
  1239. this.removeAll(false);
  1240. this.container.removeData('gridstack');
  1241. } else {
  1242. this.container.remove();
  1243. }
  1244. Utils.removeStylesheet(this._stylesId);
  1245. if (this.grid) {
  1246. this.grid = null;
  1247. }
  1248. };
  1249. GridStack.prototype.resizable = function(el, val) {
  1250. var self = this;
  1251. el = $(el);
  1252. el.each(function(index, el) {
  1253. el = $(el);
  1254. var node = el.data('_gridstack_node');
  1255. if (typeof node == 'undefined' || node === null) {
  1256. return;
  1257. }
  1258. node.noResize = !(val || false);
  1259. if (node.noResize || (self._isOneColumnMode() && !self.opts.disableOneColumnMode)) {
  1260. self.dd.resizable(el, 'disable');
  1261. } else {
  1262. self.dd.resizable(el, 'enable');
  1263. }
  1264. });
  1265. return this;
  1266. };
  1267. GridStack.prototype.movable = function(el, val) {
  1268. var self = this;
  1269. el = $(el);
  1270. el.each(function(index, el) {
  1271. el = $(el);
  1272. var node = el.data('_gridstack_node');
  1273. if (typeof node == 'undefined' || node === null) {
  1274. return;
  1275. }
  1276. node.noMove = !(val || false);
  1277. if (node.noMove || (self._isOneColumnMode() && !self.opts.disableOneColumnMode)) {
  1278. self.dd.draggable(el, 'disable');
  1279. el.removeClass('ui-draggable-handle');
  1280. } else {
  1281. self.dd.draggable(el, 'enable');
  1282. el.addClass('ui-draggable-handle');
  1283. }
  1284. });
  1285. return this;
  1286. };
  1287. GridStack.prototype.enableMove = function(doEnable, includeNewWidgets) {
  1288. this.movable(this.container.children('.' + this.opts.itemClass), doEnable);
  1289. if (includeNewWidgets) {
  1290. this.opts.disableDrag = !doEnable;
  1291. }
  1292. };
  1293. GridStack.prototype.enableResize = function(doEnable, includeNewWidgets) {
  1294. this.resizable(this.container.children('.' + this.opts.itemClass), doEnable);
  1295. if (includeNewWidgets) {
  1296. this.opts.disableResize = !doEnable;
  1297. }
  1298. };
  1299. GridStack.prototype.disable = function() {
  1300. this.movable(this.container.children('.' + this.opts.itemClass), false);
  1301. this.resizable(this.container.children('.' + this.opts.itemClass), false);
  1302. this.container.trigger('disable');
  1303. };
  1304. GridStack.prototype.enable = function() {
  1305. this.movable(this.container.children('.' + this.opts.itemClass), true);
  1306. this.resizable(this.container.children('.' + this.opts.itemClass), true);
  1307. this.container.trigger('enable');
  1308. };
  1309. GridStack.prototype.locked = function(el, val) {
  1310. el = $(el);
  1311. el.each(function(index, el) {
  1312. el = $(el);
  1313. var node = el.data('_gridstack_node');
  1314. if (typeof node == 'undefined' || node === null) {
  1315. return;
  1316. }
  1317. node.locked = (val || false);
  1318. el.attr('data-gs-locked', node.locked ? 'yes' : null);
  1319. });
  1320. return this;
  1321. };
  1322. GridStack.prototype.maxHeight = function(el, val) {
  1323. el = $(el);
  1324. el.each(function(index, el) {
  1325. el = $(el);
  1326. var node = el.data('_gridstack_node');
  1327. if (typeof node === 'undefined' || node === null) {
  1328. return;
  1329. }
  1330. if (!isNaN(val)) {
  1331. node.maxHeight = (val || false);
  1332. el.attr('data-gs-max-height', val);
  1333. }
  1334. });
  1335. return this;
  1336. };
  1337. GridStack.prototype.minHeight = function(el, val) {
  1338. el = $(el);
  1339. el.each(function(index, el) {
  1340. el = $(el);
  1341. var node = el.data('_gridstack_node');
  1342. if (typeof node === 'undefined' || node === null) {
  1343. return;
  1344. }
  1345. if (!isNaN(val)) {
  1346. node.minHeight = (val || false);
  1347. el.attr('data-gs-min-height', val);
  1348. }
  1349. });
  1350. return this;
  1351. };
  1352. GridStack.prototype.maxWidth = function(el, val) {
  1353. el = $(el);
  1354. el.each(function(index, el) {
  1355. el = $(el);
  1356. var node = el.data('_gridstack_node');
  1357. if (typeof node === 'undefined' || node === null) {
  1358. return;
  1359. }
  1360. if (!isNaN(val)) {
  1361. node.maxWidth = (val || false);
  1362. el.attr('data-gs-max-width', val);
  1363. }
  1364. });
  1365. return this;
  1366. };
  1367. GridStack.prototype.minWidth = function(el, val) {
  1368. el = $(el);
  1369. el.each(function(index, el) {
  1370. el = $(el);
  1371. var node = el.data('_gridstack_node');
  1372. if (typeof node === 'undefined' || node === null) {
  1373. return;
  1374. }
  1375. if (!isNaN(val)) {
  1376. node.minWidth = (val || false);
  1377. el.attr('data-gs-min-width', val);
  1378. }
  1379. });
  1380. return this;
  1381. };
  1382. GridStack.prototype._updateElement = function(el, callback) {
  1383. el = $(el).first();
  1384. var node = el.data('_gridstack_node');
  1385. if (typeof node == 'undefined' || node === null) {
  1386. return;
  1387. }
  1388. var self = this;
  1389. self.grid.cleanNodes();
  1390. self.grid.beginUpdate(node);
  1391. callback.call(this, el, node);
  1392. self._updateContainerHeight();
  1393. self._triggerChangeEvent();
  1394. self.grid.endUpdate();
  1395. };
  1396. GridStack.prototype.resize = function(el, width, height) {
  1397. this._updateElement(el, function(el, node) {
  1398. width = (width !== null && typeof width != 'undefined') ? width : node.width;
  1399. height = (height !== null && typeof height != 'undefined') ? height : node.height;
  1400. this.grid.moveNode(node, node.x, node.y, width, height);
  1401. });
  1402. };
  1403. GridStack.prototype.move = function(el, x, y) {
  1404. this._updateElement(el, function(el, node) {
  1405. x = (x !== null && typeof x != 'undefined') ? x : node.x;
  1406. y = (y !== null && typeof y != 'undefined') ? y : node.y;
  1407. this.grid.moveNode(node, x, y, node.width, node.height);
  1408. });
  1409. };
  1410. GridStack.prototype.update = function(el, x, y, width, height) {
  1411. this._updateElement(el, function(el, node) {
  1412. x = (x !== null && typeof x != 'undefined') ? x : node.x;
  1413. y = (y !== null && typeof y != 'undefined') ? y : node.y;
  1414. width = (width !== null && typeof width != 'undefined') ? width : node.width;
  1415. height = (height !== null && typeof height != 'undefined') ? height : node.height;
  1416. this.grid.moveNode(node, x, y, width, height);
  1417. });
  1418. };
  1419. GridStack.prototype.verticalMargin = function(val, noUpdate) {
  1420. if (typeof val == 'undefined') {
  1421. return this.opts.verticalMargin;
  1422. }
  1423. var heightData = Utils.parseHeight(val);
  1424. if (this.opts.verticalMarginUnit === heightData.unit && this.opts.height === heightData.height) {
  1425. return ;
  1426. }
  1427. this.opts.verticalMarginUnit = heightData.unit;
  1428. this.opts.verticalMargin = heightData.height;
  1429. if (!noUpdate) {
  1430. this._updateStyles();
  1431. }
  1432. };
  1433. GridStack.prototype.cellHeight = function(val, noUpdate) {
  1434. if (typeof val == 'undefined') {
  1435. if (this.opts.cellHeight) {
  1436. return this.opts.cellHeight;
  1437. }
  1438. var o = this.container.children('.' + this.opts.itemClass).first();
  1439. return Math.ceil(o.outerHeight() / o.attr('data-gs-height'));
  1440. }
  1441. var heightData = Utils.parseHeight(val);
  1442. if (this.opts.cellHeightUnit === heightData.unit && this.opts.cellHeight === heightData.height) {
  1443. return ;
  1444. }
  1445. this.opts.cellHeightUnit = heightData.unit;
  1446. this.opts.cellHeight = heightData.height;
  1447. if (!noUpdate) {
  1448. this._updateStyles();
  1449. }
  1450. };
  1451. GridStack.prototype.cellWidth = function() {
  1452. return Math.round(this.container.outerWidth() / this.opts.width);
  1453. };
  1454. GridStack.prototype.getCellFromPixel = function(position, useOffset) {
  1455. var containerPos = (typeof useOffset != 'undefined' && useOffset) ?
  1456. this.container.offset() : this.container.position();
  1457. var relativeLeft = position.left - containerPos.left;
  1458. var relativeTop = position.top - containerPos.top;
  1459. var columnWidth = Math.floor(this.container.width() / this.opts.width);
  1460. var rowHeight = Math.floor(this.container.height() / parseInt(this.container.attr('data-gs-current-height')));
  1461. return {x: Math.floor(relativeLeft / columnWidth), y: Math.floor(relativeTop / rowHeight)};
  1462. };
  1463. GridStack.prototype.batchUpdate = function() {
  1464. this.grid.batchUpdate();
  1465. };
  1466. GridStack.prototype.commit = function() {
  1467. this.grid.commit();
  1468. this._updateContainerHeight();
  1469. };
  1470. GridStack.prototype.isAreaEmpty = function(x, y, width, height) {
  1471. return this.grid.isAreaEmpty(x, y, width, height);
  1472. };
  1473. GridStack.prototype.setStatic = function(staticValue) {
  1474. this.opts.staticGrid = (staticValue === true);
  1475. this.enableMove(!staticValue);
  1476. this.enableResize(!staticValue);
  1477. this._setStaticClass();
  1478. };
  1479. GridStack.prototype._setStaticClass = function() {
  1480. var staticClassName = 'grid-stack-static';
  1481. if (this.opts.staticGrid === true) {
  1482. this.container.addClass(staticClassName);
  1483. } else {
  1484. this.container.removeClass(staticClassName);
  1485. }
  1486. };
  1487. GridStack.prototype._updateNodeWidths = function(oldWidth, newWidth) {
  1488. this.grid._sortNodes();
  1489. this.grid.batchUpdate();
  1490. var node = {};
  1491. for (var i = 0; i < this.grid.nodes.length; i++) {
  1492. node = this.grid.nodes[i];
  1493. this.update(node.el, Math.round(node.x * newWidth / oldWidth), undefined,
  1494. Math.round(node.width * newWidth / oldWidth), undefined);
  1495. }
  1496. this.grid.commit();
  1497. };
  1498. GridStack.prototype.setGridWidth = function(gridWidth,doNotPropagate) {
  1499. this.container.removeClass('grid-stack-' + this.opts.width);
  1500. if (doNotPropagate !== true) {
  1501. this._updateNodeWidths(this.opts.width, gridWidth);
  1502. }
  1503. this.opts.width = gridWidth;
  1504. this.grid.width = gridWidth;
  1505. this.container.addClass('grid-stack-' + gridWidth);
  1506. };
  1507. // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
  1508. GridStackEngine.prototype.batch_update = obsolete(GridStackEngine.prototype.batchUpdate);
  1509. GridStackEngine.prototype._fix_collisions = obsolete(GridStackEngine.prototype._fixCollisions,
  1510. '_fix_collisions', '_fixCollisions');
  1511. GridStackEngine.prototype.is_area_empty = obsolete(GridStackEngine.prototype.isAreaEmpty,
  1512. 'is_area_empty', 'isAreaEmpty');
  1513. GridStackEngine.prototype._sort_nodes = obsolete(GridStackEngine.prototype._sortNodes,
  1514. '_sort_nodes', '_sortNodes');
  1515. GridStackEngine.prototype._pack_nodes = obsolete(GridStackEngine.prototype._packNodes,
  1516. '_pack_nodes', '_packNodes');
  1517. GridStackEngine.prototype._prepare_node = obsolete(GridStackEngine.prototype._prepareNode,
  1518. '_prepare_node', '_prepareNode');
  1519. GridStackEngine.prototype.clean_nodes = obsolete(GridStackEngine.prototype.cleanNodes,
  1520. 'clean_nodes', 'cleanNodes');
  1521. GridStackEngine.prototype.get_dirty_nodes = obsolete(GridStackEngine.prototype.getDirtyNodes,
  1522. 'get_dirty_nodes', 'getDirtyNodes');
  1523. GridStackEngine.prototype.add_node = obsolete(GridStackEngine.prototype.addNode,
  1524. 'add_node', 'addNode, ');
  1525. GridStackEngine.prototype.remove_node = obsolete(GridStackEngine.prototype.removeNode,
  1526. 'remove_node', 'removeNode');
  1527. GridStackEngine.prototype.can_move_node = obsolete(GridStackEngine.prototype.canMoveNode,
  1528. 'can_move_node', 'canMoveNode');
  1529. GridStackEngine.prototype.move_node = obsolete(GridStackEngine.prototype.moveNode,
  1530. 'move_node', 'moveNode');
  1531. GridStackEngine.prototype.get_grid_height = obsolete(GridStackEngine.prototype.getGridHeight,
  1532. 'get_grid_height', 'getGridHeight');
  1533. GridStackEngine.prototype.begin_update = obsolete(GridStackEngine.prototype.beginUpdate,
  1534. 'begin_update', 'beginUpdate');
  1535. GridStackEngine.prototype.end_update = obsolete(GridStackEngine.prototype.endUpdate,
  1536. 'end_update', 'endUpdate');
  1537. GridStackEngine.prototype.can_be_placed_with_respect_to_height =
  1538. obsolete(GridStackEngine.prototype.canBePlacedWithRespectToHeight,
  1539. 'can_be_placed_with_respect_to_height', 'canBePlacedWithRespectToHeight');
  1540. GridStack.prototype._trigger_change_event = obsolete(GridStack.prototype._triggerChangeEvent,
  1541. '_trigger_change_event', '_triggerChangeEvent');
  1542. GridStack.prototype._init_styles = obsolete(GridStack.prototype._initStyles,
  1543. '_init_styles', '_initStyles');
  1544. GridStack.prototype._update_styles = obsolete(GridStack.prototype._updateStyles,
  1545. '_update_styles', '_updateStyles');
  1546. GridStack.prototype._update_container_height = obsolete(GridStack.prototype._updateContainerHeight,
  1547. '_update_container_height', '_updateContainerHeight');
  1548. GridStack.prototype._is_one_column_mode = obsolete(GridStack.prototype._isOneColumnMode,
  1549. '_is_one_column_mode','_isOneColumnMode');
  1550. GridStack.prototype._prepare_element = obsolete(GridStack.prototype._prepareElement,
  1551. '_prepare_element', '_prepareElement');
  1552. GridStack.prototype.set_animation = obsolete(GridStack.prototype.setAnimation,
  1553. 'set_animation', 'setAnimation');
  1554. GridStack.prototype.add_widget = obsolete(GridStack.prototype.addWidget,
  1555. 'add_widget', 'addWidget');
  1556. GridStack.prototype.make_widget = obsolete(GridStack.prototype.makeWidget,
  1557. 'make_widget', 'makeWidget');
  1558. GridStack.prototype.will_it_fit = obsolete(GridStack.prototype.willItFit,
  1559. 'will_it_fit', 'willItFit');
  1560. GridStack.prototype.remove_widget = obsolete(GridStack.prototype.removeWidget,
  1561. 'remove_widget', 'removeWidget');
  1562. GridStack.prototype.remove_all = obsolete(GridStack.prototype.removeAll,
  1563. 'remove_all', 'removeAll');
  1564. GridStack.prototype.min_height = obsolete(GridStack.prototype.minHeight,
  1565. 'min_height', 'minHeight');
  1566. GridStack.prototype.min_width = obsolete(GridStack.prototype.minWidth,
  1567. 'min_width', 'minWidth');
  1568. GridStack.prototype._update_element = obsolete(GridStack.prototype._updateElement,
  1569. '_update_element', '_updateElement');
  1570. GridStack.prototype.cell_height = obsolete(GridStack.prototype.cellHeight,
  1571. 'cell_height', 'cellHeight');
  1572. GridStack.prototype.cell_width = obsolete(GridStack.prototype.cellWidth,
  1573. 'cell_width', 'cellWidth');
  1574. GridStack.prototype.get_cell_from_pixel = obsolete(GridStack.prototype.getCellFromPixel,
  1575. 'get_cell_from_pixel', 'getCellFromPixel');
  1576. GridStack.prototype.batch_update = obsolete(GridStack.prototype.batchUpdate,
  1577. 'batch_update', 'batchUpdate');
  1578. GridStack.prototype.is_area_empty = obsolete(GridStack.prototype.isAreaEmpty,
  1579. 'is_area_empty', 'isAreaEmpty');
  1580. GridStack.prototype.set_static = obsolete(GridStack.prototype.setStatic,
  1581. 'set_static', 'setStatic');
  1582. GridStack.prototype._set_static_class = obsolete(GridStack.prototype._setStaticClass,
  1583. '_set_static_class', '_setStaticClass');
  1584. // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
  1585. scope.GridStackUI = GridStack;
  1586. scope.GridStackUI.Utils = Utils;
  1587. scope.GridStackUI.Engine = GridStackEngine;
  1588. scope.GridStackUI.GridStackDragDropPlugin = GridStackDragDropPlugin;
  1589. $.fn.gridstack = function(opts) {
  1590. return this.each(function() {
  1591. var o = $(this);
  1592. if (!o.data('gridstack')) {
  1593. o
  1594. .data('gridstack', new GridStack(this, opts));
  1595. }
  1596. });
  1597. };
  1598. return scope.GridStackUI;
  1599. });