Files
jquery-notify/dist/notify.js
T

439 lines
13 KiB
JavaScript
Raw Normal View History

2013-05-30 16:35:16 +10:00
/** Notify.js - v0.0.1 - 2013/05/30
2013-04-04 13:13:08 +11:00
* http://notifyjs.com/
* Copyright (c) 2013 Jaime Pillora - MIT
*/
(function(window,document,undefined) {
'use strict';
2013-05-30 16:35:16 +10:00
var Notification, className, cornerElem, createElem, getAnchorElement, incr, inherit, insertCSS, opposites, parsePosition, pluginName, pluginOptions, positions, styles;
2013-04-04 13:13:08 +11:00
pluginName = 'notify';
2013-04-04 16:58:22 +11:00
className = '__' + pluginName;
2013-04-04 13:13:08 +11:00
2013-05-30 16:35:16 +10:00
positions = {
t: 'top',
m: 'middle',
b: 'bottom',
l: 'left',
c: 'center',
r: 'right'
2013-05-29 16:59:19 +10:00
};
2013-05-30 16:35:16 +10:00
opposites = {
t: 'b',
m: null,
b: 't',
2013-05-29 16:59:19 +10:00
l: 'r',
c: null,
r: 'l'
};
parsePosition = function(str) {
var pos;
pos = [];
$.each(str.split(/\W+/), function(i, word) {
var w;
2013-05-30 16:35:16 +10:00
w = word.toLowerCase().charAt(0);
if (positions[w]) {
2013-05-29 16:59:19 +10:00
return pos.push(w);
}
});
return pos;
2013-04-04 13:13:08 +11:00
};
2013-04-04 13:48:42 +11:00
styles = {
core: {
2013-04-05 11:53:48 +11:00
html: "<div class=\"" + className + "Wrapper\">\n <div class=\"" + className + "Arrow\"></div>\n <div class=\"" + className + "Container\"></div>\n</div>",
2013-05-30 16:35:16 +10:00
css: "." + className + "Corner {\n position: fixed;\n top: 0;\n right: 0;\n margin: 5px;\n z-index: 1050;\n}\n\n." + className + "Corner ." + className + "Wrapper,\n." + className + "Corner ." + className + "Container {\n position: relative;\n display: block;\n height: inherit;\n width: inherit;\n}\n\n." + className + "Wrapper {\n z-index: 1;\n position: absolute;\n display: inline-block;\n height: 0;\n width: 0;\n border: thin solid red;\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}\n\n." + className + "Arrow {\n margin-top: 2px;\n position: absolute;\n z-index: 2;\n margin-left: 10px;\n width: 0;\n height: 0;\n}\n"
2013-04-04 13:13:08 +11:00
},
2013-04-04 13:48:42 +11:00
user: {
"default": {
2013-05-30 16:35:16 +10:00
html: "<div class=\"" + className + "Default\" \n data-notify-style=\"\n color: {{color}}; \n border-color: {{color}};\n \">\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: {
2013-05-29 16:59:19 +10:00
html: "<div class=\"alert alert-error " + className + "Bootstrap\">\n <strong data-notify-text></strong>\n</div>",
2013-04-05 15:34:55 +11:00
css: "." + className + "Bootstrap {\n white-space: nowrap;\n margin: 5px !important;\n padding-left: 25px !important;\n background-repeat: no-repeat;\n background-position: 3px 7px;\n background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAtRJREFUeNqkVc1u00AQHq+dOD+0poIQfkIjalW0SEGqRMuRnHos3DjwAH0ArlyQeANOOSMeAA5VjyBxKBQhgSpVUKKQNGloFdw4cWw2jtfMOna6JOUArDTazXi/b3dm55socPqQhFka++aHBsI8GsopRJERNFlY88FCEk9Yiwf8RhgRyaHFQpPHCDmZG5oX2ui2yilkcTT1AcDsbYC1NMAyOi7zTX2Agx7A9luAl88BauiiQ/cJaZQfIpAlngDcvZZMrl8vFPK5+XktrWlx3/ehZ5r9+t6e+WVnp1pxnNIjgBe4/6dAysQc8dsmHwPcW9C0h3fW1hans1ltwJhy0GxK7XZbUlMp5Ww2eyan6+ft/f2FAqXGK4CvQk5HueFz7D6GOZtIrK+srupdx1GRBBqNBtzc2AiMr7nPplRdKhb1q6q6zjFhrklEFOUutoQ50xcX86ZlqaZpQrfbBdu2R6/G19zX6XSgh6RX5ubyHCM8nqSID6ICrGiZjGYYxojEsiw4PDwMSL5VKsC8Yf4VRYFzMzMaxwjlJSlCyAQ9l0CW44PBADzXhe7xMdi9HtTrdYjFYkDQL0cn4Xdq2/EAE+InCnvADTf2eah4Sx9vExQjkqXT6aAERICMewd/UAp/IeYANM2joxt+q5VI+ieq2i0Wg3l6DNzHwTERPgo1ko7XBXj3vdlsT2F+UuhIhYkp7u7CarkcrFOCtR3H5JiwbAIeImjT/YQKKBtGjRFCU5IUgFRe7fF4cCNVIPMYo3VKqxwjyNAXNepuopyqnld602qVsfRpEkkz+GFL1wPj6ySXBpJtWVa5xlhpcyhBNwpZHmtX8AGgfIExo0ZpzkWVTBGiXCSEaHh62/PoR0p/vHaczxXGnj4bSo+G78lELU80h1uogBwWLf5YlsPmgDEd4M236xjm+8nm4IuE/9u+/PH2JXZfbwz4zw1WbO+SQPpXfwG/BBgAhCNZiSb/pOQAAAAASUVORK5CYII=);\n}",
2013-04-04 16:58:22 +11:00
colors: {
red: '#eed3d7'
}
2013-04-04 13:48:42 +11:00
}
2013-04-04 13:13:08 +11:00
}
};
pluginOptions = {
2013-04-04 16:58:22 +11:00
autoHide: false,
autoHideDelay: 2000,
2013-04-05 15:09:44 +11:00
arrowShow: false,
2013-04-04 13:13:08 +11:00
arrowSize: 5,
2013-04-05 15:09:44 +11:00
position: 'bottom',
2013-05-30 16:35:16 +10:00
style: 'default',
2013-04-04 13:13:08 +11:00
color: 'red',
colors: {
2013-04-04 16:58:22 +11:00
red: '#b94a48',
2013-04-04 13:13:08 +11:00
green: '#33be40',
black: '#393939',
blue: '#00f'
},
2013-04-04 16:58:22 +11:00
showAnimation: 'slideDown',
2013-04-05 11:53:48 +11:00
showDuration: 400,
2013-04-04 16:58:22 +11:00
hideAnimation: 'slideUp',
2013-04-05 11:53:48 +11:00
hideDuration: 200,
offsetY: 2,
offsetX: 0
2013-04-04 13:13:08 +11:00
};
2013-04-16 15:57:38 +10:00
createElem = function(tag) {
return $("<" + tag + "></" + tag + ">");
2013-04-04 13:13:08 +11:00
};
2013-04-05 11:53:48 +11:00
inherit = function(a, b) {
var F;
F = function() {};
F.prototype = a;
return $.extend(true, new F(), b);
2013-04-04 13:13:08 +11:00
};
2013-04-16 15:57:38 +10:00
cornerElem = createElem("div").addClass("" + className + "Corner");
2013-04-04 16:58:22 +11:00
2013-04-04 13:13:08 +11:00
getAnchorElement = function(element) {
2013-04-16 15:57:38 +10:00
var radios;
2013-04-04 13:13:08 +11:00
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();
}
return element;
};
2013-05-30 16:35:16 +10:00
incr = function(obj, pos, val, useOpposite) {
var opp, temp;
if (useOpposite == null) {
useOpposite = true;
}
if (typeof val === 'string') {
val = parseInt(val, 10);
} else if (typeof val !== 'number') {
return;
}
if (isNaN(val)) {
return;
}
opp = positions[opposites[pos.charAt(0)]];
temp = pos;
if (obj[opp] !== undefined) {
if (!useOpposite) {
return;
}
pos = positions[opp.charAt(0)];
val *= -1;
}
if (obj[pos] === undefined) {
obj[pos] = val;
} else {
obj[pos] += val;
}
console.log("incr (" + opp + ">>" + temp + ") " + pos + " by " + val);
return null;
};
2013-04-05 11:53:48 +11:00
insertCSS = function(style) {
var elem;
if (!(style && style.css)) {
return;
}
elem = style.cssElem;
2013-05-30 16:35:16 +10:00
if (elem) {
return;
2013-04-16 15:57:38 +10:00
}
2013-05-30 16:35:16 +10:00
elem = createElem("style");
$("head").append(elem);
style.cssElem = elem;
2013-04-16 15:57:38 +10:00
try {
return elem.html(style.css);
} catch (e) {
return elem[0].styleSheet.cssText = style.css;
2013-04-05 11:53:48 +11:00
}
};
2013-04-04 16:58:22 +11:00
Notification = (function() {
2013-04-04 13:13:08 +11:00
2013-04-04 16:58:22 +11:00
function Notification(elem, data, options) {
2013-04-05 11:53:48 +11:00
if (typeof options === 'string') {
2013-04-04 13:13:08 +11:00
options = {
color: options
};
}
2013-04-05 11:53:48 +11:00
this.options = inherit(pluginOptions, $.isPlainObject(options) ? options : {});
2013-04-04 13:48:42 +11:00
this.loadCSS();
this.loadHTML();
this.wrapper = $(styles.core.html);
2013-04-05 12:26:27 +11:00
this.wrapper.data(className, this);
2013-04-05 11:53:48 +11:00
this.arrow = this.wrapper.find("." + className + "Arrow");
2013-04-04 14:02:55 +11:00
this.container = this.wrapper.find("." + className + "Container");
this.container.append(this.userContainer);
2013-04-04 16:58:22 +11:00
if (elem && elem.length) {
this.elementType = elem.attr('type');
this.originalElement = elem;
this.elem = getAnchorElement(elem);
2013-04-05 12:26:27 +11:00
this.elem.data(className, this);
2013-04-04 16:58:22 +11:00
this.elem.before(this.wrapper);
} else {
this.options.arrowShow = false;
cornerElem.prepend(this.wrapper);
}
this.container.hide();
this.run(data);
2013-04-04 13:13:08 +11:00
}
2013-04-04 16:58:22 +11:00
Notification.prototype.loadCSS = function(style) {
2013-04-05 11:53:48 +11:00
var name;
if (!style) {
name = this.options.style;
style = this.getStyle(name);
2013-04-04 13:48:42 +11:00
}
2013-04-05 11:53:48 +11:00
return insertCSS(style);
2013-04-04 13:48:42 +11:00
};
2013-04-11 17:22:27 +10:00
Notification.prototype.loadHTML = function() {
var style;
style = this.getStyle();
2013-04-04 14:02:55 +11:00
this.userContainer = $(style.html);
2013-05-30 16:35:16 +10:00
this.text = this.userContainer.find('[data-notify-text]');
2013-04-04 13:48:42 +11:00
if (this.text.length === 0) {
2013-05-30 16:35:16 +10:00
throw "style: " + name + " HTML is missing the: 'data-notify-text' attribute";
2013-04-04 13:48:42 +11:00
}
return this.text.addClass("" + className + "Text");
};
2013-04-05 15:34:55 +11:00
Notification.prototype.show = function(show, callback) {
var args, elems, fn, hidden;
if (callback == null) {
callback = $.noop;
}
2013-04-04 13:48:42 +11:00
hidden = this.container.parent().parents(':hidden').length > 0;
2013-04-05 12:26:27 +11:00
elems = this.container.add(this.arrow);
2013-04-05 15:34:55 +11:00
args = [];
2013-04-04 13:13:08 +11:00
if (hidden && show) {
2013-04-05 15:34:55 +11:00
fn = 'show';
} else if (hidden && !show) {
fn = 'hide';
} else if (!hidden && show) {
fn = this.options.showAnimation;
args.push(this.options.showDuration);
} else if (!hidden && !show) {
fn = this.options.hideAnimation;
args.push(this.options.hideDuration);
2013-04-04 13:13:08 +11:00
}
2013-04-05 15:34:55 +11:00
args.push(callback);
2013-04-16 15:57:38 +10:00
return elems[fn].apply(elems, args);
2013-04-04 13:13:08 +11:00
};
2013-04-05 11:53:48 +11:00
Notification.prototype.updatePosition = function() {
2013-05-30 16:35:16 +10:00
var elementPosition, p, position, wrapperPosition;
2013-04-05 11:53:48 +11:00
if (!this.elem) {
return;
}
2013-04-04 13:13:08 +11:00
elementPosition = this.elem.position();
2013-05-30 16:35:16 +10:00
wrapperPosition = this.wrapper.position();
2013-04-05 11:53:48 +11:00
position = this.getPosition();
2013-05-30 16:35:16 +10:00
console.log(this.elem[0]);
console.log("update position", position, " elem ", elementPosition, " main ", wrapperPosition);
p = {};
switch (position[0]) {
case 'b':
incr(p, 'top', this.elem.outerHeight());
2013-04-05 11:53:48 +11:00
break;
2013-05-30 16:35:16 +10:00
case 't':
incr(p, 'bottom', 0);
break;
case 'l':
incr(p, 'right', 0);
break;
case 'r':
incr(p, 'left', this.elem.outerWidth());
2013-04-05 11:53:48 +11:00
break;
default:
throw "Unknown position: " + position;
}
2013-04-04 13:13:08 +11:00
if (!navigator.userAgent.match(/MSIE/)) {
2013-05-30 16:35:16 +10:00
incr(p, 'top', elementPosition.top - wrapperPosition.top);
2013-04-04 13:13:08 +11:00
}
2013-05-30 16:35:16 +10:00
incr(p, 'left', elementPosition.left - wrapperPosition.left);
2013-04-05 12:26:27 +11:00
return this.container.css(p);
2013-04-05 11:53:48 +11:00
};
2013-04-05 12:26:27 +11:00
Notification.prototype.updateArrow = function(p, position) {
var d, dir, size;
2013-04-05 11:53:48 +11:00
if (!(this.options.arrowShow && this.elementType !== 'radio')) {
this.arrow.hide();
2013-04-05 12:26:27 +11:00
return;
2013-04-05 11:53:48 +11:00
}
dir = arrowDirs[position];
size = this.options.arrowSize;
this.arrow.css('border-' + position, size + 'px solid ' + this.getColor());
2013-04-05 12:26:27 +11:00
this.arrow.css(p);
2013-04-05 11:53:48 +11:00
for (d in arrowDirs) {
if (d !== dir && d !== position) {
this.arrow.css('border-' + d, size + 'px solid transparent');
}
}
2013-04-05 12:26:27 +11:00
switch (position) {
case 'bottom':
p.top += size;
break;
case 'right':
p.left += size;
}
return this.arrow.show();
2013-04-05 11:53:48 +11:00
};
Notification.prototype.getPosition = function() {
2013-05-29 16:59:19 +10:00
var pos, text;
text = this.options.position;
pos = parsePosition(text);
if (pos.length === 0) {
pos.push('b');
2013-04-05 11:53:48 +11:00
}
2013-05-29 16:59:19 +10:00
if (pos.length === 1 && pos[0] === 'l' || pos[0] === 'r') {
pos.push('m');
2013-04-05 11:53:48 +11:00
}
2013-05-29 16:59:19 +10:00
if (pos.length === 1) {
pos.push('l');
}
if (!this.options.autoReposition) {
return pos;
}
throw "Not implemented";
2013-04-04 13:13:08 +11:00
};
2013-04-04 16:58:22 +11:00
Notification.prototype.getColor = function() {
var styleColors;
styleColors = this.getStyle().colors;
return (styleColors && styleColors[this.options.color]) || this.options.colors[this.options.color] || this.options.color;
2013-04-04 13:13:08 +11:00
};
2013-04-04 16:58:22 +11:00
Notification.prototype.getStyle = function(name) {
2013-04-04 13:48:42 +11:00
var style;
if (!name) {
name = this.options.style;
}
2013-04-05 11:53:48 +11:00
if (!name) {
name = 'default';
}
2013-04-04 13:48:42 +11:00
style = styles.user[name];
if (!style) {
throw "Missing style: " + name;
}
return style;
};
2013-04-05 11:53:48 +11:00
Notification.prototype.updateStyle = function() {
var _this = this;
return this.wrapper.find('[data-notify-style]').each(function(i, e) {
2013-05-29 16:59:19 +10:00
return $(e).attr('style', $(e).attr('data-notify-style').replace(/\{\{\s*color\s*\}\}/ig, _this.getColor()));
2013-04-05 11:53:48 +11:00
});
};
2013-04-04 16:58:22 +11:00
Notification.prototype.run = function(data, options) {
2013-04-05 11:53:48 +11:00
var autohideTimer,
2013-04-04 16:58:22 +11:00
_this = this;
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 16:58:22 +11:00
if (this.container && !data) {
this.show(false);
2013-04-04 13:13:08 +11:00
return;
2013-04-04 16:58:22 +11:00
} else if (!this.container && !data) {
2013-04-04 13:13:08 +11:00
return;
}
2013-04-04 16:58:22 +11:00
if ($.type(data) === 'string') {
this.text.html(data.replace('\n', '<br/>'));
2013-04-04 13:13:08 +11:00
} else {
2013-04-04 16:58:22 +11:00
this.text.empty().append(data);
2013-04-04 13:13:08 +11:00
}
2013-04-05 11:53:48 +11:00
this.updatePosition();
2013-04-04 16:58:22 +11:00
this.show(true);
if (this.options.autoHide) {
clearTimeout(this.autohideTimer);
return autohideTimer = setTimeout(function() {
return _this.show(false);
2013-04-04 13:13:08 +11:00
}, this.options.autoHideDelay);
}
};
2013-04-05 15:34:55 +11:00
Notification.prototype.destroy = function() {
var _this = this;
return this.show(false, function() {
return _this.wrapper.remove();
});
};
2013-04-04 16:58:22 +11:00
return Notification;
2013-04-04 13:13:08 +11:00
})();
$(function() {
2013-04-04 16:58:22 +11:00
$("body").append(cornerElem);
$("link").each(function() {
2013-05-29 16:59:19 +10:00
var bootstrapDetected, src;
2013-04-04 16:58:22 +11:00
src = $(this).attr('href');
2013-04-05 12:26:27 +11:00
if (src.match(/bootstrap/)) {
2013-04-05 11:53:48 +11:00
bootstrapDetected = true;
2013-04-04 16:58:22 +11:00
return false;
}
});
2013-04-05 11:53:48 +11:00
insertCSS(styles.core);
2013-04-04 16:58:22 +11:00
return $(document).on('click', "." + className + "Wrapper", function() {
2013-04-04 13:13:08 +11:00
var inst;
2013-04-05 12:26:27 +11:00
inst = $(this).data(className);
2013-04-05 15:34:55 +11:00
if (!inst) {
return;
}
if (inst.elem) {
2013-04-04 16:58:22 +11:00
return inst.show(false);
2013-04-05 15:34:55 +11:00
} else {
return inst.destroy();
2013-04-04 13:13:08 +11:00
}
});
});
2013-04-04 16:58:22 +11:00
$[pluginName] = function(elem, data, options) {
2013-04-16 15:57:38 +10:00
if ((elem && elem.nodeName) || elem.jquery) {
2013-05-29 16:59:19 +10:00
$(elem)[pluginName](data, options);
2013-04-04 16:58:22 +11:00
} else {
options = data;
data = elem;
2013-05-29 16:59:19 +10:00
new Notification(null, data, options);
2013-04-04 16:58:22 +11:00
}
2013-05-29 16:59:19 +10:00
return elem;
2013-04-04 13:13:08 +11:00
};
$[pluginName].options = function(options) {
return $.extend(pluginOptions, options);
};
2013-04-05 12:26:27 +11:00
$[pluginName].styles = function(s) {
2013-04-04 13:48:42 +11:00
return $.extend(true, styles.user, s);
2013-04-04 13:13:08 +11:00
};
2013-05-30 16:35:16 +10:00
$[pluginName].insertCSS = insertCSS;
2013-04-04 16:58:22 +11:00
$.fn[pluginName] = function(data, options) {
2013-05-29 16:59:19 +10:00
$(this).each(function() {
2013-04-04 13:13:08 +11:00
var inst;
2013-04-05 12:26:27 +11:00
inst = getAnchorElement($(this)).data(className);
2013-04-04 16:58:22 +11:00
if (inst) {
return inst.run(data, options);
2013-04-04 13:13:08 +11:00
} else {
2013-04-04 16:58:22 +11:00
return new Notification($(this), data, options);
2013-04-04 13:13:08 +11:00
}
});
2013-05-29 16:59:19 +10:00
return this;
2013-04-04 13:13:08 +11:00
};
}(window,document));