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.

630 lines
18 KiB

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