You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

809 lines
25 KiB

  1. /*!
  2. * # Semantic UI 2.1.6 - Checkbox
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Copyright 2015 Contributors
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. ;(function ( $, window, document, undefined ) {
  12. "use strict";
  13. $.fn.checkbox = function(parameters) {
  14. var
  15. $allModules = $(this),
  16. moduleSelector = $allModules.selector || '',
  17. time = new Date().getTime(),
  18. performance = [],
  19. query = arguments[0],
  20. methodInvoked = (typeof query == 'string'),
  21. queryArguments = [].slice.call(arguments, 1),
  22. returnedValue
  23. ;
  24. $allModules
  25. .each(function() {
  26. var
  27. settings = $.extend(true, {}, $.fn.checkbox.settings, parameters),
  28. className = settings.className,
  29. namespace = settings.namespace,
  30. selector = settings.selector,
  31. error = settings.error,
  32. eventNamespace = '.' + namespace,
  33. moduleNamespace = 'module-' + namespace,
  34. $module = $(this),
  35. $label = $(this).children(selector.label),
  36. $input = $(this).children(selector.input),
  37. input = $input[0],
  38. initialLoad = false,
  39. shortcutPressed = false,
  40. instance = $module.data(moduleNamespace),
  41. observer,
  42. element = this,
  43. module
  44. ;
  45. module = {
  46. initialize: function() {
  47. module.verbose('Initializing checkbox', settings);
  48. module.create.label();
  49. module.bind.events();
  50. module.set.tabbable();
  51. module.hide.input();
  52. module.observeChanges();
  53. module.instantiate();
  54. module.setup();
  55. },
  56. instantiate: function() {
  57. module.verbose('Storing instance of module', module);
  58. instance = module;
  59. $module
  60. .data(moduleNamespace, module)
  61. ;
  62. },
  63. destroy: function() {
  64. module.verbose('Destroying module');
  65. module.unbind.events();
  66. module.show.input();
  67. $module.removeData(moduleNamespace);
  68. },
  69. fix: {
  70. reference: function() {
  71. if( $module.is(selector.input) ) {
  72. module.debug('Behavior called on <input> adjusting invoked element');
  73. $module = $module.closest(selector.checkbox);
  74. module.refresh();
  75. }
  76. }
  77. },
  78. setup: function() {
  79. module.set.initialLoad();
  80. if( module.is.indeterminate() ) {
  81. module.debug('Initial value is indeterminate');
  82. module.indeterminate();
  83. }
  84. else if( module.is.checked() ) {
  85. module.debug('Initial value is checked');
  86. module.check();
  87. }
  88. else {
  89. module.debug('Initial value is unchecked');
  90. module.uncheck();
  91. }
  92. module.remove.initialLoad();
  93. },
  94. refresh: function() {
  95. $label = $module.children(selector.label);
  96. $input = $module.children(selector.input);
  97. input = $input[0];
  98. },
  99. hide: {
  100. input: function() {
  101. module.verbose('Modfying <input> z-index to be unselectable');
  102. $input.addClass(className.hidden);
  103. }
  104. },
  105. show: {
  106. input: function() {
  107. module.verbose('Modfying <input> z-index to be selectable');
  108. $input.removeClass(className.hidden);
  109. }
  110. },
  111. observeChanges: function() {
  112. if('MutationObserver' in window) {
  113. observer = new MutationObserver(function(mutations) {
  114. module.debug('DOM tree modified, updating selector cache');
  115. module.refresh();
  116. });
  117. observer.observe(element, {
  118. childList : true,
  119. subtree : true
  120. });
  121. module.debug('Setting up mutation observer', observer);
  122. }
  123. },
  124. attachEvents: function(selector, event) {
  125. var
  126. $element = $(selector)
  127. ;
  128. event = $.isFunction(module[event])
  129. ? module[event]
  130. : module.toggle
  131. ;
  132. if($element.length > 0) {
  133. module.debug('Attaching checkbox events to element', selector, event);
  134. $element
  135. .on('click' + eventNamespace, event)
  136. ;
  137. }
  138. else {
  139. module.error(error.notFound);
  140. }
  141. },
  142. event: {
  143. click: function(event) {
  144. var
  145. $target = $(event.target)
  146. ;
  147. if( $target.is(selector.input) ) {
  148. module.verbose('Using default check action on initialized checkbox');
  149. return;
  150. }
  151. if( $target.is(selector.link) ) {
  152. module.debug('Clicking link inside checkbox, skipping toggle');
  153. return;
  154. }
  155. module.toggle();
  156. $input.focus();
  157. event.preventDefault();
  158. },
  159. keydown: function(event) {
  160. var
  161. key = event.which,
  162. keyCode = {
  163. enter : 13,
  164. space : 32,
  165. escape : 27
  166. }
  167. ;
  168. if(key == keyCode.escape) {
  169. module.verbose('Escape key pressed blurring field');
  170. $input.blur();
  171. shortcutPressed = true;
  172. }
  173. else if(!event.ctrlKey && ( key == keyCode.space || key == keyCode.enter) ) {
  174. module.verbose('Enter/space key pressed, toggling checkbox');
  175. module.toggle();
  176. shortcutPressed = true;
  177. }
  178. else {
  179. shortcutPressed = false;
  180. }
  181. },
  182. keyup: function(event) {
  183. if(shortcutPressed) {
  184. event.preventDefault();
  185. }
  186. }
  187. },
  188. check: function() {
  189. if( !module.should.allowCheck() ) {
  190. return;
  191. }
  192. module.debug('Checking checkbox', $input);
  193. module.set.checked();
  194. if( !module.should.ignoreCallbacks() ) {
  195. settings.onChecked.call(input);
  196. settings.onChange.call(input);
  197. }
  198. },
  199. uncheck: function() {
  200. if( !module.should.allowUncheck() ) {
  201. return;
  202. }
  203. module.debug('Unchecking checkbox');
  204. module.set.unchecked();
  205. if( !module.should.ignoreCallbacks() ) {
  206. settings.onUnchecked.call(input);
  207. settings.onChange.call(input);
  208. }
  209. },
  210. indeterminate: function() {
  211. if( module.should.allowIndeterminate() ) {
  212. module.debug('Checkbox is already indeterminate');
  213. return;
  214. }
  215. module.debug('Making checkbox indeterminate');
  216. module.set.indeterminate();
  217. if( !module.should.ignoreCallbacks() ) {
  218. settings.onIndeterminate.call(input);
  219. settings.onChange.call(input);
  220. }
  221. },
  222. determinate: function() {
  223. if( module.should.allowDeterminate() ) {
  224. module.debug('Checkbox is already determinate');
  225. return;
  226. }
  227. module.debug('Making checkbox determinate');
  228. module.set.determinate();
  229. if( !module.should.ignoreCallbacks() ) {
  230. settings.onDeterminate.call(input);
  231. settings.onChange.call(input);
  232. }
  233. },
  234. enable: function() {
  235. if( module.is.enabled() ) {
  236. module.debug('Checkbox is already enabled');
  237. return;
  238. }
  239. module.debug('Enabling checkbox');
  240. module.set.enabled();
  241. settings.onEnable.call(input);
  242. },
  243. disable: function() {
  244. if( module.is.disabled() ) {
  245. module.debug('Checkbox is already disabled');
  246. return;
  247. }
  248. module.debug('Disabling checkbox');
  249. module.set.disabled();
  250. settings.onDisable.call(input);
  251. },
  252. get: {
  253. radios: function() {
  254. var
  255. name = module.get.name()
  256. ;
  257. return $('input[name="' + name + '"]').closest(selector.checkbox);
  258. },
  259. otherRadios: function() {
  260. return module.get.radios().not($module);
  261. },
  262. name: function() {
  263. return $input.attr('name');
  264. }
  265. },
  266. is: {
  267. initialLoad: function() {
  268. return initialLoad;
  269. },
  270. radio: function() {
  271. return ($input.hasClass(className.radio) || $input.attr('type') == 'radio');
  272. },
  273. indeterminate: function() {
  274. return $input.prop('indeterminate') !== undefined && $input.prop('indeterminate');
  275. },
  276. checked: function() {
  277. return $input.prop('checked') !== undefined && $input.prop('checked');
  278. },
  279. disabled: function() {
  280. return $input.prop('disabled') !== undefined && $input.prop('disabled');
  281. },
  282. enabled: function() {
  283. return !module.is.disabled();
  284. },
  285. determinate: function() {
  286. return !module.is.indeterminate();
  287. },
  288. unchecked: function() {
  289. return !module.is.checked();
  290. }
  291. },
  292. should: {
  293. allowCheck: function() {
  294. if(module.is.determinate() && module.is.checked() && !module.should.forceCallbacks() ) {
  295. module.debug('Should not allow check, checkbox is already checked');
  296. return false;
  297. }
  298. if(settings.beforeChecked.apply(input) === false) {
  299. module.debug('Should not allow check, beforeChecked cancelled');
  300. return false;
  301. }
  302. return true;
  303. },
  304. allowUncheck: function() {
  305. if(module.is.determinate() && module.is.unchecked() && !module.should.forceCallbacks() ) {
  306. module.debug('Should not allow uncheck, checkbox is already unchecked');
  307. return false;
  308. }
  309. if(settings.beforeUnchecked.apply(input) === false) {
  310. module.debug('Should not allow uncheck, beforeUnchecked cancelled');
  311. return false;
  312. }
  313. return true;
  314. },
  315. allowIndeterminate: function() {
  316. if(module.is.indeterminate() && !module.should.forceCallbacks() ) {
  317. module.debug('Should not allow indeterminate, checkbox is already indeterminate');
  318. return false;
  319. }
  320. if(settings.beforeIndeterminate.apply(input) === false) {
  321. module.debug('Should not allow indeterminate, beforeIndeterminate cancelled');
  322. return false;
  323. }
  324. return true;
  325. },
  326. allowDeterminate: function() {
  327. if(module.is.determinate() && !module.should.forceCallbacks() ) {
  328. module.debug('Should not allow determinate, checkbox is already determinate');
  329. return false;
  330. }
  331. if(settings.beforeDeterminate.apply(input) === false) {
  332. module.debug('Should not allow determinate, beforeDeterminate cancelled');
  333. return false;
  334. }
  335. return true;
  336. },
  337. forceCallbacks: function() {
  338. return (module.is.initialLoad() && settings.fireOnInit);
  339. },
  340. ignoreCallbacks: function() {
  341. return (initialLoad && !settings.fireOnInit);
  342. }
  343. },
  344. can: {
  345. change: function() {
  346. return !( $module.hasClass(className.disabled) || $module.hasClass(className.readOnly) || $input.prop('disabled') || $input.prop('readonly') );
  347. },
  348. uncheck: function() {
  349. return (typeof settings.uncheckable === 'boolean')
  350. ? settings.uncheckable
  351. : !module.is.radio()
  352. ;
  353. }
  354. },
  355. set: {
  356. initialLoad: function() {
  357. initialLoad = true;
  358. },
  359. checked: function() {
  360. module.verbose('Setting class to checked');
  361. $module
  362. .removeClass(className.indeterminate)
  363. .addClass(className.checked)
  364. ;
  365. if( module.is.radio() ) {
  366. module.uncheckOthers();
  367. }
  368. if(!module.is.indeterminate() && module.is.checked()) {
  369. module.debug('Input is already checked, skipping input property change');
  370. return;
  371. }
  372. module.verbose('Setting state to checked', input);
  373. $input
  374. .prop('indeterminate', false)
  375. .prop('checked', true)
  376. ;
  377. module.trigger.change();
  378. },
  379. unchecked: function() {
  380. module.verbose('Removing checked class');
  381. $module
  382. .removeClass(className.indeterminate)
  383. .removeClass(className.checked)
  384. ;
  385. if(!module.is.indeterminate() && module.is.unchecked() ) {
  386. module.debug('Input is already unchecked');
  387. return;
  388. }
  389. module.debug('Setting state to unchecked');
  390. $input
  391. .prop('indeterminate', false)
  392. .prop('checked', false)
  393. ;
  394. module.trigger.change();
  395. },
  396. indeterminate: function() {
  397. module.verbose('Setting class to indeterminate');
  398. $module
  399. .addClass(className.indeterminate)
  400. ;
  401. if( module.is.indeterminate() ) {
  402. module.debug('Input is already indeterminate, skipping input property change');
  403. return;
  404. }
  405. module.debug('Setting state to indeterminate');
  406. $input
  407. .prop('indeterminate', true)
  408. ;
  409. module.trigger.change();
  410. },
  411. determinate: function() {
  412. module.verbose('Removing indeterminate class');
  413. $module
  414. .removeClass(className.indeterminate)
  415. ;
  416. if( module.is.determinate() ) {
  417. module.debug('Input is already determinate, skipping input property change');
  418. return;
  419. }
  420. module.debug('Setting state to determinate');
  421. $input
  422. .prop('indeterminate', false)
  423. ;
  424. },
  425. disabled: function() {
  426. module.verbose('Setting class to disabled');
  427. $module
  428. .addClass(className.disabled)
  429. ;
  430. if( module.is.disabled() ) {
  431. module.debug('Input is already disabled, skipping input property change');
  432. return;
  433. }
  434. module.debug('Setting state to disabled');
  435. $input
  436. .prop('disabled', 'disabled')
  437. ;
  438. module.trigger.change();
  439. },
  440. enabled: function() {
  441. module.verbose('Removing disabled class');
  442. $module.removeClass(className.disabled);
  443. if( module.is.enabled() ) {
  444. module.debug('Input is already enabled, skipping input property change');
  445. return;
  446. }
  447. module.debug('Setting state to enabled');
  448. $input
  449. .prop('disabled', false)
  450. ;
  451. module.trigger.change();
  452. },
  453. tabbable: function() {
  454. module.verbose('Adding tabindex to checkbox');
  455. if( $input.attr('tabindex') === undefined) {
  456. $input.attr('tabindex', 0);
  457. }
  458. }
  459. },
  460. remove: {
  461. initialLoad: function() {
  462. initialLoad = false;
  463. }
  464. },
  465. trigger: {
  466. change: function() {
  467. var
  468. events = document.createEvent('HTMLEvents'),
  469. inputElement = $input[0]
  470. ;
  471. if(inputElement) {
  472. module.verbose('Triggering native change event');
  473. events.initEvent('change', true, false);
  474. inputElement.dispatchEvent(events);
  475. }
  476. }
  477. },
  478. create: {
  479. label: function() {
  480. if($input.prevAll(selector.label).length > 0) {
  481. $input.prev(selector.label).detach().insertAfter($input);
  482. module.debug('Moving existing label', $label);
  483. }
  484. else if( !module.has.label() ) {
  485. $label = $('<label>').insertAfter($input);
  486. module.debug('Creating label', $label);
  487. }
  488. }
  489. },
  490. has: {
  491. label: function() {
  492. return ($label.length > 0);
  493. }
  494. },
  495. bind: {
  496. events: function() {
  497. module.verbose('Attaching checkbox events');
  498. $module
  499. .on('click' + eventNamespace, module.event.click)
  500. .on('keydown' + eventNamespace, selector.input, module.event.keydown)
  501. .on('keyup' + eventNamespace, selector.input, module.event.keyup)
  502. ;
  503. }
  504. },
  505. unbind: {
  506. events: function() {
  507. module.debug('Removing events');
  508. $module
  509. .off(eventNamespace)
  510. ;
  511. }
  512. },
  513. uncheckOthers: function() {
  514. var
  515. $radios = module.get.otherRadios()
  516. ;
  517. module.debug('Unchecking other radios', $radios);
  518. $radios.removeClass(className.checked);
  519. },
  520. toggle: function() {
  521. if( !module.can.change() ) {
  522. if(!module.is.radio()) {
  523. module.debug('Checkbox is read-only or disabled, ignoring toggle');
  524. }
  525. return;
  526. }
  527. if( module.is.indeterminate() || module.is.unchecked() ) {
  528. module.debug('Currently unchecked');
  529. module.check();
  530. }
  531. else if( module.is.checked() && module.can.uncheck() ) {
  532. module.debug('Currently checked');
  533. module.uncheck();
  534. }
  535. },
  536. setting: function(name, value) {
  537. module.debug('Changing setting', name, value);
  538. if( $.isPlainObject(name) ) {
  539. $.extend(true, settings, name);
  540. }
  541. else if(value !== undefined) {
  542. settings[name] = value;
  543. }
  544. else {
  545. return settings[name];
  546. }
  547. },
  548. internal: function(name, value) {
  549. if( $.isPlainObject(name) ) {
  550. $.extend(true, module, name);
  551. }
  552. else if(value !== undefined) {
  553. module[name] = value;
  554. }
  555. else {
  556. return module[name];
  557. }
  558. },
  559. debug: function() {
  560. if(settings.debug) {
  561. if(settings.performance) {
  562. module.performance.log(arguments);
  563. }
  564. else {
  565. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  566. module.debug.apply(console, arguments);
  567. }
  568. }
  569. },
  570. verbose: function() {
  571. if(settings.verbose && settings.debug) {
  572. if(settings.performance) {
  573. module.performance.log(arguments);
  574. }
  575. else {
  576. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  577. module.verbose.apply(console, arguments);
  578. }
  579. }
  580. },
  581. error: function() {
  582. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  583. module.error.apply(console, arguments);
  584. },
  585. performance: {
  586. log: function(message) {
  587. var
  588. currentTime,
  589. executionTime,
  590. previousTime
  591. ;
  592. if(settings.performance) {
  593. currentTime = new Date().getTime();
  594. previousTime = time || currentTime;
  595. executionTime = currentTime - previousTime;
  596. time = currentTime;
  597. performance.push({
  598. 'Name' : message[0],
  599. 'Arguments' : [].slice.call(message, 1) || '',
  600. 'Element' : element,
  601. 'Execution Time' : executionTime
  602. });
  603. }
  604. clearTimeout(module.performance.timer);
  605. module.performance.timer = setTimeout(module.performance.display, 500);
  606. },
  607. display: function() {
  608. var
  609. title = settings.name + ':',
  610. totalTime = 0
  611. ;
  612. time = false;
  613. clearTimeout(module.performance.timer);
  614. $.each(performance, function(index, data) {
  615. totalTime += data['Execution Time'];
  616. });
  617. title += ' ' + totalTime + 'ms';
  618. if(moduleSelector) {
  619. title += ' \'' + moduleSelector + '\'';
  620. }
  621. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  622. console.groupCollapsed(title);
  623. if(console.table) {
  624. console.table(performance);
  625. }
  626. else {
  627. $.each(performance, function(index, data) {
  628. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  629. });
  630. }
  631. console.groupEnd();
  632. }
  633. performance = [];
  634. }
  635. },
  636. invoke: function(query, passedArguments, context) {
  637. var
  638. object = instance,
  639. maxDepth,
  640. found,
  641. response
  642. ;
  643. passedArguments = passedArguments || queryArguments;
  644. context = element || context;
  645. if(typeof query == 'string' && object !== undefined) {
  646. query = query.split(/[\. ]/);
  647. maxDepth = query.length - 1;
  648. $.each(query, function(depth, value) {
  649. var camelCaseValue = (depth != maxDepth)
  650. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  651. : query
  652. ;
  653. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  654. object = object[camelCaseValue];
  655. }
  656. else if( object[camelCaseValue] !== undefined ) {
  657. found = object[camelCaseValue];
  658. return false;
  659. }
  660. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  661. object = object[value];
  662. }
  663. else if( object[value] !== undefined ) {
  664. found = object[value];
  665. return false;
  666. }
  667. else {
  668. module.error(error.method, query);
  669. return false;
  670. }
  671. });
  672. }
  673. if ( $.isFunction( found ) ) {
  674. response = found.apply(context, passedArguments);
  675. }
  676. else if(found !== undefined) {
  677. response = found;
  678. }
  679. if($.isArray(returnedValue)) {
  680. returnedValue.push(response);
  681. }
  682. else if(returnedValue !== undefined) {
  683. returnedValue = [returnedValue, response];
  684. }
  685. else if(response !== undefined) {
  686. returnedValue = response;
  687. }
  688. return found;
  689. }
  690. };
  691. if(methodInvoked) {
  692. if(instance === undefined) {
  693. module.initialize();
  694. }
  695. module.invoke(query);
  696. }
  697. else {
  698. if(instance !== undefined) {
  699. instance.invoke('destroy');
  700. }
  701. module.initialize();
  702. }
  703. })
  704. ;
  705. return (returnedValue !== undefined)
  706. ? returnedValue
  707. : this
  708. ;
  709. };
  710. $.fn.checkbox.settings = {
  711. name : 'Checkbox',
  712. namespace : 'checkbox',
  713. debug : false,
  714. verbose : true,
  715. performance : true,
  716. // delegated event context
  717. uncheckable : 'auto',
  718. fireOnInit : false,
  719. onChange : function(){},
  720. beforeChecked : function(){},
  721. beforeUnchecked : function(){},
  722. beforeDeterminate : function(){},
  723. beforeIndeterminate : function(){},
  724. onChecked : function(){},
  725. onUnchecked : function(){},
  726. onDeterminate : function() {},
  727. onIndeterminate : function() {},
  728. onEnabled : function(){},
  729. onDisabled : function(){},
  730. className : {
  731. checked : 'checked',
  732. indeterminate : 'indeterminate',
  733. disabled : 'disabled',
  734. hidden : 'hidden',
  735. radio : 'radio',
  736. readOnly : 'read-only'
  737. },
  738. error : {
  739. method : 'The method you called is not defined'
  740. },
  741. selector : {
  742. checkbox : '.ui.checkbox',
  743. label : 'label, .box',
  744. input : 'input[type="checkbox"], input[type="radio"]',
  745. link : 'a[href]'
  746. }
  747. };
  748. })( jQuery, window, document );