jquery.resizableColumns.min.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /**
  2. * jquery-resizable-columns - Resizable table columns for jQuery
  3. * @date Sat May 16 2015 19:03:57 GMT+0100 (BST)
  4. * @version v0.1.0
  5. * @link http://dobtco.github.io/jquery-resizable-columns/
  6. * @license MIT
  7. */
  8. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  9. 'use strict';
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  11. var _class = require('./class');
  12. var _class2 = _interopRequireDefault(_class);
  13. var _constants = require('./constants');
  14. $.fn.resizableColumns = function (optionsOrMethod) {
  15. for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  16. args[_key - 1] = arguments[_key];
  17. }
  18. return this.each(function () {
  19. var $table = $(this);
  20. var api = $table.data(_constants.DATA_API);
  21. if (!api) {
  22. api = new _class2['default']($table, optionsOrMethod);
  23. $table.data(_constants.DATA_API, api);
  24. } else if (typeof optionsOrMethod === 'string') {
  25. return api[optionsOrMethod].apply(api, args);
  26. }
  27. });
  28. };
  29. $.resizableColumns = _class2['default'];
  30. },{"./class":2,"./constants":3}],2:[function(require,module,exports){
  31. 'use strict';
  32. Object.defineProperty(exports, '__esModule', {
  33. value: true
  34. });
  35. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  36. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  37. var _constants = require('./constants');
  38. /**
  39. Takes a <table /> element and makes it's columns resizable across both
  40. mobile and desktop clients.
  41. @class ResizableColumns
  42. @param $table {jQuery} jQuery-wrapped <table> element to make resizable
  43. @param options {Object} Configuration object
  44. **/
  45. var ResizableColumns = (function () {
  46. function ResizableColumns($table, options) {
  47. _classCallCheck(this, ResizableColumns);
  48. this.ns = '.rc' + this.count++;
  49. this.options = $.extend({}, ResizableColumns.defaults, options);
  50. this.$window = $(window);
  51. this.$ownerDocument = $($table[0].ownerDocument);
  52. this.$table = $table;
  53. this.refreshHeaders();
  54. this.restoreColumnWidths();
  55. this.syncHandleWidths();
  56. this.bindEvents(this.$window, 'resize', this.syncHandleWidths.bind(this));
  57. if (this.options.start) {
  58. this.bindEvents(this.$table, _constants.EVENT_RESIZE_START, this.options.start);
  59. }
  60. if (this.options.resize) {
  61. this.bindEvents(this.$table, _constants.EVENT_RESIZE, this.options.resize);
  62. }
  63. if (this.options.stop) {
  64. this.bindEvents(this.$table, _constants.EVENT_RESIZE_STOP, this.options.stop);
  65. }
  66. }
  67. _createClass(ResizableColumns, [{
  68. key: 'refreshHeaders',
  69. /**
  70. Refreshes the headers associated with this instances <table/> element and
  71. generates handles for them. Also assigns percentage widths.
  72. @method refreshHeaders
  73. **/
  74. value: function refreshHeaders() {
  75. // Allow the selector to be both a regular selctor string as well as
  76. // a dynamic callback
  77. var selector = this.options.selector;
  78. if (typeof selector === 'function') {
  79. selector = selector.call(this, this.$table);
  80. }
  81. // Select all table headers
  82. this.$tableHeaders = this.$table.find(selector);
  83. // Assign percentage widths first, then create drag handles
  84. this.assignPercentageWidths();
  85. this.createHandles();
  86. }
  87. }, {
  88. key: 'createHandles',
  89. /**
  90. Creates dummy handle elements for all table header columns
  91. @method createHandles
  92. **/
  93. value: function createHandles() {
  94. var _this = this;
  95. var ref = this.$handleContainer;
  96. if (ref != null) {
  97. ref.remove();
  98. }
  99. this.$handleContainer = $('<div class=\'' + _constants.CLASS_HANDLE_CONTAINER + '\' />');
  100. this.$table.before(this.$handleContainer);
  101. this.$tableHeaders.each(function (i, el) {
  102. var $current = _this.$tableHeaders.eq(i);
  103. var $next = _this.$tableHeaders.eq(i + 1);
  104. if ($next.length === 0 || $current.is(_constants.SELECTOR_UNRESIZABLE) || $next.is(_constants.SELECTOR_UNRESIZABLE)) {
  105. return;
  106. }
  107. var $handle = $('<div class=\'' + _constants.CLASS_HANDLE + '\' />').data(_constants.DATA_TH, $(el)).appendTo(_this.$handleContainer);
  108. });
  109. this.bindEvents(this.$handleContainer, ['mousedown', 'touchstart'], '.' + _constants.CLASS_HANDLE, this.onPointerDown.bind(this));
  110. }
  111. }, {
  112. key: 'assignPercentageWidths',
  113. /**
  114. Assigns a percentage width to all columns based on their current pixel width(s)
  115. @method assignPercentageWidths
  116. **/
  117. value: function assignPercentageWidths() {
  118. var _this2 = this;
  119. this.$tableHeaders.each(function (_, el) {
  120. var $el = $(el);
  121. _this2.setWidth($el[0], $el.outerWidth() / _this2.$table.width() * 100);
  122. });
  123. }
  124. }, {
  125. key: 'syncHandleWidths',
  126. /**
  127. @method syncHandleWidths
  128. **/
  129. value: function syncHandleWidths() {
  130. var _this3 = this;
  131. var $container = this.$handleContainer;
  132. $container.width(this.$table.width());
  133. $container.find('.' + _constants.CLASS_HANDLE).each(function (_, el) {
  134. var $el = $(el);
  135. var height = _this3.options.resizeFromBody ? _this3.$table.height() : _this3.$table.find('thead').height();
  136. var left = $el.data(_constants.DATA_TH).outerWidth() + ($el.data(_constants.DATA_TH).offset().left - _this3.$handleContainer.offset().left);
  137. $el.css({ left: left, height: height });
  138. });
  139. }
  140. }, {
  141. key: 'saveColumnWidths',
  142. /**
  143. Persists the column widths in localStorage
  144. @method saveColumnWidths
  145. **/
  146. value: function saveColumnWidths() {
  147. var _this4 = this;
  148. this.$tableHeaders.each(function (_, el) {
  149. var $el = $(el);
  150. if (_this4.options.store && !$el.is(_constants.SELECTOR_UNRESIZABLE)) {
  151. _this4.options.store.set(_this4.generateColumnId($el), _this4.parseWidth(el));
  152. }
  153. });
  154. }
  155. }, {
  156. key: 'restoreColumnWidths',
  157. /**
  158. Retrieves and sets the column widths from localStorage
  159. @method restoreColumnWidths
  160. **/
  161. value: function restoreColumnWidths() {
  162. var _this5 = this;
  163. this.$tableHeaders.each(function (_, el) {
  164. var $el = $(el);
  165. if (_this5.options.store && !$el.is(_constants.SELECTOR_UNRESIZABLE)) {
  166. var width = _this5.options.store.get(_this5.generateColumnId($el));
  167. if (width != null) {
  168. _this5.setWidth(el, width);
  169. }
  170. }
  171. });
  172. }
  173. }, {
  174. key: 'onPointerDown',
  175. /**
  176. Pointer/mouse down handler
  177. @method onPointerDown
  178. @param event {Object} Event object associated with the interaction
  179. **/
  180. value: function onPointerDown(event) {
  181. // Only applies to left-click dragging
  182. if (event.which !== 1) {
  183. return;
  184. }
  185. // If a previous operation is defined, we missed the last mouseup.
  186. // Probably gobbled up by user mousing out the window then releasing.
  187. // We'll simulate a pointerup here prior to it
  188. if (this.operation) {
  189. this.onPointerUp(event);
  190. }
  191. // Ignore non-resizable columns
  192. var $currentGrip = $(event.currentTarget);
  193. if ($currentGrip.is(_constants.SELECTOR_UNRESIZABLE)) {
  194. return;
  195. }
  196. var gripIndex = $currentGrip.index();
  197. var $leftColumn = this.$tableHeaders.eq(gripIndex).not(_constants.SELECTOR_UNRESIZABLE);
  198. var $rightColumn = this.$tableHeaders.eq(gripIndex + 1).not(_constants.SELECTOR_UNRESIZABLE);
  199. var leftWidth = this.parseWidth($leftColumn[0]);
  200. var rightWidth = this.parseWidth($rightColumn[0]);
  201. this.operation = {
  202. $leftColumn: $leftColumn, $rightColumn: $rightColumn, $currentGrip: $currentGrip,
  203. startX: this.getPointerX(event),
  204. widths: {
  205. left: leftWidth,
  206. right: rightWidth
  207. },
  208. newWidths: {
  209. left: leftWidth,
  210. right: rightWidth
  211. }
  212. };
  213. this.bindEvents(this.$ownerDocument, ['mousemove', 'touchmove'], this.onPointerMove.bind(this));
  214. this.bindEvents(this.$ownerDocument, ['mouseup', 'touchend'], this.onPointerUp.bind(this));
  215. this.$handleContainer.add(this.$table).addClass(_constants.CLASS_TABLE_RESIZING);
  216. $leftColumn.add($rightColumn).add($currentGrip).addClass(_constants.CLASS_COLUMN_RESIZING);
  217. this.triggerEvent(_constants.EVENT_RESIZE_START, [$leftColumn, $rightColumn, leftWidth, rightWidth], event);
  218. event.preventDefault();
  219. }
  220. }, {
  221. key: 'onPointerMove',
  222. /**
  223. Pointer/mouse movement handler
  224. @method onPointerMove
  225. @param event {Object} Event object associated with the interaction
  226. **/
  227. value: function onPointerMove(event) {
  228. var op = this.operation;
  229. if (!this.operation) {
  230. return;
  231. }
  232. // Determine the delta change between start and new mouse position, as a percentage of the table width
  233. var difference = (this.getPointerX(event) - op.startX) / this.$table.width() * 100;
  234. if (difference === 0) {
  235. return;
  236. }
  237. var leftColumn = op.$leftColumn[0];
  238. var rightColumn = op.$rightColumn[0];
  239. var widthLeft = undefined,
  240. widthRight = undefined;
  241. if (difference > 0) {
  242. widthLeft = this.constrainWidth(op.widths.left + (op.widths.right - op.newWidths.right));
  243. widthRight = this.constrainWidth(op.widths.right - difference);
  244. } else if (difference < 0) {
  245. widthLeft = this.constrainWidth(op.widths.left + difference);
  246. widthRight = this.constrainWidth(op.widths.right + (op.widths.left - op.newWidths.left));
  247. }
  248. if (leftColumn) {
  249. this.setWidth(leftColumn, widthLeft);
  250. }
  251. if (rightColumn) {
  252. this.setWidth(rightColumn, widthRight);
  253. }
  254. op.newWidths.left = widthLeft;
  255. op.newWidths.right = widthRight;
  256. return this.triggerEvent(_constants.EVENT_RESIZE, [op.$leftColumn, op.$rightColumn, widthLeft, widthRight], event);
  257. }
  258. }, {
  259. key: 'onPointerUp',
  260. /**
  261. Pointer/mouse release handler
  262. @method onPointerUp
  263. @param event {Object} Event object associated with the interaction
  264. **/
  265. value: function onPointerUp(event) {
  266. var op = this.operation;
  267. if (!this.operation) {
  268. return;
  269. }
  270. this.unbindEvents(this.$ownerDocument, ['mouseup', 'touchend', 'mousemove', 'touchmove']);
  271. this.$handleContainer.add(this.$table).removeClass(_constants.CLASS_TABLE_RESIZING);
  272. op.$leftColumn.add(op.$rightColumn).add(op.$currentGrip).removeClass(_constants.CLASS_COLUMN_RESIZING);
  273. this.syncHandleWidths();
  274. this.saveColumnWidths();
  275. this.operation = null;
  276. return this.triggerEvent(_constants.EVENT_RESIZE_STOP, [op.$leftColumn, op.$rightColumn, op.newWidths.left, op.newWidths.right], event);
  277. }
  278. }, {
  279. key: 'destroy',
  280. /**
  281. Removes all event listeners, data, and added DOM elements. Takes
  282. the <table/> element back to how it was, and returns it
  283. @method destroy
  284. @return {jQuery} Original jQuery-wrapped <table> element
  285. **/
  286. value: function destroy() {
  287. var $table = this.$table;
  288. var $handles = this.$handleContainer.find('.' + _constants.CLASS_HANDLE);
  289. this.unbindEvents(this.$window.add(this.$ownerDocument).add(this.$table).add($handles));
  290. $handles.removeData(_constants.DATA_TH);
  291. $table.removeData(_constants.DATA_API);
  292. this.$handleContainer.remove();
  293. this.$handleContainer = null;
  294. this.$tableHeaders = null;
  295. this.$table = null;
  296. return $table;
  297. }
  298. }, {
  299. key: 'bindEvents',
  300. /**
  301. Binds given events for this instance to the given target DOMElement
  302. @private
  303. @method bindEvents
  304. @param target {jQuery} jQuery-wrapped DOMElement to bind events to
  305. @param events {String|Array} Event name (or array of) to bind
  306. @param selectorOrCallback {String|Function} Selector string or callback
  307. @param [callback] {Function} Callback method
  308. **/
  309. value: function bindEvents($target, events, selectorOrCallback, callback) {
  310. if (typeof events === 'string') {
  311. events = events + this.ns;
  312. } else {
  313. events = events.join(this.ns + ' ') + this.ns;
  314. }
  315. if (arguments.length > 3) {
  316. $target.on(events, selectorOrCallback, callback);
  317. } else {
  318. $target.on(events, selectorOrCallback);
  319. }
  320. }
  321. }, {
  322. key: 'unbindEvents',
  323. /**
  324. Unbinds events specific to this instance from the given target DOMElement
  325. @private
  326. @method unbindEvents
  327. @param target {jQuery} jQuery-wrapped DOMElement to unbind events from
  328. @param events {String|Array} Event name (or array of) to unbind
  329. **/
  330. value: function unbindEvents($target, events) {
  331. if (typeof events === 'string') {
  332. events = events + this.ns;
  333. } else if (events != null) {
  334. events = events.join(this.ns + ' ') + this.ns;
  335. } else {
  336. events = this.ns;
  337. }
  338. $target.off(events);
  339. }
  340. }, {
  341. key: 'triggerEvent',
  342. /**
  343. Triggers an event on the <table/> element for a given type with given
  344. arguments, also setting and allowing access to the originalEvent if
  345. given. Returns the result of the triggered event.
  346. @private
  347. @method triggerEvent
  348. @param type {String} Event name
  349. @param args {Array} Array of arguments to pass through
  350. @param [originalEvent] If given, is set on the event object
  351. @return {Mixed} Result of the event trigger action
  352. **/
  353. value: function triggerEvent(type, args, originalEvent) {
  354. var event = $.Event(type);
  355. if (event.originalEvent) {
  356. event.originalEvent = $.extend({}, originalEvent);
  357. }
  358. return this.$table.trigger(event, [this].concat(args || []));
  359. }
  360. }, {
  361. key: 'generateColumnId',
  362. /**
  363. Calculates a unique column ID for a given column DOMElement
  364. @private
  365. @method generateColumnId
  366. @param $el {jQuery} jQuery-wrapped column element
  367. @return {String} Column ID
  368. **/
  369. value: function generateColumnId($el) {
  370. return this.$table.data(_constants.DATA_COLUMNS_ID) + '-' + $el.data(_constants.DATA_COLUMN_ID);
  371. }
  372. }, {
  373. key: 'parseWidth',
  374. /**
  375. Parses a given DOMElement's width into a float
  376. @private
  377. @method parseWidth
  378. @param element {DOMElement} Element to get width of
  379. @return {Number} Element's width as a float
  380. **/
  381. value: function parseWidth(element) {
  382. return element ? parseFloat(element.style.width.replace('%', '')) : 0;
  383. }
  384. }, {
  385. key: 'setWidth',
  386. /**
  387. Sets the percentage width of a given DOMElement
  388. @private
  389. @method setWidth
  390. @param element {DOMElement} Element to set width on
  391. @param width {Number} Width, as a percentage, to set
  392. **/
  393. value: function setWidth(element, width) {
  394. width = width.toFixed(2);
  395. width = width > 0 ? width : 0;
  396. element.style.width = width + '%';
  397. }
  398. }, {
  399. key: 'constrainWidth',
  400. /**
  401. Constrains a given width to the minimum and maximum ranges defined in
  402. the `minWidth` and `maxWidth` configuration options, respectively.
  403. @private
  404. @method constrainWidth
  405. @param width {Number} Width to constrain
  406. @return {Number} Constrained width
  407. **/
  408. value: function constrainWidth(width) {
  409. if (this.options.minWidth != undefined) {
  410. width = Math.max(this.options.minWidth, width);
  411. }
  412. if (this.options.maxWidth != undefined) {
  413. width = Math.min(this.options.maxWidth, width);
  414. }
  415. return width;
  416. }
  417. }, {
  418. key: 'getPointerX',
  419. /**
  420. Given a particular Event object, retrieves the current pointer offset along
  421. the horizontal direction. Accounts for both regular mouse clicks as well as
  422. pointer-like systems (mobiles, tablets etc.)
  423. @private
  424. @method getPointerX
  425. @param event {Object} Event object associated with the interaction
  426. @return {Number} Horizontal pointer offset
  427. **/
  428. value: function getPointerX(event) {
  429. if (event.type.indexOf('touch') === 0) {
  430. return (event.originalEvent.touches[0] || event.originalEvent.changedTouches[0]).pageX;
  431. }
  432. return event.pageX;
  433. }
  434. }]);
  435. return ResizableColumns;
  436. })();
  437. exports['default'] = ResizableColumns;
  438. ResizableColumns.defaults = {
  439. selector: function selector($table) {
  440. if ($table.find('thead').length) {
  441. return _constants.SELECTOR_TH;
  442. }
  443. return _constants.SELECTOR_TD;
  444. },
  445. store: window.store,
  446. syncHandlers: true,
  447. resizeFromBody: true,
  448. maxWidth: null,
  449. minWidth: 0.01
  450. };
  451. ResizableColumns.count = 0;
  452. module.exports = exports['default'];
  453. },{"./constants":3}],3:[function(require,module,exports){
  454. 'use strict';
  455. Object.defineProperty(exports, '__esModule', {
  456. value: true
  457. });
  458. var DATA_API = 'resizableColumns';
  459. exports.DATA_API = DATA_API;
  460. var DATA_COLUMNS_ID = 'resizable-columns-id';
  461. exports.DATA_COLUMNS_ID = DATA_COLUMNS_ID;
  462. var DATA_COLUMN_ID = 'resizable-column-id';
  463. exports.DATA_COLUMN_ID = DATA_COLUMN_ID;
  464. var DATA_TH = 'th';
  465. exports.DATA_TH = DATA_TH;
  466. var CLASS_TABLE_RESIZING = 'rc-table-resizing';
  467. exports.CLASS_TABLE_RESIZING = CLASS_TABLE_RESIZING;
  468. var CLASS_COLUMN_RESIZING = 'rc-column-resizing';
  469. exports.CLASS_COLUMN_RESIZING = CLASS_COLUMN_RESIZING;
  470. var CLASS_HANDLE = 'rc-handle';
  471. exports.CLASS_HANDLE = CLASS_HANDLE;
  472. var CLASS_HANDLE_CONTAINER = 'rc-handle-container';
  473. exports.CLASS_HANDLE_CONTAINER = CLASS_HANDLE_CONTAINER;
  474. var EVENT_RESIZE_START = 'column:resize:start';
  475. exports.EVENT_RESIZE_START = EVENT_RESIZE_START;
  476. var EVENT_RESIZE = 'column:resize';
  477. exports.EVENT_RESIZE = EVENT_RESIZE;
  478. var EVENT_RESIZE_STOP = 'column:resize:stop';
  479. exports.EVENT_RESIZE_STOP = EVENT_RESIZE_STOP;
  480. var SELECTOR_TH = 'tr:first > th:visible';
  481. exports.SELECTOR_TH = SELECTOR_TH;
  482. var SELECTOR_TD = 'tr:first > td:visible';
  483. exports.SELECTOR_TD = SELECTOR_TD;
  484. var SELECTOR_UNRESIZABLE = '[data-noresize]';
  485. exports.SELECTOR_UNRESIZABLE = SELECTOR_UNRESIZABLE;
  486. },{}],4:[function(require,module,exports){
  487. 'use strict';
  488. Object.defineProperty(exports, '__esModule', {
  489. value: true
  490. });
  491. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  492. var _class = require('./class');
  493. var _class2 = _interopRequireDefault(_class);
  494. var _adapter = require('./adapter');
  495. var _adapter2 = _interopRequireDefault(_adapter);
  496. exports['default'] = _class2['default'];
  497. module.exports = exports['default'];
  498. },{"./adapter":1,"./class":2}]},{},[4])