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.

549 lines
15 KiB

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