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.

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