2013-04-04 13:13:08 +11:00
|
|
|
/** Notify.js - v0.0.1 - 2013/04/04
|
|
|
|
|
* http://notifyjs.com/
|
|
|
|
|
* Copyright (c) 2013 Jaime Pillora - MIT
|
|
|
|
|
*/
|
|
|
|
|
(function(window,document,undefined) {
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2013-04-04 13:48:42 +11:00
|
|
|
var Options, Prompt, arrowDirs, className, create, getAnchorElement, pluginName, pluginOptions, styles;
|
2013-04-04 13:13:08 +11:00
|
|
|
|
|
|
|
|
pluginName = 'notify';
|
|
|
|
|
|
|
|
|
|
className = '__notify';
|
|
|
|
|
|
|
|
|
|
arrowDirs = {
|
|
|
|
|
top: 'bottom',
|
|
|
|
|
bottom: 'top',
|
|
|
|
|
left: 'right',
|
|
|
|
|
right: 'left'
|
|
|
|
|
};
|
|
|
|
|
|
2013-04-04 13:48:42 +11:00
|
|
|
styles = {
|
|
|
|
|
core: {
|
2013-04-04 14:02:55 +11:00
|
|
|
html: "<div class=\"" + className + "Wrapper\">\n <div class=\"" + className + "Container\">\n </div>\n</div>",
|
2013-04-04 13:48:42 +11:00
|
|
|
css: "." + className + "Wrapper {\n z-index: 1;\n position: absolute;\n display: inline-block;\n height: 0;\n width: 0;\n}\n\n." + className + "Container {\n display: none;\n z-index: 1;\n position: absolute;\n cursor: pointer;\n}\n\n." + className + "Text {\n position: relative;\n}"
|
2013-04-04 13:13:08 +11:00
|
|
|
},
|
2013-04-04 13:48:42 +11:00
|
|
|
user: {
|
|
|
|
|
"default": {
|
2013-04-04 14:02:55 +11:00
|
|
|
html: "<div class=\"" + className + "Default\" data-notify-style=\"color: {{COLOR}}; border-color: {{COLOR}};\">\n <span data-notify=\"text\"></span>\n </div>",
|
2013-04-04 13:48:42 +11:00
|
|
|
css: "." + className + "Default {\n background: #fff;\n font-size: 11px;\n box-shadow: 0 0 6px #000;\n -moz-box-shadow: 0 0 6px #000;\n -webkit-box-shadow: 0 0 6px #000;\n padding: 4px 10px 4px 8px;\n border-radius: 6px;\n border-style: solid;\n border-width: 2px;\n -moz-border-radius: 6px;\n -webkit-border-radius: 6px;\n white-space: nowrap;\n}"
|
|
|
|
|
},
|
|
|
|
|
bootstrap: {
|
|
|
|
|
html: "<span>test</span>",
|
|
|
|
|
css: "body {\n test: 42\n}"
|
|
|
|
|
}
|
2013-04-04 13:13:08 +11:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pluginOptions = {
|
|
|
|
|
autoHidePrompt: false,
|
|
|
|
|
autoHideDelay: 10000,
|
|
|
|
|
arrowShow: true,
|
|
|
|
|
arrowSize: 5,
|
|
|
|
|
arrowPosition: 'top',
|
2013-04-04 13:48:42 +11:00
|
|
|
style: 'default',
|
2013-04-04 13:13:08 +11:00
|
|
|
color: 'red',
|
|
|
|
|
colors: {
|
|
|
|
|
red: '#ee0101',
|
|
|
|
|
green: '#33be40',
|
|
|
|
|
black: '#393939',
|
|
|
|
|
blue: '#00f'
|
|
|
|
|
},
|
|
|
|
|
showAnimation: 'fadeIn',
|
|
|
|
|
showDuration: 200,
|
|
|
|
|
hideAnimation: 'fadeOut',
|
|
|
|
|
hideDuration: 600,
|
|
|
|
|
gap: 2
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
create = function(tag) {
|
|
|
|
|
return $(document.createElement(tag));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Options = function(options) {
|
2013-04-04 13:48:42 +11:00
|
|
|
return $.extend(this, options);
|
2013-04-04 13:13:08 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Options.prototype = pluginOptions;
|
|
|
|
|
|
|
|
|
|
getAnchorElement = function(element) {
|
|
|
|
|
var fBefore, radios;
|
|
|
|
|
if (element.is('[type=radio]')) {
|
|
|
|
|
radios = element.parents('form:first').find('[type=radio]').filter(function(i, e) {
|
|
|
|
|
return $(e).attr('name') === element.attr('name');
|
|
|
|
|
});
|
|
|
|
|
element = radios.first();
|
|
|
|
|
}
|
|
|
|
|
fBefore = element.prev();
|
|
|
|
|
if (fBefore.is('span.styled,span.OBS_checkbox')) {
|
|
|
|
|
element = fBefore;
|
|
|
|
|
}
|
|
|
|
|
return element;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Prompt = (function() {
|
|
|
|
|
|
|
|
|
|
function Prompt(elem, node, options) {
|
|
|
|
|
if ($.type(options) === 'string') {
|
|
|
|
|
options = {
|
|
|
|
|
color: options
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
this.options = new Options($.isPlainObject(options) ? options : {});
|
|
|
|
|
this.elementType = elem.attr('type');
|
|
|
|
|
this.originalElement = elem;
|
|
|
|
|
this.elem = getAnchorElement(elem);
|
|
|
|
|
this.elem.data(pluginName, this);
|
2013-04-04 13:48:42 +11:00
|
|
|
this.loadCSS();
|
|
|
|
|
this.loadHTML();
|
|
|
|
|
this.wrapper = $(styles.core.html);
|
2013-04-04 14:02:55 +11:00
|
|
|
this.container = this.wrapper.find("." + className + "Container");
|
|
|
|
|
this.container.append(this.userContainer);
|
2013-04-04 13:13:08 +11:00
|
|
|
this.elem.before(this.wrapper);
|
2013-04-04 13:48:42 +11:00
|
|
|
this.container.css(this.calculateCSS());
|
2013-04-04 13:13:08 +11:00
|
|
|
this.run(node);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-04 13:48:42 +11:00
|
|
|
Prompt.prototype.loadCSS = function(style) {
|
|
|
|
|
var elem, name;
|
|
|
|
|
name = this.options.style;
|
|
|
|
|
style = this.getStyle(name);
|
|
|
|
|
elem = style.cssElem;
|
|
|
|
|
if (elem) {
|
|
|
|
|
return elem.html(style.css);
|
|
|
|
|
} else {
|
|
|
|
|
elem = create("style").attr('id', "" + name + "-styles").html(style.css);
|
|
|
|
|
$("head").append(elem);
|
|
|
|
|
return style.cssElem = elem;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Prompt.prototype.loadHTML = function(style) {
|
|
|
|
|
style = this.getStyle(name);
|
2013-04-04 14:02:55 +11:00
|
|
|
this.userContainer = $(style.html);
|
|
|
|
|
this.text = this.userContainer.find('[data-notify=text]');
|
2013-04-04 13:48:42 +11:00
|
|
|
if (this.text.length === 0) {
|
|
|
|
|
throw "style: " + name + " HTML is missing the: data-notify='text' attribute";
|
|
|
|
|
}
|
|
|
|
|
return this.text.addClass("" + className + "Text");
|
|
|
|
|
};
|
|
|
|
|
|
2013-04-04 13:13:08 +11:00
|
|
|
Prompt.prototype.buildArrow = function() {
|
|
|
|
|
var alt, d, dir, showArrow, size;
|
|
|
|
|
dir = this.options.arrowPosition;
|
|
|
|
|
size = this.options.arrowSize;
|
|
|
|
|
alt = arrowDirs[dir];
|
|
|
|
|
this.arrow = create("div");
|
|
|
|
|
this.arrow.addClass(className + 'Arrow').css({
|
|
|
|
|
'margin-top': 2 + (document.documentMode === 5 ? size * -4 : 0),
|
|
|
|
|
'position': 'relative',
|
|
|
|
|
'z-index': '2',
|
|
|
|
|
'margin-left': 10,
|
|
|
|
|
'width': 0,
|
|
|
|
|
'height': 0
|
|
|
|
|
}).css('border-' + alt, size + 'px solid ' + this.getColor());
|
|
|
|
|
for (d in arrowDirs) {
|
|
|
|
|
if (d !== dir && d !== alt) {
|
|
|
|
|
this.arrow.css('border-' + d, size + 'px solid transparent');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
showArrow = this.options.arrowShow && this.elementType !== 'radio';
|
|
|
|
|
if (showArrow) {
|
|
|
|
|
return this.arrow.show();
|
|
|
|
|
} else {
|
|
|
|
|
return this.arrow.hide();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Prompt.prototype.showMain = function(show) {
|
|
|
|
|
var hidden;
|
2013-04-04 13:48:42 +11:00
|
|
|
hidden = this.container.parent().parents(':hidden').length > 0;
|
2013-04-04 13:13:08 +11:00
|
|
|
if (hidden && show) {
|
2013-04-04 13:48:42 +11:00
|
|
|
this.container.show();
|
2013-04-04 13:13:08 +11:00
|
|
|
}
|
|
|
|
|
if (hidden && !show) {
|
2013-04-04 13:48:42 +11:00
|
|
|
this.container.hide();
|
2013-04-04 13:13:08 +11:00
|
|
|
}
|
|
|
|
|
if (!hidden && show) {
|
2013-04-04 13:48:42 +11:00
|
|
|
this.container[this.options.showAnimation](this.options.showDuration);
|
2013-04-04 13:13:08 +11:00
|
|
|
}
|
|
|
|
|
if (!hidden && !show) {
|
2013-04-04 13:48:42 +11:00
|
|
|
return this.container[this.options.hideAnimation](this.options.hideDuration);
|
2013-04-04 13:13:08 +11:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Prompt.prototype.calculateCSS = function() {
|
|
|
|
|
var elementPosition, height, left, mainPosition;
|
|
|
|
|
elementPosition = this.elem.position();
|
2013-04-04 13:48:42 +11:00
|
|
|
mainPosition = this.container.parent().position();
|
2013-04-04 13:13:08 +11:00
|
|
|
height = this.elem.outerHeight();
|
|
|
|
|
left = elementPosition.left - mainPosition.left;
|
|
|
|
|
if (!navigator.userAgent.match(/MSIE/)) {
|
|
|
|
|
height += elementPosition.top - mainPosition.top;
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
top: height + this.options.gap,
|
|
|
|
|
left: left
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Prompt.prototype.getColor = function() {
|
|
|
|
|
return this.options.colors[this.options.color] || this.options.color;
|
|
|
|
|
};
|
|
|
|
|
|
2013-04-04 13:48:42 +11:00
|
|
|
Prompt.prototype.getStyle = function(name) {
|
|
|
|
|
var style;
|
|
|
|
|
if (!name) {
|
|
|
|
|
name = this.options.style;
|
|
|
|
|
}
|
|
|
|
|
style = styles.user[name];
|
|
|
|
|
if (!style) {
|
|
|
|
|
throw "Missing style: " + name;
|
|
|
|
|
}
|
|
|
|
|
return style;
|
|
|
|
|
};
|
|
|
|
|
|
2013-04-04 13:13:08 +11:00
|
|
|
Prompt.prototype.run = function(node, options) {
|
2013-04-04 14:02:55 +11:00
|
|
|
var color, t;
|
2013-04-04 13:13:08 +11:00
|
|
|
if ($.isPlainObject(options)) {
|
|
|
|
|
$.extend(this.options, options);
|
|
|
|
|
} else if ($.type(options) === 'string') {
|
|
|
|
|
this.options.color = options;
|
|
|
|
|
}
|
2013-04-04 13:48:42 +11:00
|
|
|
if (this.container && !node) {
|
2013-04-04 13:13:08 +11:00
|
|
|
this.showMain(false);
|
|
|
|
|
return;
|
2013-04-04 13:48:42 +11:00
|
|
|
} else if (!this.container && !node) {
|
2013-04-04 13:13:08 +11:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ($.type(node) === 'string') {
|
2013-04-04 13:48:42 +11:00
|
|
|
this.text.html(node.replace('\n', '<br/>'));
|
2013-04-04 13:13:08 +11:00
|
|
|
} else {
|
2013-04-04 13:48:42 +11:00
|
|
|
this.text.empty().append(node);
|
2013-04-04 13:13:08 +11:00
|
|
|
}
|
2013-04-04 14:02:55 +11:00
|
|
|
color = this.getColor();
|
|
|
|
|
this.container.find('[data-notify-style]').each(function() {
|
|
|
|
|
var s;
|
|
|
|
|
s = $(this).attr('data-notify-style');
|
|
|
|
|
return $(this).attr('style', s.replace(/\{\{\s*COLOR\s*\}\}/ig, color));
|
2013-04-04 13:13:08 +11:00
|
|
|
});
|
|
|
|
|
if (this.arrow) {
|
|
|
|
|
this.arrow.remove();
|
|
|
|
|
}
|
|
|
|
|
this.buildArrow();
|
2013-04-04 14:02:55 +11:00
|
|
|
this.container.prepend(this.arrow);
|
2013-04-04 13:13:08 +11:00
|
|
|
this.showMain(true);
|
|
|
|
|
if (this.options.autoHidePrompt) {
|
|
|
|
|
clearTimeout(this.elem.data('mainTimer'));
|
|
|
|
|
t = setTimeout(function() {
|
|
|
|
|
return this.showMain(false);
|
|
|
|
|
}, this.options.autoHideDelay);
|
|
|
|
|
return this.elem.data('mainTimer', t);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Prompt;
|
|
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
$(function() {
|
2013-04-04 13:48:42 +11:00
|
|
|
$("head").append(create("style").html(styles.core.css));
|
2013-04-04 13:13:08 +11:00
|
|
|
return $(document).on('click', "." + className, function() {
|
|
|
|
|
var inst;
|
|
|
|
|
inst = getAnchorElement($(this)).data(pluginName);
|
|
|
|
|
if (inst != null) {
|
|
|
|
|
return inst.showMain(false);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$[pluginName] = function(elem, node, options) {
|
|
|
|
|
return $(elem)[pluginName](node, options);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$[pluginName].options = function(options) {
|
|
|
|
|
return $.extend(pluginOptions, options);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$[pluginName].addStyle = function(s) {
|
2013-04-04 13:48:42 +11:00
|
|
|
return $.extend(true, styles.user, s);
|
2013-04-04 13:13:08 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$.fn[pluginName] = function(node, options) {
|
|
|
|
|
return $(this).each(function() {
|
|
|
|
|
var inst;
|
|
|
|
|
inst = getAnchorElement($(this)).data(pluginName);
|
|
|
|
|
if (inst != null) {
|
|
|
|
|
return inst.run(node, options);
|
|
|
|
|
} else {
|
|
|
|
|
return new Prompt($(this), node, options);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}(window,document));
|