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.
 
 
 

495 lines
14 KiB

/* ******************************
Semantic Module: Dimmer
Author: Jack Lukic
Notes: First Commit May 30, 2013
Simple plug-in which maintains the state for ui dimmer
****************************** */
;(function ( $, window, document, undefined ) {
$.fn.dimmer = function(parameters) {
var
$allModules = $(this),
settings = ( $.isPlainObject(parameters) )
? $.extend(true, {}, $.fn.dimmer.settings, parameters)
: $.fn.dimmer.settings,
selector = settings.selector,
namespace = settings.namespace,
className = settings.className,
error = settings.error,
eventNamespace = '.' + namespace,
moduleNamespace = 'module-' + namespace,
moduleSelector = $allModules.selector || '',
time = new Date().getTime(),
performance = [],
query = arguments[0],
methodInvoked = (typeof query == 'string'),
queryArguments = [].slice.call(arguments, 1),
invokedResponse
;
$allModules
.each(function() {
var
$module = $(this),
$dimmer = $module.children(selector.dimmer).first(),
element = this,
instance = $dimmer.data(moduleNamespace),
module
;
module = {
initialize: function() {
if( module.is.dimmer() ) {
$dimmer = $module;
$module = $dimmer.parent();
module.debug('Module initialized as dimmer');
}
else {
if( module.has.dimmer() ) {
$dimmer = $module.find(selector.dimmer);
module.debug('Module initialized with found dimmer');
}
else {
$dimmer = settings.template.dimmer();
$dimmer
.appendTo($module)
;
module.debug('Module initialized with created dimmer');
}
if(settings.on == 'hover') {
$module
.on('mouseenter' + eventNamespace, module.show)
.on('mouseleave' + eventNamespace, module.hide)
;
}
else if(settings.on == 'click') {
$module
.on('click' + eventNamespace, module.toggle)
;
}
}
$module
.addClass(className.dimmable)
;
if(settings.closable) {
$dimmer
.on('click', module.event.click)
;
}
module.instantiate();
},
instantiate: function() {
module.verbose('Storing instance of module');
instance = module;
$dimmer
.data(moduleNamespace, instance)
;
},
destroy: function() {
module.verbose('Destroying previous module');
$module
.off(namespace)
;
},
event: {
click: function(event) {
module.verbose('Determining if event occured on dimmer', event);
if( $dimmer.find(event.target).size() === 0 || $(event.target).is(selector.content) ) {
module.hide();
}
}
},
animate: {
show: function() {
module.set.dimmed();
if(settings.animation.show == 'css') {
module.verbose('Showing dimmer animation with css');
$dimmer
.one(module.get.animationEndEvent(), function() {
module.set.active();
$dimmer.removeClass(className.show);
})
.addClass(className.show)
;
}
else if(settings.animation.show == 'fade') {
module.verbose('Showing dimmer animation with javascript');
$dimmer
.stop()
.css({
opacity : 0,
width : '100%',
height : '100%'
})
.fadeTo(settings.duration, 1, function() {
$dimmer.removeAttr('style');
module.set.active();
})
;
}
},
hide: function() {
module.remove.dimmed();
if(settings.animation.hide == 'css') {
module.verbose('Hiding dimmer with css');
$dimmer
.one(module.get.animationEndEvent(), function(){
module.remove.active();
$dimmer.removeClass(className.hide);
})
.addClass(className.hide)
;
}
else if(settings.animation.hide == 'fade') {
module.verbose('Hiding dimmer with javascript');
$dimmer
.stop()
.fadeOut( (settings.duration * 0.75) , function() {
$dimmer.removeAttr('style');
module.remove.active();
})
;
}
else if( $.isFunction(settings.animation.hide) ) {
$.proxy(settings.animation.hide, $dimmer)();
}
}
},
get: {
animationEndEvent: function() {
var
element = document.createElement('element'),
animations = {
'animation' : 'animationend',
'OAnimation' : 'oAnimationEnd',
'MozAnimation' : 'animationend',
'WebkitAnimation' : 'webkitAnimationEnd'
},
animation
;
for(animation in animations){
if( element.style[animation] !== undefined ){
return animations[animation];
}
}
}
},
has: {
dimmer: function() {
return ( $module.children(selector.dimmer).size() > 0 );
}
},
is: {
animating: function() {
return ( $dimmer.hasClass(className.show) || $dimmer.hasClass(className.hide) || $dimmer.is(':animated') );
},
dimmer: function() {
return $module.is(selector.dimmer);
},
pageDimmer: function() {
return $module.is(selector.pageDimmer);
},
dimmable: function() {
return $module.is(selector.dimmable);
},
enabled: function() {
return !$module.hasClass(className.disabled);
},
disabled: function() {
return $module.hasClass(className.disabled);
},
active: function() {
return $dimmer.hasClass(className.active);
}
},
can: {
show: function() {
return !$dimmer.hasClass(className.disabled);
}
},
set: {
active: function() {
$dimmer.addClass(className.active);
},
dimmed: function() {
$module.addClass(className.dimmed);
},
disabled: function() {
$dimmer.addClass(className.disabled);
}
},
remove: {
active: function() {
$dimmer.removeClass(className.active);
},
dimmed: function() {
$module.removeClass(className.dimmed);
},
disabled: function() {
$dimmer.removeClass(className.disabled);
}
},
show: function() {
module.debug('Showing dimmer', $dimmer);
if( (!module.is.active() || module.is.animating() ) && module.is.enabled() ) {
module.animate.show();
$.proxy(settings.onShow, element)();
$.proxy(settings.onChange, element)();
}
else {
module.debug('Dimmer is already shown or disabled');
}
},
hide: function() {
if( module.is.active() || module.is.animating() ) {
module.debug('Hiding dimmer', $dimmer);
module.animate.hide();
$.proxy(settings.onHide, element)();
$.proxy(settings.onChange, element)();
}
else {
module.debug('Dimmer is not visible');
}
},
toggle: function() {
module.verbose('Toggling dimmer visibility', $dimmer);
if( module.is.hidden() ) {
module.show();
}
else {
module.hide();
}
},
setting: function(name, value) {
if(value !== undefined) {
if( $.isPlainObject(name) ) {
$.extend(true, settings, name);
}
else {
settings[name] = value;
}
}
else {
return settings[name];
}
},
internal: function(name, value) {
if(value !== undefined) {
if( $.isPlainObject(name) ) {
$.extend(true, module, name);
}
else {
module[name] = value;
}
}
else {
return module[name];
}
},
debug: function() {
if(settings.debug) {
if(settings.performance) {
module.performance.log(arguments);
}
else {
module.debug = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
}
}
},
verbose: function() {
if(settings.verbose && settings.debug) {
if(settings.performance) {
module.performance.log(arguments);
}
else {
module.verbose = Function.prototype.bind.call(console.info, console, settings.moduleName + ':');
}
}
},
error: function() {
module.error = Function.prototype.bind.call(console.log, console, settings.moduleName + ':');
},
performance: {
log: function(message) {
var
currentTime,
executionTime,
previousTime
;
if(settings.performance) {
currentTime = new Date().getTime();
previousTime = time || currentTime;
executionTime = currentTime - previousTime;
time = currentTime;
performance.push({
'Element' : element,
'Name' : message[0],
'Arguments' : [].slice.call(message, 1) || '',
'Execution Time' : executionTime
});
}
clearTimeout(module.performance.timer);
module.performance.timer = setTimeout(module.performance.display, 100);
},
display: function() {
var
title = settings.moduleName + ':',
totalTime = 0
;
time = false;
clearTimeout(module.performance.timer);
$.each(performance, function(index, data) {
totalTime += data['Execution Time'];
});
title += ' ' + totalTime + 'ms';
if(moduleSelector) {
title += ' \'' + moduleSelector + '\'';
}
if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
console.groupCollapsed(title);
if(console.table) {
console.table(performance);
}
else {
$.each(performance, function(index, data) {
console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
});
}
console.groupEnd();
}
performance = [];
}
},
invoke: function(query, passedArguments, context) {
var
maxDepth,
found
;
passedArguments = passedArguments || queryArguments;
context = element || context;
if(typeof query == 'string' && instance !== undefined) {
query = query.split(/[\. ]/);
maxDepth = query.length - 1;
$.each(query, function(depth, value) {
if( $.isPlainObject( instance[value] ) && (depth != maxDepth) ) {
instance = instance[value];
}
else if( instance[value] !== undefined ) {
found = instance[value];
}
else {
module.error(error.method);
}
});
}
if ( $.isFunction( found ) ) {
return found.apply(context, passedArguments);
}
return found || false;
}
};
if(methodInvoked) {
if(instance === undefined) {
module.initialize();
}
invokedResponse = module.invoke(query);
}
else {
if(instance !== undefined) {
module.destroy();
}
module.initialize();
}
})
;
return (invokedResponse)
? invokedResponse
: this
;
};
$.fn.dimmer.settings = {
moduleName : 'Dimmer',
namespace : 'dimmer',
verbose : true,
debug : true,
performance : true,
animation : {
show: 'css',
hide: 'css'
},
on : false,
closable : true,
duration : 500,
onChange : function(){},
onShow : function(){},
onHide : function(){},
error : {
method : 'The method you called is not defined.'
},
selector: {
dimmable : '.ui.dimmable',
dimmer : '.ui.dimmer',
content : '.ui.dimmer > .content, .ui.dimmer > .content > .center'
},
template: {
dimmer: function() {
return $('<div />').attr('class', 'ui dimmer');
}
},
className : {
active : 'active',
animating : 'animating',
dimmable : 'ui dimmable',
dimmed : 'dimmed',
disabled : 'disabled',
hide : 'hide',
show : 'show'
}
};
})( jQuery, window , document );