dimmer.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*!
  2. * # Semantic UI 2.4.2 - Dimmer
  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.dimmer = function(parameters) {
  19. var
  20. $allModules = $(this),
  21. time = new Date().getTime(),
  22. performance = [],
  23. query = arguments[0],
  24. methodInvoked = (typeof query == 'string'),
  25. queryArguments = [].slice.call(arguments, 1),
  26. returnedValue
  27. ;
  28. $allModules
  29. .each(function() {
  30. var
  31. settings = ( $.isPlainObject(parameters) )
  32. ? $.extend(true, {}, $.fn.dimmer.settings, parameters)
  33. : $.extend({}, $.fn.dimmer.settings),
  34. selector = settings.selector,
  35. namespace = settings.namespace,
  36. className = settings.className,
  37. error = settings.error,
  38. eventNamespace = '.' + namespace,
  39. moduleNamespace = 'module-' + namespace,
  40. moduleSelector = $allModules.selector || '',
  41. clickEvent = ('ontouchstart' in document.documentElement)
  42. ? 'touchstart'
  43. : 'click',
  44. $module = $(this),
  45. $dimmer,
  46. $dimmable,
  47. element = this,
  48. instance = $module.data(moduleNamespace),
  49. module
  50. ;
  51. module = {
  52. preinitialize: function() {
  53. if( module.is.dimmer() ) {
  54. $dimmable = $module.parent();
  55. $dimmer = $module;
  56. }
  57. else {
  58. $dimmable = $module;
  59. if( module.has.dimmer() ) {
  60. if(settings.dimmerName) {
  61. $dimmer = $dimmable.find(selector.dimmer).filter('.' + settings.dimmerName);
  62. }
  63. else {
  64. $dimmer = $dimmable.find(selector.dimmer);
  65. }
  66. }
  67. else {
  68. $dimmer = module.create();
  69. }
  70. }
  71. },
  72. initialize: function() {
  73. module.debug('Initializing dimmer', settings);
  74. module.bind.events();
  75. module.set.dimmable();
  76. module.instantiate();
  77. },
  78. instantiate: function() {
  79. module.verbose('Storing instance of module', module);
  80. instance = module;
  81. $module
  82. .data(moduleNamespace, instance)
  83. ;
  84. },
  85. destroy: function() {
  86. module.verbose('Destroying previous module', $dimmer);
  87. module.unbind.events();
  88. module.remove.variation();
  89. $dimmable
  90. .off(eventNamespace)
  91. ;
  92. },
  93. bind: {
  94. events: function() {
  95. if(settings.on == 'hover') {
  96. $dimmable
  97. .on('mouseenter' + eventNamespace, module.show)
  98. .on('mouseleave' + eventNamespace, module.hide)
  99. ;
  100. }
  101. else if(settings.on == 'click') {
  102. $dimmable
  103. .on(clickEvent + eventNamespace, module.toggle)
  104. ;
  105. }
  106. if( module.is.page() ) {
  107. module.debug('Setting as a page dimmer', $dimmable);
  108. module.set.pageDimmer();
  109. }
  110. if( module.is.closable() ) {
  111. module.verbose('Adding dimmer close event', $dimmer);
  112. $dimmable
  113. .on(clickEvent + eventNamespace, selector.dimmer, module.event.click)
  114. ;
  115. }
  116. }
  117. },
  118. unbind: {
  119. events: function() {
  120. $module
  121. .removeData(moduleNamespace)
  122. ;
  123. $dimmable
  124. .off(eventNamespace)
  125. ;
  126. }
  127. },
  128. event: {
  129. click: function(event) {
  130. module.verbose('Determining if event occured on dimmer', event);
  131. if( $dimmer.find(event.target).length === 0 || $(event.target).is(selector.content) ) {
  132. module.hide();
  133. event.stopImmediatePropagation();
  134. }
  135. },
  136. },
  137. addContent: function(element) {
  138. var
  139. $content = $(element)
  140. ;
  141. module.debug('Add content to dimmer', $content);
  142. if($content.parent()[0] !== $dimmer[0]) {
  143. $content.detach().appendTo($dimmer);
  144. }
  145. },
  146. create: function() {
  147. var
  148. $element = $( settings.template.dimmer() )
  149. ;
  150. if(settings.dimmerName) {
  151. module.debug('Creating named dimmer', settings.dimmerName);
  152. $element.addClass(settings.dimmerName);
  153. }
  154. $element
  155. .appendTo($dimmable)
  156. ;
  157. return $element;
  158. },
  159. show: function(callback) {
  160. callback = $.isFunction(callback)
  161. ? callback
  162. : function(){}
  163. ;
  164. module.debug('Showing dimmer', $dimmer, settings);
  165. module.set.variation();
  166. if( (!module.is.dimmed() || module.is.animating()) && module.is.enabled() ) {
  167. module.animate.show(callback);
  168. settings.onShow.call(element);
  169. settings.onChange.call(element);
  170. }
  171. else {
  172. module.debug('Dimmer is already shown or disabled');
  173. }
  174. },
  175. hide: function(callback) {
  176. callback = $.isFunction(callback)
  177. ? callback
  178. : function(){}
  179. ;
  180. if( module.is.dimmed() || module.is.animating() ) {
  181. module.debug('Hiding dimmer', $dimmer);
  182. module.animate.hide(callback);
  183. settings.onHide.call(element);
  184. settings.onChange.call(element);
  185. }
  186. else {
  187. module.debug('Dimmer is not visible');
  188. }
  189. },
  190. toggle: function() {
  191. module.verbose('Toggling dimmer visibility', $dimmer);
  192. if( !module.is.dimmed() ) {
  193. module.show();
  194. }
  195. else {
  196. module.hide();
  197. }
  198. },
  199. animate: {
  200. show: function(callback) {
  201. callback = $.isFunction(callback)
  202. ? callback
  203. : function(){}
  204. ;
  205. if(settings.useCSS && $.fn.transition !== undefined && $dimmer.transition('is supported')) {
  206. if(settings.useFlex) {
  207. module.debug('Using flex dimmer');
  208. module.remove.legacy();
  209. }
  210. else {
  211. module.debug('Using legacy non-flex dimmer');
  212. module.set.legacy();
  213. }
  214. if(settings.opacity !== 'auto') {
  215. module.set.opacity();
  216. }
  217. $dimmer
  218. .transition({
  219. displayType : settings.useFlex
  220. ? 'flex'
  221. : 'block',
  222. animation : settings.transition + ' in',
  223. queue : false,
  224. duration : module.get.duration(),
  225. useFailSafe : true,
  226. onStart : function() {
  227. module.set.dimmed();
  228. },
  229. onComplete : function() {
  230. module.set.active();
  231. callback();
  232. }
  233. })
  234. ;
  235. }
  236. else {
  237. module.verbose('Showing dimmer animation with javascript');
  238. module.set.dimmed();
  239. if(settings.opacity == 'auto') {
  240. settings.opacity = 0.8;
  241. }
  242. $dimmer
  243. .stop()
  244. .css({
  245. opacity : 0,
  246. width : '100%',
  247. height : '100%'
  248. })
  249. .fadeTo(module.get.duration(), settings.opacity, function() {
  250. $dimmer.removeAttr('style');
  251. module.set.active();
  252. callback();
  253. })
  254. ;
  255. }
  256. },
  257. hide: function(callback) {
  258. callback = $.isFunction(callback)
  259. ? callback
  260. : function(){}
  261. ;
  262. if(settings.useCSS && $.fn.transition !== undefined && $dimmer.transition('is supported')) {
  263. module.verbose('Hiding dimmer with css');
  264. $dimmer
  265. .transition({
  266. displayType : settings.useFlex
  267. ? 'flex'
  268. : 'block',
  269. animation : settings.transition + ' out',
  270. queue : false,
  271. duration : module.get.duration(),
  272. useFailSafe : true,
  273. onStart : function() {
  274. module.remove.dimmed();
  275. },
  276. onComplete : function() {
  277. module.remove.variation();
  278. module.remove.active();
  279. callback();
  280. }
  281. })
  282. ;
  283. }
  284. else {
  285. module.verbose('Hiding dimmer with javascript');
  286. module.remove.dimmed();
  287. $dimmer
  288. .stop()
  289. .fadeOut(module.get.duration(), function() {
  290. module.remove.active();
  291. $dimmer.removeAttr('style');
  292. callback();
  293. })
  294. ;
  295. }
  296. }
  297. },
  298. get: {
  299. dimmer: function() {
  300. return $dimmer;
  301. },
  302. duration: function() {
  303. if(typeof settings.duration == 'object') {
  304. if( module.is.active() ) {
  305. return settings.duration.hide;
  306. }
  307. else {
  308. return settings.duration.show;
  309. }
  310. }
  311. return settings.duration;
  312. }
  313. },
  314. has: {
  315. dimmer: function() {
  316. if(settings.dimmerName) {
  317. return ($module.find(selector.dimmer).filter('.' + settings.dimmerName).length > 0);
  318. }
  319. else {
  320. return ( $module.find(selector.dimmer).length > 0 );
  321. }
  322. }
  323. },
  324. is: {
  325. active: function() {
  326. return $dimmer.hasClass(className.active);
  327. },
  328. animating: function() {
  329. return ( $dimmer.is(':animated') || $dimmer.hasClass(className.animating) );
  330. },
  331. closable: function() {
  332. if(settings.closable == 'auto') {
  333. if(settings.on == 'hover') {
  334. return false;
  335. }
  336. return true;
  337. }
  338. return settings.closable;
  339. },
  340. dimmer: function() {
  341. return $module.hasClass(className.dimmer);
  342. },
  343. dimmable: function() {
  344. return $module.hasClass(className.dimmable);
  345. },
  346. dimmed: function() {
  347. return $dimmable.hasClass(className.dimmed);
  348. },
  349. disabled: function() {
  350. return $dimmable.hasClass(className.disabled);
  351. },
  352. enabled: function() {
  353. return !module.is.disabled();
  354. },
  355. page: function () {
  356. return $dimmable.is('body');
  357. },
  358. pageDimmer: function() {
  359. return $dimmer.hasClass(className.pageDimmer);
  360. }
  361. },
  362. can: {
  363. show: function() {
  364. return !$dimmer.hasClass(className.disabled);
  365. }
  366. },
  367. set: {
  368. opacity: function(opacity) {
  369. var
  370. color = $dimmer.css('background-color'),
  371. colorArray = color.split(','),
  372. isRGB = (colorArray && colorArray.length == 3),
  373. isRGBA = (colorArray && colorArray.length == 4)
  374. ;
  375. opacity = settings.opacity === 0 ? 0 : settings.opacity || opacity;
  376. if(isRGB || isRGBA) {
  377. colorArray[3] = opacity + ')';
  378. color = colorArray.join(',');
  379. }
  380. else {
  381. color = 'rgba(0, 0, 0, ' + opacity + ')';
  382. }
  383. module.debug('Setting opacity to', opacity);
  384. $dimmer.css('background-color', color);
  385. },
  386. legacy: function() {
  387. $dimmer.addClass(className.legacy);
  388. },
  389. active: function() {
  390. $dimmer.addClass(className.active);
  391. },
  392. dimmable: function() {
  393. $dimmable.addClass(className.dimmable);
  394. },
  395. dimmed: function() {
  396. $dimmable.addClass(className.dimmed);
  397. },
  398. pageDimmer: function() {
  399. $dimmer.addClass(className.pageDimmer);
  400. },
  401. disabled: function() {
  402. $dimmer.addClass(className.disabled);
  403. },
  404. variation: function(variation) {
  405. variation = variation || settings.variation;
  406. if(variation) {
  407. $dimmer.addClass(variation);
  408. }
  409. }
  410. },
  411. remove: {
  412. active: function() {
  413. $dimmer
  414. .removeClass(className.active)
  415. ;
  416. },
  417. legacy: function() {
  418. $dimmer.removeClass(className.legacy);
  419. },
  420. dimmed: function() {
  421. $dimmable.removeClass(className.dimmed);
  422. },
  423. disabled: function() {
  424. $dimmer.removeClass(className.disabled);
  425. },
  426. variation: function(variation) {
  427. variation = variation || settings.variation;
  428. if(variation) {
  429. $dimmer.removeClass(variation);
  430. }
  431. }
  432. },
  433. setting: function(name, value) {
  434. module.debug('Changing setting', name, value);
  435. if( $.isPlainObject(name) ) {
  436. $.extend(true, settings, name);
  437. }
  438. else if(value !== undefined) {
  439. if($.isPlainObject(settings[name])) {
  440. $.extend(true, settings[name], value);
  441. }
  442. else {
  443. settings[name] = value;
  444. }
  445. }
  446. else {
  447. return settings[name];
  448. }
  449. },
  450. internal: function(name, value) {
  451. if( $.isPlainObject(name) ) {
  452. $.extend(true, module, name);
  453. }
  454. else if(value !== undefined) {
  455. module[name] = value;
  456. }
  457. else {
  458. return module[name];
  459. }
  460. },
  461. debug: function() {
  462. if(!settings.silent && settings.debug) {
  463. if(settings.performance) {
  464. module.performance.log(arguments);
  465. }
  466. else {
  467. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  468. module.debug.apply(console, arguments);
  469. }
  470. }
  471. },
  472. verbose: function() {
  473. if(!settings.silent && settings.verbose && settings.debug) {
  474. if(settings.performance) {
  475. module.performance.log(arguments);
  476. }
  477. else {
  478. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  479. module.verbose.apply(console, arguments);
  480. }
  481. }
  482. },
  483. error: function() {
  484. if(!settings.silent) {
  485. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  486. module.error.apply(console, arguments);
  487. }
  488. },
  489. performance: {
  490. log: function(message) {
  491. var
  492. currentTime,
  493. executionTime,
  494. previousTime
  495. ;
  496. if(settings.performance) {
  497. currentTime = new Date().getTime();
  498. previousTime = time || currentTime;
  499. executionTime = currentTime - previousTime;
  500. time = currentTime;
  501. performance.push({
  502. 'Name' : message[0],
  503. 'Arguments' : [].slice.call(message, 1) || '',
  504. 'Element' : element,
  505. 'Execution Time' : executionTime
  506. });
  507. }
  508. clearTimeout(module.performance.timer);
  509. module.performance.timer = setTimeout(module.performance.display, 500);
  510. },
  511. display: function() {
  512. var
  513. title = settings.name + ':',
  514. totalTime = 0
  515. ;
  516. time = false;
  517. clearTimeout(module.performance.timer);
  518. $.each(performance, function(index, data) {
  519. totalTime += data['Execution Time'];
  520. });
  521. title += ' ' + totalTime + 'ms';
  522. if(moduleSelector) {
  523. title += ' \'' + moduleSelector + '\'';
  524. }
  525. if($allModules.length > 1) {
  526. title += ' ' + '(' + $allModules.length + ')';
  527. }
  528. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  529. console.groupCollapsed(title);
  530. if(console.table) {
  531. console.table(performance);
  532. }
  533. else {
  534. $.each(performance, function(index, data) {
  535. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  536. });
  537. }
  538. console.groupEnd();
  539. }
  540. performance = [];
  541. }
  542. },
  543. invoke: function(query, passedArguments, context) {
  544. var
  545. object = instance,
  546. maxDepth,
  547. found,
  548. response
  549. ;
  550. passedArguments = passedArguments || queryArguments;
  551. context = element || context;
  552. if(typeof query == 'string' && object !== undefined) {
  553. query = query.split(/[\. ]/);
  554. maxDepth = query.length - 1;
  555. $.each(query, function(depth, value) {
  556. var camelCaseValue = (depth != maxDepth)
  557. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  558. : query
  559. ;
  560. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  561. object = object[camelCaseValue];
  562. }
  563. else if( object[camelCaseValue] !== undefined ) {
  564. found = object[camelCaseValue];
  565. return false;
  566. }
  567. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  568. object = object[value];
  569. }
  570. else if( object[value] !== undefined ) {
  571. found = object[value];
  572. return false;
  573. }
  574. else {
  575. module.error(error.method, query);
  576. return false;
  577. }
  578. });
  579. }
  580. if ( $.isFunction( found ) ) {
  581. response = found.apply(context, passedArguments);
  582. }
  583. else if(found !== undefined) {
  584. response = found;
  585. }
  586. if($.isArray(returnedValue)) {
  587. returnedValue.push(response);
  588. }
  589. else if(returnedValue !== undefined) {
  590. returnedValue = [returnedValue, response];
  591. }
  592. else if(response !== undefined) {
  593. returnedValue = response;
  594. }
  595. return found;
  596. }
  597. };
  598. module.preinitialize();
  599. if(methodInvoked) {
  600. if(instance === undefined) {
  601. module.initialize();
  602. }
  603. module.invoke(query);
  604. }
  605. else {
  606. if(instance !== undefined) {
  607. instance.invoke('destroy');
  608. }
  609. module.initialize();
  610. }
  611. })
  612. ;
  613. return (returnedValue !== undefined)
  614. ? returnedValue
  615. : this
  616. ;
  617. };
  618. $.fn.dimmer.settings = {
  619. name : 'Dimmer',
  620. namespace : 'dimmer',
  621. silent : false,
  622. debug : false,
  623. verbose : false,
  624. performance : true,
  625. // whether should use flex layout
  626. useFlex : true,
  627. // name to distinguish between multiple dimmers in context
  628. dimmerName : false,
  629. // whether to add a variation type
  630. variation : false,
  631. // whether to bind close events
  632. closable : 'auto',
  633. // whether to use css animations
  634. useCSS : true,
  635. // css animation to use
  636. transition : 'fade',
  637. // event to bind to
  638. on : false,
  639. // overriding opacity value
  640. opacity : 'auto',
  641. // transition durations
  642. duration : {
  643. show : 500,
  644. hide : 500
  645. },
  646. onChange : function(){},
  647. onShow : function(){},
  648. onHide : function(){},
  649. error : {
  650. method : 'The method you called is not defined.'
  651. },
  652. className : {
  653. active : 'active',
  654. animating : 'animating',
  655. dimmable : 'dimmable',
  656. dimmed : 'dimmed',
  657. dimmer : 'dimmer',
  658. disabled : 'disabled',
  659. hide : 'hide',
  660. legacy : 'legacy',
  661. pageDimmer : 'page',
  662. show : 'show'
  663. },
  664. selector: {
  665. dimmer : '> .ui.dimmer',
  666. content : '.ui.dimmer > .content, .ui.dimmer > .content > .center'
  667. },
  668. template: {
  669. dimmer: function() {
  670. return $('<div />').attr('class', 'ui dimmer');
  671. }
  672. }
  673. };
  674. })( jQuery, window, document );