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.

804 lines
25 KiB

9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
  1. /*!
  2. * # Semantic UI 2.1.4 - 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. module.verbose('Triggering change event from programmatic change');
  468. $input
  469. .trigger('change')
  470. ;
  471. }
  472. },
  473. create: {
  474. label: function() {
  475. if($input.prevAll(selector.label).length > 0) {
  476. $input.prev(selector.label).detach().insertAfter($input);
  477. module.debug('Moving existing label', $label);
  478. }
  479. else if( !module.has.label() ) {
  480. $label = $('<label>').insertAfter($input);
  481. module.debug('Creating label', $label);
  482. }
  483. }
  484. },
  485. has: {
  486. label: function() {
  487. return ($label.length > 0);
  488. }
  489. },
  490. bind: {
  491. events: function() {
  492. module.verbose('Attaching checkbox events');
  493. $module
  494. .on('click' + eventNamespace, module.event.click)
  495. .on('keydown' + eventNamespace, selector.input, module.event.keydown)
  496. .on('keyup' + eventNamespace, selector.input, module.event.keyup)
  497. ;
  498. }
  499. },
  500. unbind: {
  501. events: function() {
  502. module.debug('Removing events');
  503. $module
  504. .off(eventNamespace)
  505. ;
  506. }
  507. },
  508. uncheckOthers: function() {
  509. var
  510. $radios = module.get.otherRadios()
  511. ;
  512. module.debug('Unchecking other radios', $radios);
  513. $radios.removeClass(className.checked);
  514. },
  515. toggle: function() {
  516. if( !module.can.change() ) {
  517. if(!module.is.radio()) {
  518. module.debug('Checkbox is read-only or disabled, ignoring toggle');
  519. }
  520. return;
  521. }
  522. if( module.is.indeterminate() || module.is.unchecked() ) {
  523. module.debug('Currently unchecked');
  524. module.check();
  525. }
  526. else if( module.is.checked() && module.can.uncheck() ) {
  527. module.debug('Currently checked');
  528. module.uncheck();
  529. }
  530. },
  531. setting: function(name, value) {
  532. module.debug('Changing setting', name, value);
  533. if( $.isPlainObject(name) ) {
  534. $.extend(true, settings, name);
  535. }
  536. else if(value !== undefined) {
  537. settings[name] = value;
  538. }
  539. else {
  540. return settings[name];
  541. }
  542. },
  543. internal: function(name, value) {
  544. if( $.isPlainObject(name) ) {
  545. $.extend(true, module, name);
  546. }
  547. else if(value !== undefined) {
  548. module[name] = value;
  549. }
  550. else {
  551. return module[name];
  552. }
  553. },
  554. debug: function() {
  555. if(settings.debug) {
  556. if(settings.performance) {
  557. module.performance.log(arguments);
  558. }
  559. else {
  560. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  561. module.debug.apply(console, arguments);
  562. }
  563. }
  564. },
  565. verbose: function() {
  566. if(settings.verbose && settings.debug) {
  567. if(settings.performance) {
  568. module.performance.log(arguments);
  569. }
  570. else {
  571. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  572. module.verbose.apply(console, arguments);
  573. }
  574. }
  575. },
  576. error: function() {
  577. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  578. module.error.apply(console, arguments);
  579. },
  580. performance: {
  581. log: function(message) {
  582. var
  583. currentTime,
  584. executionTime,
  585. previousTime
  586. ;
  587. if(settings.performance) {
  588. currentTime = new Date().getTime();
  589. previousTime = time || currentTime;
  590. executionTime = currentTime - previousTime;
  591. time = currentTime;
  592. performance.push({
  593. 'Name' : message[0],
  594. 'Arguments' : [].slice.call(message, 1) || '',
  595. 'Element' : element,
  596. 'Execution Time' : executionTime
  597. });
  598. }
  599. clearTimeout(module.performance.timer);
  600. module.performance.timer = setTimeout(module.performance.display, 500);
  601. },
  602. display: function() {
  603. var
  604. title = settings.name + ':',
  605. totalTime = 0
  606. ;
  607. time = false;
  608. clearTimeout(module.performance.timer);
  609. $.each(performance, function(index, data) {
  610. totalTime += data['Execution Time'];
  611. });
  612. title += ' ' + totalTime + 'ms';
  613. if(moduleSelector) {
  614. title += ' \'' + moduleSelector + '\'';
  615. }
  616. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  617. console.groupCollapsed(title);
  618. if(console.table) {
  619. console.table(performance);
  620. }
  621. else {
  622. $.each(performance, function(index, data) {
  623. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  624. });
  625. }
  626. console.groupEnd();
  627. }
  628. performance = [];
  629. }
  630. },
  631. invoke: function(query, passedArguments, context) {
  632. var
  633. object = instance,
  634. maxDepth,
  635. found,
  636. response
  637. ;
  638. passedArguments = passedArguments || queryArguments;
  639. context = element || context;
  640. if(typeof query == 'string' && object !== undefined) {
  641. query = query.split(/[\. ]/);
  642. maxDepth = query.length - 1;
  643. $.each(query, function(depth, value) {
  644. var camelCaseValue = (depth != maxDepth)
  645. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  646. : query
  647. ;
  648. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  649. object = object[camelCaseValue];
  650. }
  651. else if( object[camelCaseValue] !== undefined ) {
  652. found = object[camelCaseValue];
  653. return false;
  654. }
  655. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  656. object = object[value];
  657. }
  658. else if( object[value] !== undefined ) {
  659. found = object[value];
  660. return false;
  661. }
  662. else {
  663. module.error(error.method, query);
  664. return false;
  665. }
  666. });
  667. }
  668. if ( $.isFunction( found ) ) {
  669. response = found.apply(context, passedArguments);
  670. }
  671. else if(found !== undefined) {
  672. response = found;
  673. }
  674. if($.isArray(returnedValue)) {
  675. returnedValue.push(response);
  676. }
  677. else if(returnedValue !== undefined) {
  678. returnedValue = [returnedValue, response];
  679. }
  680. else if(response !== undefined) {
  681. returnedValue = response;
  682. }
  683. return found;
  684. }
  685. };
  686. if(methodInvoked) {
  687. if(instance === undefined) {
  688. module.initialize();
  689. }
  690. module.invoke(query);
  691. }
  692. else {
  693. if(instance !== undefined) {
  694. instance.invoke('destroy');
  695. }
  696. module.initialize();
  697. }
  698. })
  699. ;
  700. return (returnedValue !== undefined)
  701. ? returnedValue
  702. : this
  703. ;
  704. };
  705. $.fn.checkbox.settings = {
  706. name : 'Checkbox',
  707. namespace : 'checkbox',
  708. debug : false,
  709. verbose : true,
  710. performance : true,
  711. // delegated event context
  712. uncheckable : 'auto',
  713. fireOnInit : false,
  714. onChange : function(){},
  715. beforeChecked : function(){},
  716. beforeUnchecked : function(){},
  717. beforeDeterminate : function(){},
  718. beforeIndeterminate : function(){},
  719. onChecked : function(){},
  720. onUnchecked : function(){},
  721. onDeterminate : function() {},
  722. onIndeterminate : function() {},
  723. onEnabled : function(){},
  724. onDisabled : function(){},
  725. className : {
  726. checked : 'checked',
  727. indeterminate : 'indeterminate',
  728. disabled : 'disabled',
  729. hidden : 'hidden',
  730. radio : 'radio',
  731. readOnly : 'read-only'
  732. },
  733. error : {
  734. method : 'The method you called is not defined'
  735. },
  736. selector : {
  737. checkbox : '.ui.checkbox',
  738. label : 'label, .box',
  739. input : 'input[type="checkbox"], input[type="radio"]',
  740. link : 'a[href]'
  741. }
  742. };
  743. })( jQuery, window , document );