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.

709 lines
20 KiB

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