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.
 
 
 

545 lines
16 KiB

/*
* # Semantic - Nag
* http://github.com/jlukic/semantic-ui/
*
*
* Copyright 2014 Contributors
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
*/
;(function ($, window, document, undefined) {
module.exports = function(parameters) {
var _module = module;
var
$allModules = $(this),
moduleSelector = $allModules.selector || '',
time = new Date().getTime(),
performance = [],
query = arguments[0],
methodInvoked = (typeof query == 'string'),
queryArguments = [].slice.call(arguments, 1),
returnedValue
;
$(this)
.each(function() {
var
settings = $.extend(true, {}, _module.exports.settings, parameters),
className = settings.className,
selector = settings.selector,
error = settings.error,
namespace = settings.namespace,
eventNamespace = '.' + namespace,
moduleNamespace = namespace + '-module',
$module = $(this),
$close = $module.find(selector.close),
$context = $(settings.context),
element = this,
instance = $module.data(moduleNamespace),
moduleOffset,
moduleHeight,
contextWidth,
contextHeight,
contextOffset,
yOffset,
yPosition,
timer,
module,
requestAnimationFrame = window.requestAnimationFrame
|| window.mozRequestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.msRequestAnimationFrame
|| function(callback) { setTimeout(callback, 0); }
;
module = {
initialize: function() {
module.verbose('Initializing element');
// calculate module offset once
moduleOffset = $module.offset();
moduleHeight = $module.outerHeight();
contextWidth = $context.outerWidth();
contextHeight = $context.outerHeight();
contextOffset = $context.offset();
$module
.data(moduleNamespace, module)
;
$close
.on('click' + eventNamespace, module.dismiss)
;
// lets avoid javascript if we dont need to reposition
if(settings.context == window && settings.position == 'fixed') {
$module
.addClass(className.fixed)
;
}
if(settings.sticky) {
module.verbose('Adding scroll events');
// retrigger on scroll for absolute
if(settings.position == 'absolute') {
$context
.on('scroll' + eventNamespace, module.event.scroll)
.on('resize' + eventNamespace, module.event.scroll)
;
}
// fixed is always relative to window
else {
$(window)
.on('scroll' + eventNamespace, module.event.scroll)
.on('resize' + eventNamespace, module.event.scroll)
;
}
// fire once to position on init
$.proxy(module.event.scroll, this)();
}
if(settings.displayTime > 0) {
setTimeout(module.hide, settings.displayTime);
}
if(module.should.show()) {
if( !$module.is(':visible') ) {
module.show();
}
}
else {
module.hide();
}
},
destroy: function() {
module.verbose('Destroying instance');
$module
.removeData(moduleNamespace)
.off(eventNamespace)
;
if(settings.sticky) {
$context
.off(eventNamespace)
;
}
},
refresh: function() {
module.debug('Refreshing cached calculations');
moduleOffset = $module.offset();
moduleHeight = $module.outerHeight();
contextWidth = $context.outerWidth();
contextHeight = $context.outerHeight();
contextOffset = $context.offset();
},
show: function() {
module.debug('Showing nag', settings.animation.show);
if(settings.animation.show == 'fade') {
$module
.fadeIn(settings.duration, settings.easing)
;
}
else {
$module
.slideDown(settings.duration, settings.easing)
;
}
},
hide: function() {
module.debug('Showing nag', settings.animation.hide);
if(settings.animation.show == 'fade') {
$module
.fadeIn(settings.duration, settings.easing)
;
}
else {
$module
.slideUp(settings.duration, settings.easing)
;
}
},
onHide: function() {
module.debug('Removing nag', settings.animation.hide);
$module.remove();
if (settings.onHide) {
settings.onHide();
}
},
stick: function() {
module.refresh();
if(settings.position == 'fixed') {
var
windowScroll = $(window).prop('pageYOffset') || $(window).scrollTop(),
fixedOffset = ( $module.hasClass(className.bottom) )
? contextOffset.top + (contextHeight - moduleHeight) - windowScroll
: contextOffset.top - windowScroll
;
$module
.css({
position : 'fixed',
top : fixedOffset,
left : contextOffset.left,
width : contextWidth - settings.scrollBarWidth
})
;
}
else {
$module
.css({
top : yPosition
})
;
}
},
unStick: function() {
$module
.css({
top : ''
})
;
},
dismiss: function(event) {
if(settings.storageMethod) {
module.storage.set(settings.storedKey, settings.storedValue);
}
module.hide();
event.stopImmediatePropagation();
event.preventDefault();
},
should: {
show: function() {
if(settings.persist) {
module.debug('Persistent nag is set, can show nag');
return true;
}
if(module.storage.get(settings.storedKey) != settings.storedValue) {
module.debug('Stored value is not set, can show nag', module.storage.get(settings.storedKey));
return true;
}
module.debug('Stored value is set, cannot show nag', module.storage.get(settings.storedKey));
return false;
},
stick: function() {
yOffset = $context.prop('pageYOffset') || $context.scrollTop();
yPosition = ( $module.hasClass(className.bottom) )
? (contextHeight - $module.outerHeight() ) + yOffset
: yOffset
;
// absolute position calculated when y offset met
if(yPosition > moduleOffset.top) {
return true;
}
else if(settings.position == 'fixed') {
return true;
}
return false;
}
},
storage: {
set: function(key, value) {
module.debug('Setting stored value', key, value, settings.storageMethod);
if(settings.storageMethod == 'local' && window.store !== undefined) {
window.store.set(key, value);
}
// store by cookie
else if($.cookie !== undefined) {
$.cookie(key, value);
}
else {
module.error(error.noStorage);
}
},
get: function(key) {
module.debug('Getting stored value', key, settings.storageMethod);
if(settings.storageMethod == 'local' && window.store !== undefined) {
return window.store.get(key);
}
// get by cookie
else if($.cookie !== undefined) {
return $.cookie(key);
}
else {
module.error(error.noStorage);
}
}
},
event: {
scroll: function() {
if(timer !== undefined) {
clearTimeout(timer);
}
timer = setTimeout(function() {
if(module.should.stick() ) {
requestAnimationFrame(module.stick);
}
else {
module.unStick();
}
}, settings.lag);
}
},
setting: function(name, value) {
if( $.isPlainObject(name) ) {
$.extend(true, settings, name);
}
else if(value !== undefined) {
settings[name] = value;
}
else {
return settings[name];
}
},
internal: function(name, value) {
module.debug('Changing internal', 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.name + ':');
module.debug.apply(console, arguments);
}
}
},
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.name + ':');
module.verbose.apply(console, arguments);
}
}
},
error: function() {
module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
module.error.apply(console, arguments);
},
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.name + ':',
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($allModules.size() > 1) {
title += ' ' + '(' + $allModules.size() + ')';
}
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
object = instance,
maxDepth,
found,
response
;
passedArguments = passedArguments || queryArguments;
context = element || context;
if(typeof query == 'string' && object !== undefined) {
query = query.split(/[\. ]/);
maxDepth = query.length - 1;
$.each(query, function(depth, value) {
var camelCaseValue = (depth != maxDepth)
? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
: query
;
if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
object = object[camelCaseValue];
}
else if( object[camelCaseValue] !== undefined ) {
found = object[camelCaseValue];
return false;
}
else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
object = object[value];
}
else if( object[value] !== undefined ) {
found = object[value];
return false;
}
else {
return false;
}
});
}
if ( $.isFunction( found ) ) {
response = found.apply(context, passedArguments);
}
else if(found !== undefined) {
response = found;
}
if($.isArray(returnedValue)) {
returnedValue.push(response);
}
else if(returnedValue !== undefined) {
returnedValue = [returnedValue, response];
}
else if(response !== undefined) {
returnedValue = response;
}
return found;
}
};
if(methodInvoked) {
if(instance === undefined) {
module.initialize();
}
module.invoke(query);
}
else {
if(instance !== undefined) {
module.destroy();
}
module.initialize();
}
})
;
return (returnedValue !== undefined)
? returnedValue
: this
;
};
module.exports.settings = {
name : 'Nag',
debug : false,
verbose : true,
performance : true,
namespace : 'Nag',
// allows cookie to be overriden
persist : false,
// set to zero to manually dismiss, otherwise hides on its own
displayTime : 0,
animation : {
show: 'slide',
hide: 'slide'
},
// method of stickyness
position : 'fixed',
scrollBarWidth : 18,
// type of storage to use
storageMethod : 'cookie',
// value to store in dismissed localstorage/cookie
storedKey : 'nag',
storedValue : 'dismiss',
// need to calculate stickyness on scroll
sticky : false,
// how often to check scroll event
lag : 0,
// context for scroll event
context : window,
error: {
noStorage : 'Neither $.cookie or store is defined. A storage solution is required for storing state',
method : 'The method you called is not defined.'
},
className : {
bottom : 'bottom',
fixed : 'fixed'
},
selector : {
close: '.icon.close'
},
speed : 500,
easing : 'easeOutQuad',
onHide: function() {}
};
})( require("jquery"), window , document );