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 16:58:22 +11:00
|
|
|
var Notification, Options, arrowDirs, className, cornerElem, create, getAnchorElement, pluginName, pluginOptions, 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
|
|
|
|
|
|
|
|
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 16:58:22 +11: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\n." + className + "Wrapper {\n z-index: 1;\n position: absolute;\n display: inline-block;\n height: 0;\n width: 0;\n opacity: 0.85;\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 16:58:22 +11: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-04-04 16:58:22 +11:00
|
|
|
html: "<div class=\"alert alert-error " + className + "Bootstrap\">\n <strong>Warning!</strong> <span data-notify=\"text\"></span>\n</div>",
|
|
|
|
|
css: "." + className + "Bootstrap {\n white-space: nowrap;\n}",
|
|
|
|
|
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-04 13:13:08 +11:00
|
|
|
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: {
|
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-04 13:13:08 +11:00
|
|
|
showDuration: 200,
|
2013-04-04 16:58:22 +11:00
|
|
|
hideAnimation: 'slideUp',
|
2013-04-04 13:13:08 +11:00
|
|
|
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;
|
|
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
cornerElem = create("div").addClass("" + className + "Corner");
|
|
|
|
|
|
2013-04-04 13:13:08 +11:00
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
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-04 13:13:08 +11:00
|
|
|
if ($.type(options) === 'string') {
|
|
|
|
|
options = {
|
|
|
|
|
color: options
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
this.options = new Options($.isPlainObject(options) ? options : {});
|
2013-04-04 13:48:42 +11:00
|
|
|
this.loadCSS();
|
|
|
|
|
this.loadHTML();
|
|
|
|
|
this.wrapper = $(styles.core.html);
|
2013-04-04 16:58:22 +11:00
|
|
|
this.wrapper.data(pluginName, this);
|
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);
|
|
|
|
|
this.elem.data(pluginName, this);
|
|
|
|
|
this.elem.before(this.wrapper);
|
|
|
|
|
this.container.css(this.calculateCSS());
|
|
|
|
|
} 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-04 13:48:42 +11:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
Notification.prototype.loadHTML = function(style) {
|
2013-04-04 13:48:42 +11:00
|
|
|
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 16:58:22 +11:00
|
|
|
Notification.prototype.buildArrow = function() {
|
2013-04-04 13:13:08 +11:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
Notification.prototype.show = function(show) {
|
2013-04-04 13:13:08 +11:00
|
|
|
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
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
Notification.prototype.calculateCSS = function() {
|
2013-04-04 13:13:08 +11:00
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
style = styles.user[name];
|
|
|
|
|
if (!style) {
|
|
|
|
|
throw "Missing style: " + name;
|
|
|
|
|
}
|
|
|
|
|
return style;
|
|
|
|
|
};
|
|
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
Notification.prototype.run = function(data, options) {
|
|
|
|
|
var autohideTimer, color,
|
|
|
|
|
_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-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');
|
2013-04-04 16:58:22 +11:00
|
|
|
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 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-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() {
|
|
|
|
|
var src;
|
|
|
|
|
src = $(this).attr('href');
|
|
|
|
|
if (src.match(/bootstrap[^\/]*\.css/)) {
|
|
|
|
|
$[pluginName].options({
|
|
|
|
|
style: 'bootstrap'
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-04-04 13:48:42 +11:00
|
|
|
$("head").append(create("style").html(styles.core.css));
|
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-04 16:58:22 +11:00
|
|
|
inst = $(this).data(pluginName);
|
|
|
|
|
if (inst) {
|
|
|
|
|
return inst.show(false);
|
2013-04-04 13:13:08 +11:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
$[pluginName] = function(elem, data, options) {
|
|
|
|
|
if (elem instanceof HTMLElement || elem.jquery) {
|
|
|
|
|
return $(elem)[pluginName](data, options);
|
|
|
|
|
} else {
|
|
|
|
|
options = data;
|
|
|
|
|
data = elem;
|
|
|
|
|
return new Notification(null, data, options);
|
|
|
|
|
}
|
2013-04-04 13:13:08 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$[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
|
|
|
};
|
|
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
$.fn[pluginName] = function(data, options) {
|
2013-04-04 13:13:08 +11:00
|
|
|
return $(this).each(function() {
|
|
|
|
|
var inst;
|
|
|
|
|
inst = getAnchorElement($(this)).data(pluginName);
|
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
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}(window,document));
|