PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/free-jqgrid/4.11.0/plugins/ui.multiselect.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 341 lines | 261 code | 34 blank | 46 comment | 29 complexity | 6cfd386e1da5551b2d66a0e550f03f98 MD5 | raw file
  1. /**
  2. * @license jQuery UI Multiselect
  3. *
  4. * Authors:
  5. * Michael Aufreiter (quasipartikel.at)
  6. * Yanick Rochon (yanick.rochon[at]gmail[dot]com)
  7. *
  8. * Dual licensed under the MIT (MIT-LICENSE.txt)
  9. * and GPL (GPL-LICENSE.txt) licenses.
  10. *
  11. * http://www.quasipartikel.at/multiselect/
  12. *
  13. *
  14. * Depends:
  15. * ui.core.js
  16. * ui.sortable.js
  17. *
  18. * Optional:
  19. * localization (http://plugins.jquery.com/project/localisation)
  20. * scrollTo (http://plugins.jquery.com/project/ScrollTo)
  21. *
  22. * Todo:
  23. * Make batch actions faster
  24. * Implement dynamic insertion through remote calls
  25. */
  26. (function($) {
  27. $.widget("ui.multiselect", {
  28. options: {
  29. sortable: true,
  30. searchable: true,
  31. doubleClickable: true,
  32. animated: 'fast',
  33. show: 'slideDown',
  34. hide: 'slideUp',
  35. dividerLocation: 0.6,
  36. availableFirst: false,
  37. nodeComparator: function(node1,node2) {
  38. var text1 = node1.text(),
  39. text2 = node2.text();
  40. return text1 == text2 ? 0 : (text1 < text2 ? -1 : 1);
  41. }
  42. },
  43. _create: function() {
  44. this.element.hide();
  45. this.id = this.element.attr("id");
  46. this.container = $('<div class="ui-multiselect ui-helper-clearfix ui-widget"></div>').insertAfter(this.element);
  47. this.count = 0; // number of currently selected options
  48. this.selectedContainer = $('<div class="selected"></div>').appendTo(this.container);
  49. this.availableContainer = $('<div class="available"></div>')[this.options.availableFirst?'prependTo': 'appendTo'](this.container);
  50. this.selectedActions = $('<div class="actions ui-widget-header ui-helper-clearfix"><span class="count">0 '+$.ui.multiselect.locale.itemsCount+'</span><a href="#" class="remove-all">'+$.ui.multiselect.locale.removeAll+'</a></div>').appendTo(this.selectedContainer);
  51. this.availableActions = $('<div class="actions ui-widget-header ui-helper-clearfix"><input type="text" class="search empty ui-widget-content ui-corner-all"/><a href="#" class="add-all">'+$.ui.multiselect.locale.addAll+'</a></div>').appendTo(this.availableContainer);
  52. this.selectedList = $('<ul class="selected connected-list"><li class="ui-helper-hidden-accessible"></li></ul>').bind('selectstart', function(){return false;}).appendTo(this.selectedContainer);
  53. this.availableList = $('<ul class="available connected-list"><li class="ui-helper-hidden-accessible"></li></ul>').bind('selectstart', function(){return false;}).appendTo(this.availableContainer);
  54. var that = this;
  55. // set dimensions
  56. this.container.width(this.element.width()+1);
  57. this.selectedContainer.width(Math.floor(this.element.width()*this.options.dividerLocation));
  58. this.availableContainer.width(Math.floor(this.element.width()*(1-this.options.dividerLocation)));
  59. // fix list height to match <option> depending on their individual header's heights
  60. this.selectedList.height(Math.max(this.element.height()-this.selectedActions.height(),1));
  61. this.availableList.height(Math.max(this.element.height()-this.availableActions.height(),1));
  62. if ( !this.options.animated ) {
  63. this.options.show = 'show';
  64. this.options.hide = 'hide';
  65. }
  66. // init lists
  67. this._populateLists(this.element.find('option'));
  68. // make selection sortable
  69. if (this.options.sortable) {
  70. this.selectedList.sortable({
  71. placeholder: 'ui-state-highlight',
  72. axis: 'y',
  73. update: function(event, ui) {
  74. // apply the new sort order to the original selectbox
  75. that.selectedList.find('li').each(function() {
  76. if ($(this).data('optionLink'))
  77. $(this).data('optionLink').remove().appendTo(that.element);
  78. });
  79. },
  80. receive: function(event, ui) {
  81. ui.item.data('optionLink').attr('selected', true);
  82. // increment count
  83. that.count += 1;
  84. that._updateCount();
  85. // workaround, because there's no way to reference
  86. // the new element, see http://dev.jqueryui.com/ticket/4303
  87. that.selectedList.children('.ui-draggable').each(function() {
  88. $(this).removeClass('ui-draggable');
  89. $(this).data('optionLink', ui.item.data('optionLink'));
  90. $(this).data('idx', ui.item.data('idx'));
  91. that._applyItemState($(this), true);
  92. });
  93. // workaround according to http://dev.jqueryui.com/ticket/4088
  94. setTimeout(function() { ui.item.remove(); }, 1);
  95. }
  96. });
  97. }
  98. // set up livesearch
  99. if (this.options.searchable) {
  100. this._registerSearchEvents(this.availableContainer.find('input.search'));
  101. } else {
  102. $('.search').hide();
  103. }
  104. // batch actions
  105. this.container.find(".remove-all").click(function() {
  106. that._populateLists(that.element.find('option').removeAttr('selected'));
  107. return false;
  108. });
  109. this.container.find(".add-all").click(function() {
  110. var options = that.element.find('option').not("[selected]");
  111. if (that.availableList.children('li:hidden').length > 1) {
  112. that.availableList.children('li').each(function(i) {
  113. if ($(this).is(":visible")) $(options[i-1]).attr('selected', 'selected');
  114. });
  115. } else {
  116. options.attr('selected', 'selected');
  117. }
  118. that._populateLists(that.element.find('option'));
  119. return false;
  120. });
  121. },
  122. destroy: function() {
  123. this.element.show();
  124. this.container.remove();
  125. $.Widget.prototype.destroy.apply(this, arguments);
  126. },
  127. _populateLists: function(options) {
  128. this.selectedList.children('.ui-element').remove();
  129. this.availableList.children('.ui-element').remove();
  130. this.count = 0;
  131. var that = this;
  132. var items = $(options.map(function(i) {
  133. var isSelected = $(this).is("[selected]"), item = that._getOptionNode(this).appendTo(isSelected ? that.selectedList : that.availableList).show();
  134. if (isSelected) that.count += 1;
  135. that._applyItemState(item, isSelected);
  136. item.data('idx', i);
  137. return item[0];
  138. }));
  139. // update count
  140. this._updateCount();
  141. that._filter.apply(this.availableContainer.find('input.search'), [that.availableList]);
  142. },
  143. _updateCount: function() {
  144. this.element.trigger('change');
  145. this.selectedContainer.find('span.count').text(this.count+" "+$.ui.multiselect.locale.itemsCount);
  146. },
  147. _getOptionNode: function(option) {
  148. option = $(option);
  149. var node = $('<li class="ui-state-default ui-element" title="'+(option.attr("title") || option.text())+'"><span class="ui-icon"/>'+option.text()+'<a href="#" class="action"><span class="ui-corner-all ui-icon"/></a></li>').hide();
  150. node.data('optionLink', option);
  151. return node;
  152. },
  153. // clones an item with associated data
  154. // didn't find a smarter away around this
  155. _cloneWithData: function(clonee) {
  156. var clone = clonee.clone(false,false);
  157. clone.data('optionLink', clonee.data('optionLink'));
  158. clone.data('idx', clonee.data('idx'));
  159. return clone;
  160. },
  161. _setSelected: function(item, selected) {
  162. item.data('optionLink').attr('selected', selected);
  163. if (selected) {
  164. var selectedItem = this._cloneWithData(item);
  165. item[this.options.hide](this.options.animated, function() { $(this).remove(); });
  166. selectedItem.appendTo(this.selectedList).hide()[this.options.show](this.options.animated);
  167. this._applyItemState(selectedItem, true);
  168. return selectedItem;
  169. } else {
  170. // look for successor based on initial option index
  171. var items = this.availableList.find('li'), comparator = this.options.nodeComparator;
  172. var succ = null, i = item.data('idx'), direction = comparator(item, $(items[i]));
  173. // TODO: test needed for dynamic list populating
  174. if ( direction ) {
  175. while (i>=0 && i<items.length) {
  176. direction > 0 ? i++ : i--;
  177. if ( direction != comparator(item, $(items[i])) ) {
  178. // going up, go back one item down, otherwise leave as is
  179. succ = items[direction > 0 ? i : i+1];
  180. break;
  181. }
  182. }
  183. } else {
  184. succ = items[i];
  185. }
  186. var availableItem = this._cloneWithData(item);
  187. succ ? availableItem.insertBefore($(succ)) : availableItem.appendTo(this.availableList);
  188. item[this.options.hide](this.options.animated, function() { $(this).remove(); });
  189. availableItem.hide()[this.options.show](this.options.animated);
  190. this._applyItemState(availableItem, false);
  191. return availableItem;
  192. }
  193. },
  194. _applyItemState: function(item, selected) {
  195. if (selected) {
  196. if (this.options.sortable)
  197. item.children('span').addClass('ui-icon-arrowthick-2-n-s').removeClass('ui-helper-hidden').addClass('ui-icon');
  198. else
  199. item.children('span').removeClass('ui-icon-arrowthick-2-n-s').addClass('ui-helper-hidden').removeClass('ui-icon');
  200. item.find('a.action span').addClass('ui-icon-minus').removeClass('ui-icon-plus');
  201. this._registerRemoveEvents(item.find('a.action'));
  202. } else {
  203. item.children('span').removeClass('ui-icon-arrowthick-2-n-s').addClass('ui-helper-hidden').removeClass('ui-icon');
  204. item.find('a.action span').addClass('ui-icon-plus').removeClass('ui-icon-minus');
  205. this._registerAddEvents(item.find('a.action'));
  206. }
  207. this._registerDoubleClickEvents(item);
  208. this._registerHoverEvents(item);
  209. },
  210. // taken from John Resig's liveUpdate script
  211. _filter: function(list) {
  212. var input = $(this);
  213. var rows = list.children('li'),
  214. cache = rows.map(function(){
  215. return $(this).text().toLowerCase();
  216. });
  217. var term = $.trim(input.val().toLowerCase()), scores = [];
  218. if (!term) {
  219. rows.show();
  220. } else {
  221. rows.hide();
  222. cache.each(function(i) {
  223. if (this.indexOf(term)>-1) { scores.push(i); }
  224. });
  225. $.each(scores, function() {
  226. $(rows[this]).show();
  227. });
  228. }
  229. },
  230. _registerDoubleClickEvents: function(elements) {
  231. if (!this.options.doubleClickable) return;
  232. elements.dblclick(function(ev) {
  233. if ($(ev.target).closest('.action').length === 0) {
  234. // This may be triggered with rapid clicks on actions as well. In that
  235. // case don't trigger an additional click.
  236. elements.find('a.action').click();
  237. }
  238. });
  239. },
  240. _registerHoverEvents: function(elements) {
  241. elements.removeClass('ui-state-hover');
  242. elements.mouseover(function() {
  243. $(this).addClass('ui-state-hover');
  244. });
  245. elements.mouseout(function() {
  246. $(this).removeClass('ui-state-hover');
  247. });
  248. },
  249. _registerAddEvents: function(elements) {
  250. var that = this;
  251. elements.click(function() {
  252. var item = that._setSelected($(this).parent(), true);
  253. that.count += 1;
  254. that._updateCount();
  255. return false;
  256. });
  257. // make draggable
  258. if (this.options.sortable) {
  259. elements.each(function() {
  260. $(this).parent().draggable({
  261. connectToSortable: that.selectedList,
  262. helper: function() {
  263. var selectedItem = that._cloneWithData($(this)).width($(this).width() - 50);
  264. selectedItem.width($(this).width());
  265. return selectedItem;
  266. },
  267. appendTo: that.container,
  268. containment: that.container,
  269. revert: 'invalid'
  270. });
  271. });
  272. }
  273. },
  274. _registerRemoveEvents: function(elements) {
  275. var that = this;
  276. elements.click(function() {
  277. that._setSelected($(this).parent(), false);
  278. that.count -= 1;
  279. that._updateCount();
  280. return false;
  281. });
  282. },
  283. _registerSearchEvents: function(input) {
  284. var that = this;
  285. input.focus(function() {
  286. $(this).addClass('ui-state-active');
  287. })
  288. .blur(function() {
  289. $(this).removeClass('ui-state-active');
  290. })
  291. .keypress(function(e) {
  292. if (e.keyCode == 13)
  293. return false;
  294. })
  295. .keyup(function() {
  296. that._filter.apply(this, [that.availableList]);
  297. });
  298. }
  299. });
  300. $.extend($.ui.multiselect, {
  301. locale: {
  302. addAll:'Add all',
  303. removeAll:'Remove all',
  304. itemsCount:'items selected'
  305. }
  306. });
  307. })(jQuery);