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.

462 lines
13 KiB

  1. /* ******************************
  2. Semantic Module: Dimmer
  3. Author: Jack Lukic
  4. Notes: First Commit May 30, 2013
  5. Simple plug-in which maintains the state for ui dimmer
  6. ****************************** */
  7. ;(function ( $, window, document, undefined ) {
  8. $.fn.dimmer = function(parameters) {
  9. var
  10. $allModules = $(this),
  11. settings = ( $.isPlainObject(parameters) )
  12. ? $.extend(true, {}, $.fn.dimmer.settings, parameters)
  13. : $.fn.dimmer.settings,
  14. eventNamespace = '.' + settings.namespace,
  15. moduleNamespace = 'module-' + settings.namespace,
  16. moduleSelector = $allModules.selector || '',
  17. moduleCount = $allModules.size(),
  18. time = new Date().getTime(),
  19. performance = [],
  20. query = arguments[0],
  21. methodInvoked = (typeof query == 'string'),
  22. queryArguments = [].slice.call(arguments, 1),
  23. selector = settings.selector,
  24. namespace = settings.namespace,
  25. className = settings.className,
  26. error = settings.error,
  27. invokedResponse
  28. ;
  29. $allModules
  30. .each(function() {
  31. var
  32. $module = $(this),
  33. $dimmer = $module.children(selector.dimmer).first(),
  34. animationEnd = 'animationend msAnimationEnd oAnimationEnd webkitAnimationEnd',
  35. element = this,
  36. instance = $dimmer.data('module-' + namespace),
  37. module
  38. ;
  39. module = {
  40. initialize: function() {
  41. if( module.is.dimmer() ) {
  42. $dimmer = $module;
  43. $module = $dimmer.parent();
  44. module.debug('Module initialized as dimmer', $dimmer);
  45. }
  46. else {
  47. if( module.has.dimmer() ) {
  48. $dimmer = $module.find(selector.dimmer);
  49. module.debug('Module initialized with found dimmer', $dimmer);
  50. }
  51. else {
  52. $dimmer = settings.template.dimmer();
  53. $dimmer
  54. .appendTo($module)
  55. ;
  56. module.debug('Module initialized with created dimmer', $dimmer);
  57. }
  58. }
  59. $module
  60. .addClass(className.dimmable)
  61. ;
  62. if(settings.closable) {
  63. $dimmer
  64. .on('click', module.event.click)
  65. ;
  66. }
  67. module.instantiate();
  68. },
  69. instantiate: function() {
  70. module.verbose('Storing instance of module');
  71. instance = module;
  72. $dimmer
  73. .data('module-' + namespace, instance)
  74. ;
  75. },
  76. destroy: function() {
  77. module.verbose('Destroying previous module for', $module);
  78. $module
  79. .off(namespace)
  80. ;
  81. },
  82. event: {
  83. click: function(event) {
  84. module.verbose('Determining if event occured on dimmer', event);
  85. console.log(event.target, selector.content, $(event.target).is(selector.content));
  86. if( $dimmer.find(event.target).size() === 0 || $(event.target).is(selector.content) ) {
  87. module.hide();
  88. }
  89. }
  90. },
  91. animate: {
  92. show: function() {
  93. module.set.dimmed();
  94. if(settings.animation.show == 'css') {
  95. module.verbose('Showing dimmer animation with css');
  96. $dimmer
  97. .one(animationEnd, function() {
  98. module.set.active();
  99. $dimmer.removeClass(className.show);
  100. })
  101. .addClass(className.show)
  102. ;
  103. }
  104. else if(settings.animation.show == 'fade') {
  105. module.verbose('Showing dimmer animation with javascript');
  106. $dimmer
  107. .stop()
  108. .css({
  109. opacity : 0,
  110. width : '100%',
  111. height : '100%'
  112. })
  113. .fadeTo(settings.duration, 1, function() {
  114. $dimmer.removeAttr('style');
  115. module.set.active();
  116. })
  117. ;
  118. }
  119. },
  120. hide: function() {
  121. module.remove.dimmed();
  122. if(settings.animation.hide == 'css') {
  123. module.verbose('Hiding dimmer with css');
  124. $dimmer
  125. .one(animationEnd, function(){
  126. module.remove.active();
  127. $dimmer.removeClass(className.hide);
  128. })
  129. .addClass(className.hide)
  130. ;
  131. }
  132. else if(settings.animation.hide == 'fade') {
  133. module.verbose('Hiding dimmer with javascript');
  134. $dimmer
  135. .stop()
  136. .fadeOut(settings.duration, function() {
  137. $dimmer.removeAttr('style');
  138. module.remove.active();
  139. })
  140. ;
  141. }
  142. else if( $.isFunction(settings.animation.hide) ) {
  143. $.proxy(settings.animation.hide, $dimmer)();
  144. }
  145. }
  146. },
  147. has: {
  148. dimmer: function() {
  149. return ( $module.children(selector.dimmer).size() > 0 );
  150. }
  151. },
  152. is: {
  153. animating: function() {
  154. return ( $dimmer.hasClass(className.show) || $dimmer.hasClass(className.hide) || $dimmer.is(':animated') );
  155. },
  156. dimmer: function() {
  157. return $module.is(selector.dimmer);
  158. },
  159. pageDimmer: function() {
  160. return $module.is(selector.pageDimmer);
  161. },
  162. dimmable: function() {
  163. return $module.is(selector.dimmable);
  164. },
  165. enabled: function() {
  166. return !$module.hasClass(className.disabled);
  167. },
  168. disabled: function() {
  169. return $module.hasClass(className.disabled);
  170. },
  171. active: function() {
  172. return $dimmer.hasClass(className.active);
  173. }
  174. },
  175. can: {
  176. show: function() {
  177. return !$dimmer.hasClass(className.disabled);
  178. }
  179. },
  180. set: {
  181. active: function() {
  182. $dimmer.addClass(className.active);
  183. },
  184. dimmed: function() {
  185. $module.addClass(className.dimmed);
  186. },
  187. disabled: function() {
  188. $dimmer.addClass(className.disabled);
  189. }
  190. },
  191. remove: {
  192. active: function() {
  193. $dimmer.removeClass(className.active);
  194. },
  195. dimmed: function() {
  196. $module.removeClass(className.dimmed);
  197. },
  198. disabled: function() {
  199. $dimmer.removeClass(className.disabled);
  200. }
  201. },
  202. show: function() {
  203. module.debug('Showing dimmer', $dimmer);
  204. if( !module.is.active() && module.is.enabled() ) {
  205. module.animate.show();
  206. $.proxy(settings.onShow, element)();
  207. $.proxy(settings.onChange, element)();
  208. }
  209. else {
  210. module.debug('Dimmer is already shown or disabled');
  211. }
  212. },
  213. hide: function() {
  214. if( module.is.active() ) {
  215. module.debug('Hiding dimmer', $dimmer);
  216. module.animate.hide();
  217. $.proxy(settings.onHide, element)();
  218. $.proxy(settings.onChange, element)();
  219. }
  220. else {
  221. module.debug('Dimmer is not visible');
  222. }
  223. },
  224. toggle: function() {
  225. module.verbose('Toggling dimmer visibility', $dimmer);
  226. if( module.is.hidden() ) {
  227. module.show();
  228. }
  229. else {
  230. module.hide();
  231. }
  232. },
  233. setting: function(name, value) {
  234. if(value !== undefined) {
  235. if( $.isPlainObject(name) ) {
  236. $.extend(true, settings, name);
  237. }
  238. else {
  239. settings[name] = value;
  240. }
  241. }
  242. else {
  243. return settings[name];
  244. }
  245. },
  246. internal: function(name, value) {
  247. if(value !== undefined) {
  248. if( $.isPlainObject(name) ) {
  249. $.extend(true, module, name);
  250. }
  251. else {
  252. module[name] = value;
  253. }
  254. }
  255. else {
  256. return module[name];
  257. }
  258. },
  259. debug: function() {
  260. if(settings.debug) {
  261. if(settings.performance) {
  262. module.performance.log(arguments);
  263. }
  264. else {
  265. module.debug = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
  266. }
  267. }
  268. },
  269. verbose: function() {
  270. if(settings.verbose && settings.debug) {
  271. if(settings.performance) {
  272. module.performance.log(arguments);
  273. }
  274. else {
  275. module.verbose = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
  276. }
  277. }
  278. },
  279. error: function() {
  280. module.error = Function.prototype.bind.call(console.log, console, settings.moduleName + ':');
  281. },
  282. performance: {
  283. log: function(message) {
  284. var
  285. currentTime,
  286. executionTime,
  287. previousTime
  288. ;
  289. if(settings.performance) {
  290. currentTime = new Date().getTime();
  291. previousTime = time || currentTime,
  292. executionTime = currentTime - previousTime;
  293. time = currentTime;
  294. performance.push({
  295. 'Element' : element,
  296. 'Name' : message[0],
  297. 'Arguments' : message[1] || '',
  298. 'Execution Time' : executionTime
  299. });
  300. }
  301. clearTimeout(module.performance.timer);
  302. module.performance.timer = setTimeout(module.performance.display, 100);
  303. },
  304. display: function() {
  305. var
  306. title = settings.moduleName + ':',
  307. totalTime = 0
  308. ;
  309. time = false;
  310. $.each(performance, function(index, data) {
  311. totalTime += data['Execution Time'];
  312. });
  313. title += ' ' + totalTime + 'ms';
  314. if(moduleSelector) {
  315. title += ' \'' + moduleSelector + '\'';
  316. }
  317. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  318. console.groupCollapsed(title);
  319. if(console.table) {
  320. console.table(performance);
  321. }
  322. else {
  323. $.each(performance, function(index, data) {
  324. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  325. });
  326. }
  327. console.groupEnd();
  328. }
  329. performance = [];
  330. }
  331. },
  332. invoke: function(query, passedArguments, context) {
  333. var
  334. maxDepth,
  335. found
  336. ;
  337. passedArguments = passedArguments || queryArguments;
  338. context = element || context;
  339. if(typeof query == 'string' && instance !== undefined) {
  340. query = query.split('.');
  341. maxDepth = query.length - 1;
  342. $.each(query, function(depth, value) {
  343. if( $.isPlainObject( instance[value] ) && (depth != maxDepth) ) {
  344. instance = instance[value];
  345. }
  346. else if( instance[value] !== undefined ) {
  347. found = instance[value];
  348. }
  349. else {
  350. module.error(error.method);
  351. }
  352. });
  353. }
  354. if ( $.isFunction( found ) ) {
  355. instance.verbose('Executing invoked function', found);
  356. return found.apply(context, passedArguments);
  357. }
  358. return found || false;
  359. }
  360. };
  361. if(methodInvoked) {
  362. if(instance === undefined) {
  363. module.initialize();
  364. }
  365. invokedResponse = module.invoke(query);
  366. }
  367. else {
  368. if(instance === undefined) {
  369. module.destroy();
  370. }
  371. module.initialize();
  372. }
  373. })
  374. ;
  375. return (invokedResponse)
  376. ? invokedResponse
  377. : this
  378. ;
  379. };
  380. $.fn.dimmer.settings = {
  381. moduleName : 'Dimmer',
  382. namespace : 'dimmer',
  383. verbose : true,
  384. debug : true,
  385. performance : true,
  386. animation : {
  387. show: 'fade',
  388. hide: 'fade'
  389. },
  390. closable : true,
  391. duration : 500,
  392. onChange : function(){},
  393. onShow : function(){},
  394. onHide : function(){},
  395. error : {
  396. method : 'The method you called is not defined.'
  397. },
  398. selector: {
  399. dimmable : '.ui.dimmable',
  400. dimmer : '.ui.dimmer',
  401. content : '.ui.dimmer > .content'
  402. },
  403. template: {
  404. dimmer: function() {
  405. return $('<div />').attr('class', 'ui dimmer');
  406. }
  407. },
  408. className : {
  409. active : 'active',
  410. animating : 'animating',
  411. dimmable : 'ui dimmable',
  412. dimmed : 'dimmed',
  413. disabled : 'disabled',
  414. hide : 'hide',
  415. show : 'show'
  416. }
  417. };
  418. })( jQuery, window , document );