multiselect.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. * @license
  3. *
  4. * Multiselect v2.5.2
  5. * http://crlcu.github.io/multiselect/
  6. *
  7. * Copyright (c) 2016-2018 Adrian Crisan
  8. * Licensed under the MIT license (https://github.com/crlcu/multiselect/blob/master/LICENSE)
  9. */
  10. if (typeof jQuery === 'undefined') {
  11. throw new Error('multiselect requires jQuery');
  12. }
  13. ;(function ($) {
  14. 'use strict';
  15. var version = $.fn.jquery.split(' ')[0].split('.');
  16. if (version[0] < 2 && version[1] < 7) {
  17. throw new Error('multiselect requires jQuery version 1.7 or higher');
  18. }
  19. })(jQuery);
  20. ;(function (factory) {
  21. if (typeof define === 'function' && define.amd) {
  22. // AMD. Register as an anonymous module depending on jQuery.
  23. define(['jquery'], factory);
  24. } else {
  25. // No AMD. Register plugin with global jQuery object.
  26. factory(jQuery);
  27. }
  28. }(function ($) {
  29. 'use strict';
  30. var Multiselect = (function($) {
  31. /** Multiselect object constructor
  32. *
  33. * @class Multiselect
  34. * @constructor
  35. **/
  36. function Multiselect( $select, settings ) {
  37. var id = $select.prop('id');
  38. this.$left = $select;
  39. this.$right = $( settings.right ).length ? $( settings.right ) : $('#' + id + '_to');
  40. this.actions = {
  41. $leftAll: $( settings.leftAll ).length ? $( settings.leftAll ) : $('#' + id + '_leftAll'),
  42. $rightAll: $( settings.rightAll ).length ? $( settings.rightAll ) : $('#' + id + '_rightAll'),
  43. $leftSelected: $( settings.leftSelected ).length ? $( settings.leftSelected ) : $('#' + id + '_leftSelected'),
  44. $rightSelected: $( settings.rightSelected ).length ? $( settings.rightSelected ) : $('#' + id + '_rightSelected'),
  45. $undo: $( settings.undo ).length ? $( settings.undo ) : $('#' + id + '_undo'),
  46. $redo: $( settings.redo ).length ? $( settings.redo ) : $('#' + id + '_redo'),
  47. $moveUp: $( settings.moveUp ).length ? $( settings.moveUp ) : $('#' + id + '_move_up'),
  48. $moveDown: $( settings.moveDown ).length ? $( settings.moveDown ) : $('#' + id + '_move_down')
  49. };
  50. delete settings.leftAll;
  51. delete settings.leftSelected;
  52. delete settings.right;
  53. delete settings.rightAll;
  54. delete settings.rightSelected;
  55. delete settings.undo;
  56. delete settings.redo;
  57. delete settings.moveUp;
  58. delete settings.moveDown;
  59. this.options = {
  60. keepRenderingSort: settings.keepRenderingSort,
  61. submitAllLeft: settings.submitAllLeft !== undefined ? settings.submitAllLeft : true,
  62. submitAllRight: settings.submitAllRight !== undefined ? settings.submitAllRight : true,
  63. search: settings.search,
  64. ignoreDisabled: settings.ignoreDisabled !== undefined ? settings.ignoreDisabled : false,
  65. matchOptgroupBy: settings.matchOptgroupBy !== undefined ? settings.matchOptgroupBy : 'label'
  66. };
  67. delete settings.keepRenderingSort, settings.submitAllLeft, settings.submitAllRight, settings.search, settings.ignoreDisabled, settings.matchOptgroupBy;
  68. this.callbacks = settings;
  69. if ( typeof this.callbacks.sort == 'function' ) {
  70. var sort = this.callbacks.sort;
  71. this.callbacks.sort = {
  72. left: sort,
  73. right: sort,
  74. };
  75. }
  76. this.init();
  77. }
  78. Multiselect.prototype = {
  79. init: function() {
  80. var self = this;
  81. self.undoStack = [];
  82. self.redoStack = [];
  83. if (self.options.keepRenderingSort) {
  84. self.skipInitSort = true;
  85. if (self.callbacks.sort !== false) {
  86. self.callbacks.sort = {
  87. left: function(a, b) {
  88. return $(a).data('position') > $(b).data('position') ? 1 : -1;
  89. },
  90. right: function(a, b) {
  91. return $(a).data('position') > $(b).data('position') ? 1 : -1;
  92. },
  93. };
  94. }
  95. self.$left.attachIndex();
  96. self.$right.each(function(i, select) {
  97. $(select).attachIndex();
  98. });
  99. }
  100. if ( typeof self.callbacks.startUp == 'function' ) {
  101. self.callbacks.startUp( self.$left, self.$right );
  102. }
  103. if ( !self.skipInitSort ) {
  104. if ( typeof self.callbacks.sort.left == 'function' ) {
  105. self.$left.mSort(self.callbacks.sort.left);
  106. }
  107. if ( typeof self.callbacks.sort.right == 'function' ) {
  108. self.$right.each(function(i, select) {
  109. $(select).mSort(self.callbacks.sort.right);
  110. });
  111. }
  112. }
  113. // Append left filter
  114. if (self.options.search && self.options.search.left) {
  115. self.options.search.$left = $(self.options.search.left);
  116. self.$left.before(self.options.search.$left);
  117. }
  118. // Append right filter
  119. if (self.options.search && self.options.search.right) {
  120. self.options.search.$right = $(self.options.search.right);
  121. self.$right.before($(self.options.search.$right));
  122. }
  123. // Initialize events
  124. self.events();
  125. if ( typeof self.callbacks.afterInit == 'function' ) {
  126. self.callbacks.afterInit();
  127. }
  128. },
  129. events: function() {
  130. var self = this;
  131. // Attach event to left filter
  132. if (self.options.search && self.options.search.$left) {
  133. self.options.search.$left.on('keyup', function(e) {
  134. if (self.callbacks.fireSearch(this.value)) {
  135. var $toShow = self.$left.find('option:search("' + this.value + '")').mShow();
  136. var $toHide = self.$left.find('option:not(:search("' + this.value + '"))').mHide();
  137. var $grpHide = self.$left.find('option').closest('optgroup').mHide();
  138. var $grpShow = self.$left.find('option:not(.hidden)').parent('optgroup').mShow();
  139. } else {
  140. self.$left.find('option, optgroup').mShow();
  141. }
  142. });
  143. }
  144. // Attach event to right filter
  145. if (self.options.search && self.options.search.$right) {
  146. self.options.search.$right.on('keyup', function(e) {
  147. if (self.callbacks.fireSearch(this.value)) {
  148. var $toShow = self.$right.find('option:search("' + this.value + '")').mShow();
  149. var $toHide = self.$right.find('option:not(:search("' + this.value + '"))').mHide();
  150. var $grpHide = self.$right.find('option').closest('optgroup').mHide();
  151. var $grpShow = self.$right.find('option:not(.hidden)').parent('optgroup').mShow();
  152. } else {
  153. self.$right.find('option, optgroup').mShow();
  154. }
  155. });
  156. }
  157. // Select all the options from left and right side when submiting the parent form
  158. self.$right.closest('form').on('submit', function(e) {
  159. if (self.options.search) {
  160. // Clear left search input
  161. if (self.options.search.$left) {
  162. self.options.search.$left.val('').trigger('keyup');
  163. }
  164. // Clear right search input
  165. if (self.options.search.$right) {
  166. self.options.search.$right.val('').trigger('keyup');
  167. }
  168. }
  169. self.$left.find('option').prop('selected', self.options.submitAllLeft);
  170. self.$right.find('option').prop('selected', self.options.submitAllRight);
  171. });
  172. // Attach event for double clicking on options from left side
  173. self.$left.on('dblclick', 'option', function(e) {
  174. e.preventDefault();
  175. var $options = self.$left.find('option:selected');
  176. if ( $options.length ) {
  177. self.moveToRight($options, e);
  178. }
  179. });
  180. // Attach event for clicking on optgroup's from left side
  181. self.$left.on('click', 'optgroup', function(e) {
  182. if ($(e.target).prop('tagName') == 'OPTGROUP') {
  183. $(this)
  184. .children()
  185. .prop('selected', true);
  186. }
  187. });
  188. // Attach event for pushing ENTER on options from left side
  189. self.$left.on('keypress', function(e) {
  190. if (e.keyCode === 13) {
  191. e.preventDefault();
  192. var $options = self.$left.find('option:selected');
  193. if ( $options.length ) {
  194. self.moveToRight($options, e);
  195. }
  196. }
  197. });
  198. // Attach event for double clicking on options from right side
  199. self.$right.on('dblclick', 'option', function(e) {
  200. e.preventDefault();
  201. var $options = self.$right.find('option:selected');
  202. if ( $options.length ) {
  203. self.moveToLeft($options, e);
  204. }
  205. });
  206. // Attach event for clicking on optgroup's from right side
  207. self.$right.on('click', 'optgroup', function(e) {
  208. if ($(e.target).prop('tagName') == 'OPTGROUP') {
  209. $(this)
  210. .children()
  211. .prop('selected', true);
  212. }
  213. });
  214. // Attach event for pushing BACKSPACE or DEL on options from right side
  215. self.$right.on('keydown', function(e) {
  216. if (e.keyCode === 8 || e.keyCode === 46) {
  217. e.preventDefault();
  218. var $options = self.$right.find('option:selected');
  219. if ( $options.length ) {
  220. self.moveToLeft($options, e);
  221. }
  222. }
  223. });
  224. // dblclick support for IE
  225. if ( navigator.userAgent.match(/MSIE/i) || navigator.userAgent.indexOf('Trident/') > 0 || navigator.userAgent.indexOf('Edge/') > 0) {
  226. self.$left.dblclick(function(e) {
  227. self.actions.$rightSelected.trigger('click');
  228. });
  229. self.$right.dblclick(function(e) {
  230. self.actions.$leftSelected.trigger('click');
  231. });
  232. }
  233. self.actions.$rightSelected.on('click', function(e) {
  234. e.preventDefault();
  235. var $options = self.$left.find('option:selected');
  236. if ( $options.length ) {
  237. self.moveToRight($options, e);
  238. }
  239. $(this).blur();
  240. });
  241. self.actions.$leftSelected.on('click', function(e) {
  242. e.preventDefault();
  243. var $options = self.$right.find('option:selected');
  244. if ( $options.length ) {
  245. self.moveToLeft($options, e);
  246. }
  247. $(this).blur();
  248. });
  249. self.actions.$rightAll.on('click', function(e) {
  250. e.preventDefault();
  251. var $options = self.$left.children(':not(span):not(.hidden)');
  252. if ( $options.length ) {
  253. self.moveToRight($options, e);
  254. }
  255. $(this).blur();
  256. });
  257. self.actions.$leftAll.on('click', function(e) {
  258. e.preventDefault();
  259. var $options = self.$right.children(':not(span):not(.hidden)');
  260. if ( $options.length ) {
  261. self.moveToLeft($options, e);
  262. }
  263. $(this).blur();
  264. });
  265. self.actions.$undo.on('click', function(e) {
  266. e.preventDefault();
  267. self.undo(e);
  268. });
  269. self.actions.$redo.on('click', function(e) {
  270. e.preventDefault();
  271. self.redo(e);
  272. });
  273. self.actions.$moveUp.on('click', function(e) {
  274. e.preventDefault();
  275. var $options = self.$right.find(':selected:not(span):not(.hidden)');
  276. if ( $options.length ) {
  277. self.moveUp($options, e);
  278. }
  279. $(this).blur();
  280. });
  281. self.actions.$moveDown.on('click', function(e) {
  282. e.preventDefault();
  283. var $options = self.$right.find(':selected:not(span):not(.hidden)');
  284. if ( $options.length ) {
  285. self.moveDown($options, e);
  286. }
  287. $(this).blur();
  288. });
  289. },
  290. moveToRight: function( $options, event, silent, skipStack ) {
  291. var self = this;
  292. if ( typeof self.callbacks.moveToRight == 'function' ) {
  293. return self.callbacks.moveToRight( self, $options, event, silent, skipStack );
  294. }
  295. if ( typeof self.callbacks.beforeMoveToRight == 'function' && !silent ) {
  296. if ( !self.callbacks.beforeMoveToRight( self.$left, self.$right, $options ) ) {
  297. return false;
  298. }
  299. }
  300. self.moveFromAtoB(self.$left, self.$right, $options, event, silent, skipStack);
  301. if ( !skipStack ) {
  302. self.undoStack.push(['right', $options ]);
  303. self.redoStack = [];
  304. }
  305. if ( typeof self.callbacks.sort.right == 'function' && !silent && !self.doNotSortRight ) {
  306. self.$right.mSort(self.callbacks.sort.right);
  307. }
  308. if ( typeof self.callbacks.afterMoveToRight == 'function' && !silent ) {
  309. self.callbacks.afterMoveToRight( self.$left, self.$right, $options );
  310. }
  311. return self;
  312. },
  313. moveToLeft: function( $options, event, silent, skipStack ) {
  314. var self = this;
  315. if ( typeof self.callbacks.moveToLeft == 'function' ) {
  316. return self.callbacks.moveToLeft( self, $options, event, silent, skipStack );
  317. }
  318. if ( typeof self.callbacks.beforeMoveToLeft == 'function' && !silent ) {
  319. if ( !self.callbacks.beforeMoveToLeft( self.$left, self.$right, $options ) ) {
  320. return false;
  321. }
  322. }
  323. self.moveFromAtoB(self.$right, self.$left, $options, event, silent, skipStack);
  324. if ( !skipStack ) {
  325. self.undoStack.push(['left', $options ]);
  326. self.redoStack = [];
  327. }
  328. if ( typeof self.callbacks.sort.left == 'function' && !silent ) {
  329. self.$left.mSort(self.callbacks.sort.left);
  330. }
  331. if ( typeof self.callbacks.afterMoveToLeft == 'function' && !silent ) {
  332. self.callbacks.afterMoveToLeft( self.$left, self.$right, $options );
  333. }
  334. return self;
  335. },
  336. moveFromAtoB: function( $source, $destination, $options, event, silent, skipStack ) {
  337. var self = this;
  338. if ( typeof self.callbacks.moveFromAtoB == 'function' ) {
  339. return self.callbacks.moveFromAtoB(self, $source, $destination, $options, event, silent, skipStack);
  340. }
  341. $options.each(function(index, option) {
  342. var $option = $(option);
  343. if (self.options.ignoreDisabled && $option.is(':disabled')) {
  344. return true;
  345. }
  346. if ($option.is('optgroup') || $option.parent().is('optgroup')) {
  347. var $sourceGroup = $option.is('optgroup') ? $option : $option.parent();
  348. var optgroupSelector = 'optgroup[' + self.options.matchOptgroupBy + '="' + $sourceGroup.prop(self.options.matchOptgroupBy) + '"]';
  349. var $destinationGroup = $destination.find(optgroupSelector);
  350. if (!$destinationGroup.length) {
  351. $destinationGroup = $sourceGroup.clone(true);
  352. $destinationGroup.empty();
  353. $destination.move($destinationGroup);
  354. }
  355. if ($option.is('optgroup')) {
  356. $destinationGroup.move($option.find('option'));
  357. } else {
  358. $destinationGroup.move($option);
  359. }
  360. $sourceGroup.removeIfEmpty();
  361. } else {
  362. $destination.move($option);
  363. }
  364. });
  365. return self;
  366. },
  367. moveUp: function($options) {
  368. var self = this;
  369. if ( typeof self.callbacks.beforeMoveUp == 'function' ) {
  370. if ( !self.callbacks.beforeMoveUp( $options ) ) {
  371. return false;
  372. }
  373. }
  374. $options.first().prev().before($options);
  375. if ( typeof self.callbacks.afterMoveUp == 'function' ) {
  376. self.callbacks.afterMoveUp( $options );
  377. }
  378. },
  379. moveDown: function($options) {
  380. var self = this;
  381. if ( typeof self.callbacks.beforeMoveDown == 'function' ) {
  382. if ( !self.callbacks.beforeMoveDown( $options ) ) {
  383. return false;
  384. }
  385. }
  386. $options.last().next().after($options);
  387. if ( typeof self.callbacks.afterMoveDown == 'function' ) {
  388. self.callbacks.afterMoveDown( $options );
  389. }
  390. },
  391. undo: function(event) {
  392. var self = this;
  393. var last = self.undoStack.pop();
  394. if ( last ) {
  395. self.redoStack.push(last);
  396. switch(last[0]) {
  397. case 'left':
  398. self.moveToRight(last[1], event, false, true);
  399. break;
  400. case 'right':
  401. self.moveToLeft(last[1], event, false, true);
  402. break;
  403. }
  404. }
  405. },
  406. redo: function(event) {
  407. var self = this;
  408. var last = self.redoStack.pop();
  409. if ( last ) {
  410. self.undoStack.push(last);
  411. switch(last[0]) {
  412. case 'left':
  413. self.moveToLeft(last[1], event, false, true);
  414. break;
  415. case 'right':
  416. self.moveToRight(last[1], event, false, true);
  417. break;
  418. }
  419. }
  420. }
  421. }
  422. return Multiselect;
  423. })($);
  424. $.multiselect = {
  425. defaults: {
  426. /** will be executed once - remove from $left all options that are already in $right
  427. *
  428. * @method startUp
  429. * @attribute $left jQuery object
  430. * @attribute $right jQuery object
  431. **/
  432. startUp: function( $left, $right ) {
  433. $right.find('option').each(function(index, rightOption) {
  434. if ($(rightOption).parent().prop('tagName') == 'OPTGROUP') {
  435. var optgroupSelector = 'optgroup[label="' + $(rightOption).parent().attr('label') + '"]';
  436. $left.find(optgroupSelector + ' option[value="' + rightOption.value + '"]').each(function(index, leftOption) {
  437. leftOption.remove();
  438. });
  439. $left.find(optgroupSelector).removeIfEmpty();
  440. } else {
  441. var $option = $left.find('option[value="' + rightOption.value + '"]');
  442. $option.remove();
  443. }
  444. });
  445. },
  446. /** will be executed after initialize plugin
  447. *
  448. * @method afterInit
  449. *
  450. * @default true
  451. * @return {boolean}
  452. **/
  453. afterInit: function(){ return true; },
  454. /** will be executed each time before moving option[s] to right
  455. *
  456. * IMPORTANT : this method must return boolean value
  457. * true : continue to moveToRight method
  458. * false : stop
  459. *
  460. * @method beforeMoveToRight
  461. * @attribute $left jQuery object
  462. * @attribute $right jQuery object
  463. * @attribute $options HTML object (the option[s] which was selected to be moved)
  464. *
  465. * @default true
  466. * @return {boolean}
  467. **/
  468. beforeMoveToRight: function($left, $right, $options) { return true; },
  469. /* will be executed each time after moving option[s] to right
  470. *
  471. * @method afterMoveToRight
  472. * @attribute $left jQuery object
  473. * @attribute $right jQuery object
  474. * @attribute $options HTML object (the option[s] which was selected to be moved)
  475. **/
  476. afterMoveToRight: function($left, $right, $options) {},
  477. /** will be executed each time before moving option[s] to left
  478. *
  479. * IMPORTANT : this method must return boolean value
  480. * true : continue to moveToRight method
  481. * false : stop
  482. *
  483. * @method beforeMoveToLeft
  484. * @attribute $left jQuery object
  485. * @attribute $right jQuery object
  486. * @attribute $options HTML object (the option[s] which was selected to be moved)
  487. *
  488. * @default true
  489. * @return {boolean}
  490. **/
  491. beforeMoveToLeft: function($left, $right, $options) { return true; },
  492. /* will be executed each time after moving option[s] to left
  493. *
  494. * @method afterMoveToLeft
  495. * @attribute $left jQuery object
  496. * @attribute $right jQuery object
  497. * @attribute $options HTML object (the option[s] which was selected to be moved)
  498. **/
  499. afterMoveToLeft: function($left, $right, $options) {},
  500. /** will be executed each time before moving option[s] up
  501. *
  502. * IMPORTANT : this method must return boolean value
  503. * true : continue to moveUp method
  504. * false : stop
  505. *
  506. * @method beforeMoveUp
  507. * @attribute $options HTML object (the option[s] which was selected to be moved)
  508. *
  509. * @default true
  510. * @return {boolean}
  511. **/
  512. beforeMoveUp: function($options) { return true; },
  513. /* will be executed each time after moving option[s] up
  514. *
  515. * @method afterMoveUp
  516. * @attribute $left jQuery object
  517. * @attribute $right jQuery object
  518. * @attribute $options HTML object (the option[s] which was selected to be moved)
  519. **/
  520. afterMoveUp: function($options) {},
  521. /** will be executed each time before moving option[s] down
  522. *
  523. * IMPORTANT : this method must return boolean value
  524. * true : continue to moveUp method
  525. * false : stop
  526. *
  527. * @method beforeMoveDown
  528. * @attribute $options HTML object (the option[s] which was selected to be moved)
  529. *
  530. * @default true
  531. * @return {boolean}
  532. **/
  533. beforeMoveDown: function($options) { return true; },
  534. /* will be executed each time after moving option[s] down
  535. *
  536. * @method afterMoveUp
  537. * @attribute $left jQuery object
  538. * @attribute $right jQuery object
  539. * @attribute $options HTML object (the option[s] which was selected to be moved)
  540. **/
  541. afterMoveDown: function($options) {},
  542. /** sort options by option text
  543. *
  544. * @method sort
  545. * @attribute a HTML option
  546. * @attribute b HTML option
  547. *
  548. * @return 1/-1
  549. **/
  550. sort: function(a, b) {
  551. if (a.innerHTML == 'NA') {
  552. return 1;
  553. } else if (b.innerHTML == 'NA') {
  554. return -1;
  555. }
  556. return (a.innerHTML > b.innerHTML) ? 1 : -1;
  557. },
  558. /* will tell if the search can start
  559. *
  560. * @method fireSearch
  561. * @attribute value String
  562. *
  563. * @return {boolean}
  564. **/
  565. fireSearch: function(value) {
  566. return value.length > 1;
  567. }
  568. }
  569. };
  570. var ua = window.navigator.userAgent;
  571. var isIE = (ua.indexOf("MSIE ") + ua.indexOf("Trident/") + ua.indexOf("Edge/")) > -3;
  572. var isSafari = ua.toLowerCase().indexOf("safari") > -1;
  573. var isFirefox = ua.toLowerCase().indexOf("firefox") > -1;
  574. $.fn.multiselect = function( options ) {
  575. return this.each(function() {
  576. var $this = $(this),
  577. data = $this.data('crlcu.multiselect'),
  578. settings = $.extend({}, $.multiselect.defaults, $this.data(), (typeof options === 'object' && options));
  579. if (!data) {
  580. $this.data('crlcu.multiselect', (data = new Multiselect($this, settings)));
  581. }
  582. });
  583. };
  584. // append options
  585. // then set the selected attribute to false
  586. $.fn.move = function( $options ) {
  587. this
  588. .append($options)
  589. .find('option')
  590. .prop('selected', false);
  591. return this;
  592. };
  593. $.fn.removeIfEmpty = function() {
  594. if (!this.children().length) {
  595. this.remove();
  596. }
  597. return this;
  598. };
  599. $.fn.mShow = function() {
  600. this.removeClass('hidden').show();
  601. if (isIE || isSafari) {
  602. this.each(function(index, option) {
  603. // Remove <span> to make it compatible with IE
  604. if($(option).parent().is('span')) {
  605. $(option).parent().replaceWith(option);
  606. }
  607. $(option).show();
  608. });
  609. }
  610. if(isFirefox){
  611. this.attr('disabled', false)
  612. }
  613. return this;
  614. };
  615. $.fn.mHide = function() {
  616. this.addClass('hidden').hide();
  617. if (isIE || isSafari) {
  618. this.each(function(index, option) {
  619. // Wrap with <span> to make it compatible with IE
  620. if(!$(option).parent().is('span')) {
  621. $(option).wrap('<span>').hide();
  622. }
  623. });
  624. }
  625. if(isFirefox){
  626. this.attr('disabled', true)
  627. }
  628. return this;
  629. };
  630. // sort options then reappend them to the select
  631. $.fn.mSort = function(callback) {
  632. this
  633. .children()
  634. .sort(callback)
  635. .appendTo(this);
  636. this
  637. .find('optgroup')
  638. .each(function(i, group) {
  639. $(group).children()
  640. .sort(callback)
  641. .appendTo(group);
  642. })
  643. return this;
  644. };
  645. // attach index to children
  646. $.fn.attachIndex = function() {
  647. this.children().each(function(index, option) {
  648. var $option = $(option);
  649. if ($option.is('optgroup')) {
  650. $option.children().each(function(i, children) {
  651. $(children).data('position', i);
  652. });
  653. }
  654. $option.data('position', index);
  655. });
  656. };
  657. $.expr[":"].search = function(elem, index, meta) {
  658. var regex = new RegExp(meta[3].replace(/([^a-zA-Z0-9])/g, "\\$1"), "i");
  659. return $(elem).text().match(regex);
  660. }
  661. }));