state.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*!
  2. * # Semantic UI 2.3.0 - State
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Released under the MIT license
  7. * http://opensource.org/licenses/MIT
  8. *
  9. */
  10. ;(function ($, window, document, undefined) {
  11. "use strict";
  12. window = (typeof window != 'undefined' && window.Math == Math)
  13. ? window
  14. : (typeof self != 'undefined' && self.Math == Math)
  15. ? self
  16. : Function('return this')()
  17. ;
  18. $.fn.state = function(parameters) {
  19. var
  20. $allModules = $(this),
  21. moduleSelector = $allModules.selector || '',
  22. hasTouch = ('ontouchstart' in document.documentElement),
  23. time = new Date().getTime(),
  24. performance = [],
  25. query = arguments[0],
  26. methodInvoked = (typeof query == 'string'),
  27. queryArguments = [].slice.call(arguments, 1),
  28. returnedValue
  29. ;
  30. $allModules
  31. .each(function() {
  32. var
  33. settings = ( $.isPlainObject(parameters) )
  34. ? $.extend(true, {}, $.fn.state.settings, parameters)
  35. : $.extend({}, $.fn.state.settings),
  36. error = settings.error,
  37. metadata = settings.metadata,
  38. className = settings.className,
  39. namespace = settings.namespace,
  40. states = settings.states,
  41. text = settings.text,
  42. eventNamespace = '.' + namespace,
  43. moduleNamespace = namespace + '-module',
  44. $module = $(this),
  45. element = this,
  46. instance = $module.data(moduleNamespace),
  47. module
  48. ;
  49. module = {
  50. initialize: function() {
  51. module.verbose('Initializing module');
  52. // allow module to guess desired state based on element
  53. if(settings.automatic) {
  54. module.add.defaults();
  55. }
  56. // bind events with delegated events
  57. if(settings.context && moduleSelector !== '') {
  58. $(settings.context)
  59. .on(moduleSelector, 'mouseenter' + eventNamespace, module.change.text)
  60. .on(moduleSelector, 'mouseleave' + eventNamespace, module.reset.text)
  61. .on(moduleSelector, 'click' + eventNamespace, module.toggle.state)
  62. ;
  63. }
  64. else {
  65. $module
  66. .on('mouseenter' + eventNamespace, module.change.text)
  67. .on('mouseleave' + eventNamespace, module.reset.text)
  68. .on('click' + eventNamespace, module.toggle.state)
  69. ;
  70. }
  71. module.instantiate();
  72. },
  73. instantiate: function() {
  74. module.verbose('Storing instance of module', module);
  75. instance = module;
  76. $module
  77. .data(moduleNamespace, module)
  78. ;
  79. },
  80. destroy: function() {
  81. module.verbose('Destroying previous module', instance);
  82. $module
  83. .off(eventNamespace)
  84. .removeData(moduleNamespace)
  85. ;
  86. },
  87. refresh: function() {
  88. module.verbose('Refreshing selector cache');
  89. $module = $(element);
  90. },
  91. add: {
  92. defaults: function() {
  93. var
  94. userStates = parameters && $.isPlainObject(parameters.states)
  95. ? parameters.states
  96. : {}
  97. ;
  98. $.each(settings.defaults, function(type, typeStates) {
  99. if( module.is[type] !== undefined && module.is[type]() ) {
  100. module.verbose('Adding default states', type, element);
  101. $.extend(settings.states, typeStates, userStates);
  102. }
  103. });
  104. }
  105. },
  106. is: {
  107. active: function() {
  108. return $module.hasClass(className.active);
  109. },
  110. loading: function() {
  111. return $module.hasClass(className.loading);
  112. },
  113. inactive: function() {
  114. return !( $module.hasClass(className.active) );
  115. },
  116. state: function(state) {
  117. if(className[state] === undefined) {
  118. return false;
  119. }
  120. return $module.hasClass( className[state] );
  121. },
  122. enabled: function() {
  123. return !( $module.is(settings.filter.active) );
  124. },
  125. disabled: function() {
  126. return ( $module.is(settings.filter.active) );
  127. },
  128. textEnabled: function() {
  129. return !( $module.is(settings.filter.text) );
  130. },
  131. // definitions for automatic type detection
  132. button: function() {
  133. return $module.is('.button:not(a, .submit)');
  134. },
  135. input: function() {
  136. return $module.is('input');
  137. },
  138. progress: function() {
  139. return $module.is('.ui.progress');
  140. }
  141. },
  142. allow: function(state) {
  143. module.debug('Now allowing state', state);
  144. states[state] = true;
  145. },
  146. disallow: function(state) {
  147. module.debug('No longer allowing', state);
  148. states[state] = false;
  149. },
  150. allows: function(state) {
  151. return states[state] || false;
  152. },
  153. enable: function() {
  154. $module.removeClass(className.disabled);
  155. },
  156. disable: function() {
  157. $module.addClass(className.disabled);
  158. },
  159. setState: function(state) {
  160. if(module.allows(state)) {
  161. $module.addClass( className[state] );
  162. }
  163. },
  164. removeState: function(state) {
  165. if(module.allows(state)) {
  166. $module.removeClass( className[state] );
  167. }
  168. },
  169. toggle: {
  170. state: function() {
  171. var
  172. apiRequest,
  173. requestCancelled
  174. ;
  175. if( module.allows('active') && module.is.enabled() ) {
  176. module.refresh();
  177. if($.fn.api !== undefined) {
  178. apiRequest = $module.api('get request');
  179. requestCancelled = $module.api('was cancelled');
  180. if( requestCancelled ) {
  181. module.debug('API Request cancelled by beforesend');
  182. settings.activateTest = function(){ return false; };
  183. settings.deactivateTest = function(){ return false; };
  184. }
  185. else if(apiRequest) {
  186. module.listenTo(apiRequest);
  187. return;
  188. }
  189. }
  190. module.change.state();
  191. }
  192. }
  193. },
  194. listenTo: function(apiRequest) {
  195. module.debug('API request detected, waiting for state signal', apiRequest);
  196. if(apiRequest) {
  197. if(text.loading) {
  198. module.update.text(text.loading);
  199. }
  200. $.when(apiRequest)
  201. .then(function() {
  202. if(apiRequest.state() == 'resolved') {
  203. module.debug('API request succeeded');
  204. settings.activateTest = function(){ return true; };
  205. settings.deactivateTest = function(){ return true; };
  206. }
  207. else {
  208. module.debug('API request failed');
  209. settings.activateTest = function(){ return false; };
  210. settings.deactivateTest = function(){ return false; };
  211. }
  212. module.change.state();
  213. })
  214. ;
  215. }
  216. },
  217. // checks whether active/inactive state can be given
  218. change: {
  219. state: function() {
  220. module.debug('Determining state change direction');
  221. // inactive to active change
  222. if( module.is.inactive() ) {
  223. module.activate();
  224. }
  225. else {
  226. module.deactivate();
  227. }
  228. if(settings.sync) {
  229. module.sync();
  230. }
  231. settings.onChange.call(element);
  232. },
  233. text: function() {
  234. if( module.is.textEnabled() ) {
  235. if(module.is.disabled() ) {
  236. module.verbose('Changing text to disabled text', text.hover);
  237. module.update.text(text.disabled);
  238. }
  239. else if( module.is.active() ) {
  240. if(text.hover) {
  241. module.verbose('Changing text to hover text', text.hover);
  242. module.update.text(text.hover);
  243. }
  244. else if(text.deactivate) {
  245. module.verbose('Changing text to deactivating text', text.deactivate);
  246. module.update.text(text.deactivate);
  247. }
  248. }
  249. else {
  250. if(text.hover) {
  251. module.verbose('Changing text to hover text', text.hover);
  252. module.update.text(text.hover);
  253. }
  254. else if(text.activate){
  255. module.verbose('Changing text to activating text', text.activate);
  256. module.update.text(text.activate);
  257. }
  258. }
  259. }
  260. }
  261. },
  262. activate: function() {
  263. if( settings.activateTest.call(element) ) {
  264. module.debug('Setting state to active');
  265. $module
  266. .addClass(className.active)
  267. ;
  268. module.update.text(text.active);
  269. settings.onActivate.call(element);
  270. }
  271. },
  272. deactivate: function() {
  273. if( settings.deactivateTest.call(element) ) {
  274. module.debug('Setting state to inactive');
  275. $module
  276. .removeClass(className.active)
  277. ;
  278. module.update.text(text.inactive);
  279. settings.onDeactivate.call(element);
  280. }
  281. },
  282. sync: function() {
  283. module.verbose('Syncing other buttons to current state');
  284. if( module.is.active() ) {
  285. $allModules
  286. .not($module)
  287. .state('activate');
  288. }
  289. else {
  290. $allModules
  291. .not($module)
  292. .state('deactivate')
  293. ;
  294. }
  295. },
  296. get: {
  297. text: function() {
  298. return (settings.selector.text)
  299. ? $module.find(settings.selector.text).text()
  300. : $module.html()
  301. ;
  302. },
  303. textFor: function(state) {
  304. return text[state] || false;
  305. }
  306. },
  307. flash: {
  308. text: function(text, duration, callback) {
  309. var
  310. previousText = module.get.text()
  311. ;
  312. module.debug('Flashing text message', text, duration);
  313. text = text || settings.text.flash;
  314. duration = duration || settings.flashDuration;
  315. callback = callback || function() {};
  316. module.update.text(text);
  317. setTimeout(function(){
  318. module.update.text(previousText);
  319. callback.call(element);
  320. }, duration);
  321. }
  322. },
  323. reset: {
  324. // on mouseout sets text to previous value
  325. text: function() {
  326. var
  327. activeText = text.active || $module.data(metadata.storedText),
  328. inactiveText = text.inactive || $module.data(metadata.storedText)
  329. ;
  330. if( module.is.textEnabled() ) {
  331. if( module.is.active() && activeText) {
  332. module.verbose('Resetting active text', activeText);
  333. module.update.text(activeText);
  334. }
  335. else if(inactiveText) {
  336. module.verbose('Resetting inactive text', activeText);
  337. module.update.text(inactiveText);
  338. }
  339. }
  340. }
  341. },
  342. update: {
  343. text: function(text) {
  344. var
  345. currentText = module.get.text()
  346. ;
  347. if(text && text !== currentText) {
  348. module.debug('Updating text', text);
  349. if(settings.selector.text) {
  350. $module
  351. .data(metadata.storedText, text)
  352. .find(settings.selector.text)
  353. .text(text)
  354. ;
  355. }
  356. else {
  357. $module
  358. .data(metadata.storedText, text)
  359. .html(text)
  360. ;
  361. }
  362. }
  363. else {
  364. module.debug('Text is already set, ignoring update', text);
  365. }
  366. }
  367. },
  368. setting: function(name, value) {
  369. module.debug('Changing setting', name, value);
  370. if( $.isPlainObject(name) ) {
  371. $.extend(true, settings, name);
  372. }
  373. else if(value !== undefined) {
  374. if($.isPlainObject(settings[name])) {
  375. $.extend(true, settings[name], value);
  376. }
  377. else {
  378. settings[name] = value;
  379. }
  380. }
  381. else {
  382. return settings[name];
  383. }
  384. },
  385. internal: function(name, value) {
  386. if( $.isPlainObject(name) ) {
  387. $.extend(true, module, name);
  388. }
  389. else if(value !== undefined) {
  390. module[name] = value;
  391. }
  392. else {
  393. return module[name];
  394. }
  395. },
  396. debug: function() {
  397. if(!settings.silent && settings.debug) {
  398. if(settings.performance) {
  399. module.performance.log(arguments);
  400. }
  401. else {
  402. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  403. module.debug.apply(console, arguments);
  404. }
  405. }
  406. },
  407. verbose: function() {
  408. if(!settings.silent && settings.verbose && settings.debug) {
  409. if(settings.performance) {
  410. module.performance.log(arguments);
  411. }
  412. else {
  413. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  414. module.verbose.apply(console, arguments);
  415. }
  416. }
  417. },
  418. error: function() {
  419. if(!settings.silent) {
  420. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  421. module.error.apply(console, arguments);
  422. }
  423. },
  424. performance: {
  425. log: function(message) {
  426. var
  427. currentTime,
  428. executionTime,
  429. previousTime
  430. ;
  431. if(settings.performance) {
  432. currentTime = new Date().getTime();
  433. previousTime = time || currentTime;
  434. executionTime = currentTime - previousTime;
  435. time = currentTime;
  436. performance.push({
  437. 'Name' : message[0],
  438. 'Arguments' : [].slice.call(message, 1) || '',
  439. 'Element' : element,
  440. 'Execution Time' : executionTime
  441. });
  442. }
  443. clearTimeout(module.performance.timer);
  444. module.performance.timer = setTimeout(module.performance.display, 500);
  445. },
  446. display: function() {
  447. var
  448. title = settings.name + ':',
  449. totalTime = 0
  450. ;
  451. time = false;
  452. clearTimeout(module.performance.timer);
  453. $.each(performance, function(index, data) {
  454. totalTime += data['Execution Time'];
  455. });
  456. title += ' ' + totalTime + 'ms';
  457. if(moduleSelector) {
  458. title += ' \'' + moduleSelector + '\'';
  459. }
  460. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  461. console.groupCollapsed(title);
  462. if(console.table) {
  463. console.table(performance);
  464. }
  465. else {
  466. $.each(performance, function(index, data) {
  467. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  468. });
  469. }
  470. console.groupEnd();
  471. }
  472. performance = [];
  473. }
  474. },
  475. invoke: function(query, passedArguments, context) {
  476. var
  477. object = instance,
  478. maxDepth,
  479. found,
  480. response
  481. ;
  482. passedArguments = passedArguments || queryArguments;
  483. context = element || context;
  484. if(typeof query == 'string' && object !== undefined) {
  485. query = query.split(/[\. ]/);
  486. maxDepth = query.length - 1;
  487. $.each(query, function(depth, value) {
  488. var camelCaseValue = (depth != maxDepth)
  489. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  490. : query
  491. ;
  492. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  493. object = object[camelCaseValue];
  494. }
  495. else if( object[camelCaseValue] !== undefined ) {
  496. found = object[camelCaseValue];
  497. return false;
  498. }
  499. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  500. object = object[value];
  501. }
  502. else if( object[value] !== undefined ) {
  503. found = object[value];
  504. return false;
  505. }
  506. else {
  507. module.error(error.method, query);
  508. return false;
  509. }
  510. });
  511. }
  512. if ( $.isFunction( found ) ) {
  513. response = found.apply(context, passedArguments);
  514. }
  515. else if(found !== undefined) {
  516. response = found;
  517. }
  518. if($.isArray(returnedValue)) {
  519. returnedValue.push(response);
  520. }
  521. else if(returnedValue !== undefined) {
  522. returnedValue = [returnedValue, response];
  523. }
  524. else if(response !== undefined) {
  525. returnedValue = response;
  526. }
  527. return found;
  528. }
  529. };
  530. if(methodInvoked) {
  531. if(instance === undefined) {
  532. module.initialize();
  533. }
  534. module.invoke(query);
  535. }
  536. else {
  537. if(instance !== undefined) {
  538. instance.invoke('destroy');
  539. }
  540. module.initialize();
  541. }
  542. })
  543. ;
  544. return (returnedValue !== undefined)
  545. ? returnedValue
  546. : this
  547. ;
  548. };
  549. $.fn.state.settings = {
  550. // module info
  551. name : 'State',
  552. // debug output
  553. debug : false,
  554. // verbose debug output
  555. verbose : false,
  556. // namespace for events
  557. namespace : 'state',
  558. // debug data includes performance
  559. performance : true,
  560. // callback occurs on state change
  561. onActivate : function() {},
  562. onDeactivate : function() {},
  563. onChange : function() {},
  564. // state test functions
  565. activateTest : function() { return true; },
  566. deactivateTest : function() { return true; },
  567. // whether to automatically map default states
  568. automatic : true,
  569. // activate / deactivate changes all elements instantiated at same time
  570. sync : false,
  571. // default flash text duration, used for temporarily changing text of an element
  572. flashDuration : 1000,
  573. // selector filter
  574. filter : {
  575. text : '.loading, .disabled',
  576. active : '.disabled'
  577. },
  578. context : false,
  579. // error
  580. error: {
  581. beforeSend : 'The before send function has cancelled state change',
  582. method : 'The method you called is not defined.'
  583. },
  584. // metadata
  585. metadata: {
  586. promise : 'promise',
  587. storedText : 'stored-text'
  588. },
  589. // change class on state
  590. className: {
  591. active : 'active',
  592. disabled : 'disabled',
  593. error : 'error',
  594. loading : 'loading',
  595. success : 'success',
  596. warning : 'warning'
  597. },
  598. selector: {
  599. // selector for text node
  600. text: false
  601. },
  602. defaults : {
  603. input: {
  604. disabled : true,
  605. loading : true,
  606. active : true
  607. },
  608. button: {
  609. disabled : true,
  610. loading : true,
  611. active : true,
  612. },
  613. progress: {
  614. active : true,
  615. success : true,
  616. warning : true,
  617. error : true
  618. }
  619. },
  620. states : {
  621. active : true,
  622. disabled : true,
  623. error : true,
  624. loading : true,
  625. success : true,
  626. warning : true
  627. },
  628. text : {
  629. disabled : false,
  630. flash : false,
  631. hover : false,
  632. active : false,
  633. inactive : false,
  634. activate : false,
  635. deactivate : false
  636. }
  637. };
  638. })( jQuery, window, document );