diff --git a/dist/notify.js b/dist/notify.js index 7055dd2..1252f7f 100644 --- a/dist/notify.js +++ b/dist/notify.js @@ -1,27 +1,48 @@ -/** Notify.js - v0.0.1 - 2013/05/28 +/** Notify.js - v0.0.1 - 2013/05/29 * http://notifyjs.com/ * Copyright (c) 2013 Jaime Pillora - MIT */ (function(window,document,undefined) { 'use strict'; -var Notification, arrowDirs, bootstrapDetected, className, cornerElem, createElem, getAnchorElement, inherit, insertCSS, pluginName, pluginOptions, styles; +var Notification, className, cornerElem, createElem, getAnchorElement, hPositions, inherit, insertCSS, parsePosition, pluginName, pluginOptions, styles, vPositions; pluginName = 'notify'; className = '__' + pluginName; -arrowDirs = { - top: 'bottom', - bottom: 'top', - left: 'right', - right: 'left' +vPositions = { + t: 'b', + m: null, + b: 't' +}; + +hPositions = { + l: 'r', + c: null, + r: 'l' +}; + +parsePosition = function(str) { + var pos; + pos = []; + $.each(str.split(/\W+/), function(i, word) { + var w; + w = word.toLowerCase()[0]; + if (vPositions[w]) { + pos.push(w); + } + if (hPositions[w]) { + return pos.push(w); + } + }); + return pos; }; styles = { core: { html: "
\n
\n
\n
", - 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 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}\n\n." + className + "Arrow {\n margin-top: " + (2 + (document.documentMode === 5 ? size * -4 : 0)) + "px;\n position: absolute;\n z-index: 2;\n margin-left: 10px;\n width: 0;\n height: 0;\n}\n" + 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 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}\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" }, user: { "default": { @@ -29,7 +50,7 @@ styles = { 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: "
\n \n
", + html: "
\n \n
", 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}", colors: { red: '#eed3d7' @@ -38,8 +59,6 @@ styles = { } }; -bootstrapDetected = false; - pluginOptions = { autoHide: false, autoHideDelay: 2000, @@ -235,13 +254,22 @@ Notification = (function() { }; Notification.prototype.getPosition = function() { - if (this.options.position) { - return this.options.position; + var pos, text; + text = this.options.position; + pos = parsePosition(text); + if (pos.length === 0) { + pos.push('b'); } - if (this.elem.position().left + this.elem.width() + this.wrapper.width() < $(window).width()) { - return 'right'; + if (pos.length === 1 && pos[0] === 'l' || pos[0] === 'r') { + pos.push('m'); } - return 'bottom'; + if (pos.length === 1) { + pos.push('l'); + } + if (!this.options.autoReposition) { + return pos; + } + throw "Not implemented"; }; Notification.prototype.getColor = function() { @@ -271,7 +299,7 @@ Notification = (function() { Notification.prototype.updateStyle = function() { var _this = this; return this.wrapper.find('[data-notify-style]').each(function(i, e) { - return $(e).attr('style', $(e).attr('data-notify-style').replace(/\{\{\s*color\s*\}\}/ig, _this.getColor()).replace(/\{\{\s*position\s*\}\}/ig, _this.getPosition())); + return $(e).attr('style', $(e).attr('data-notify-style').replace(/\{\{\s*color\s*\}\}/ig, _this.getColor())); }); }; @@ -318,7 +346,7 @@ Notification = (function() { $(function() { $("body").append(cornerElem); $("link").each(function() { - var src; + var bootstrapDetected, src; src = $(this).attr('href'); if (src.match(/bootstrap/)) { bootstrapDetected = true; @@ -342,12 +370,13 @@ $(function() { $[pluginName] = function(elem, data, options) { if ((elem && elem.nodeName) || elem.jquery) { - return $(elem)[pluginName](data, options); + $(elem)[pluginName](data, options); } else { options = data; data = elem; - return new Notification(null, data, options); + new Notification(null, data, options); } + return elem; }; $[pluginName].options = function(options) { @@ -359,7 +388,7 @@ $[pluginName].styles = function(s) { }; $.fn[pluginName] = function(data, options) { - return $(this).each(function() { + $(this).each(function() { var inst; inst = getAnchorElement($(this)).data(className); if (inst) { @@ -368,6 +397,7 @@ $.fn[pluginName] = function(data, options) { return new Notification($(this), data, options); } }); + return this; }; }(window,document)); \ No newline at end of file diff --git a/dist/notify.min.js b/dist/notify.min.js index 263d83f..649a6c1 100644 --- a/dist/notify.min.js +++ b/dist/notify.min.js @@ -1,4 +1,4 @@ -/** Notify.js - v0.0.1 - 2013/05/28 +/** Notify.js - v0.0.1 - 2013/05/29 * http://notifyjs.com/ * Copyright (c) 2013 Jaime Pillora - MIT - */(function(t,n){"use strict";var o,i,e,r,s,a,p,h,d,l,u,c;l="notify",r="__"+l,i={top:"bottom",bottom:"top",left:"right",right:"left"},c={core:{html:'
\n
\n
\n
',css:"."+r+"Corner {\n position: fixed;\n top: 0;\n right: 0;\n margin: 5px;\n z-index: 1050;\n}\n\n."+r+"Corner ."+r+"Wrapper,\n."+r+"Corner ."+r+"Container {\n position: relative;\n display: block;\n height: inherit;\n width: inherit;\n}\n\n."+r+"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."+r+"Container {\n display: none;\n z-index: 1;\n position: absolute;\n cursor: pointer;\n}\n\n."+r+"Text {\n position: relative;\n}\n\n."+r+"Arrow {\n margin-top: "+(2+(5===n.documentMode?-4*size:0))+"px;\n position: absolute;\n z-index: 2;\n margin-left: 10px;\n width: 0;\n height: 0;\n}\n"},user:{"default":{html:'
\n \n
',css:"."+r+"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:'
\n \n
',css:"."+r+"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}",colors:{red:"#eed3d7"}}}},e=!1,u={autoHide:!1,autoHideDelay:2e3,arrowShow:!1,arrowSize:5,position:"bottom",style:null,color:"red",colors:{red:"#b94a48",green:"#33be40",black:"#393939",blue:"#00f"},showAnimation:"slideDown",showDuration:400,hideAnimation:"slideUp",hideDuration:200,offsetY:2,offsetX:0},a=function(t){return $("<"+t+">")},h=function(t,n){var o;return o=function(){},o.prototype=t,$.extend(!0,new o,n)},s=a("div").addClass(""+r+"Corner"),p=function(t){var n;return t.is("[type=radio]")&&(n=t.parents("form:first").find("[type=radio]").filter(function(n,o){return $(o).attr("name")===t.attr("name")}),t=n.first()),t},d=function(t){var n;if(t&&t.css){n=t.cssElem,n||(n=a("style"),$("head").append(n),t.cssElem=n);try{return n.html(t.css)}catch(o){return n[0].styleSheet.cssText=t.css}}},o=function(){function n(t,n,o){"string"==typeof o&&(o={color:o}),this.options=h(u,$.isPlainObject(o)?o:{}),this.loadCSS(),this.loadHTML(),this.wrapper=$(c.core.html),this.wrapper.data(r,this),this.arrow=this.wrapper.find("."+r+"Arrow"),this.container=this.wrapper.find("."+r+"Container"),this.container.append(this.userContainer),t&&t.length?(this.elementType=t.attr("type"),this.originalElement=t,this.elem=p(t),this.elem.data(r,this),this.elem.before(this.wrapper)):(this.options.arrowShow=!1,s.prepend(this.wrapper)),this.container.hide(),this.run(n)}return n.prototype.loadCSS=function(t){var n;return t||(n=this.options.style,t=this.getStyle(n)),d(t)},n.prototype.loadHTML=function(){var t;if(t=this.getStyle(),this.userContainer=$(t.html),this.text=this.userContainer.find("[data-notify=text]"),0===this.text.length)throw"style: "+name+" HTML is missing the: data-notify='text' attribute";return this.text.addClass(""+r+"Text")},n.prototype.show=function(t,n){var o,i,e,r;return null==n&&(n=$.noop),r=this.container.parent().parents(":hidden").length>0,i=this.container.add(this.arrow),o=[],r&&t?e="show":r&&!t?e="hide":!r&&t?(e=this.options.showAnimation,o.push(this.options.showDuration)):r||t||(e=this.options.hideAnimation,o.push(this.options.hideDuration)),o.push(n),i[e].apply(i,o)},n.prototype.updatePosition=function(){var t,n,o,i;if(this.elem){switch(t=this.elem.position(),n=this.wrapper.position(),i=this.getPosition(),o={top:0,left:0},i){case"bottom":o.top+=this.elem.outerHeight();break;case"right":o.left+=this.elem.width();break;default:throw"Unknown position: "+i}return navigator.userAgent.match(/MSIE/)||(o.top+=t.top-n.top),o.left+=t.left-n.left,o.top+=this.options.offsetY,o.left+=this.options.offsetX,this.updateArrow(o,i),this.container.css(o)}},n.prototype.updateArrow=function(t,n){var o,e,r;if(!this.options.arrowShow||"radio"===this.elementType)return this.arrow.hide(),undefined;e=i[n],r=this.options.arrowSize,this.arrow.css("border-"+n,r+"px solid "+this.getColor()),this.arrow.css(t);for(o in i)o!==e&&o!==n&&this.arrow.css("border-"+o,r+"px solid transparent");switch(n){case"bottom":t.top+=r;break;case"right":t.left+=r}return this.arrow.show()},n.prototype.getPosition=function(){return this.options.position?this.options.position:this.elem.position().left+this.elem.width()+this.wrapper.width()<$(t).width()?"right":"bottom"},n.prototype.getColor=function(){var t;return t=this.getStyle().colors,t&&t[this.options.color]||this.options.colors[this.options.color]||this.options.color},n.prototype.getStyle=function(t){var n;if(t||(t=this.options.style),e&&!t&&(t="bootstrap"),t||(t="default"),n=c.user[t],!n)throw"Missing style: "+t;return n},n.prototype.updateStyle=function(){var t=this;return this.wrapper.find("[data-notify-style]").each(function(n,o){return $(o).attr("style",$(o).attr("data-notify-style").replace(/\{\{\s*color\s*\}\}/gi,t.getColor()).replace(/\{\{\s*position\s*\}\}/gi,t.getPosition()))})},n.prototype.run=function(t,n){var o,i=this;return $.isPlainObject(n)?$.extend(this.options,n):"string"===$.type(n)&&(this.options.color=n),this.container&&!t?(this.show(!1),undefined):this.container||t?("string"===$.type(t)?this.text.html(t.replace("\n","
")):this.text.empty().append(t),this.updatePosition(),this.show(!0),this.options.autoHide?(clearTimeout(this.autohideTimer),o=setTimeout(function(){return i.show(!1)},this.options.autoHideDelay)):undefined):undefined},n.prototype.destroy=function(){var t=this;return this.show(!1,function(){return t.wrapper.remove()})},n}(),$(function(){return $("body").append(s),$("link").each(function(){var t;return t=$(this).attr("href"),t.match(/bootstrap/)?(e=!0,!1):undefined}),d(c.core),$(n).on("click","."+r+"Wrapper",function(){var t;return(t=$(this).data(r))?t.elem?t.show(!1):t.destroy():undefined})}),$[l]=function(t,n,i){return t&&t.nodeName||t.jquery?$(t)[l](n,i):(i=n,n=t,new o(null,n,i))},$[l].options=function(t){return $.extend(u,t)},$[l].styles=function(t){return $.extend(!0,c.user,t)},$.fn[l]=function(t,n){return $(this).each(function(){var i;return i=p($(this)).data(r),i?i.run(t,n):new o($(this),t,n)})}})(window,document); \ No newline at end of file + */(function(t,n){"use strict";var o,e,i,r,s,a,p,h,l,d,u,c,f;d="notify",e="__"+d,f={t:"b",m:null,b:"t"},a={l:"r",c:null,r:"l"},l=function(t){var n;return n=[],$.each(t.split(/\W+/),function(t,o){var e;return e=o.toLowerCase()[0],f[e]&&n.push(e),a[e]?n.push(e):undefined}),n},c={core:{html:'
\n
\n
\n
',css:"."+e+"Corner {\n position: fixed;\n top: 0;\n right: 0;\n margin: 5px;\n z-index: 1050;\n}\n\n."+e+"Corner ."+e+"Wrapper,\n."+e+"Corner ."+e+"Container {\n position: relative;\n display: block;\n height: inherit;\n width: inherit;\n}\n\n."+e+"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."+e+"Container {\n display: none;\n z-index: 1;\n position: absolute;\n cursor: pointer;\n}\n\n."+e+"Text {\n position: relative;\n}\n\n."+e+"Arrow {\n margin-top: 2px;\n position: absolute;\n z-index: 2;\n margin-left: 10px;\n width: 0;\n height: 0;\n}\n"},user:{"default":{html:'
\n \n
',css:"."+e+"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:'
\n \n
',css:"."+e+"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}",colors:{red:"#eed3d7"}}}},u={autoHide:!1,autoHideDelay:2e3,arrowShow:!1,arrowSize:5,position:"bottom",style:null,color:"red",colors:{red:"#b94a48",green:"#33be40",black:"#393939",blue:"#00f"},showAnimation:"slideDown",showDuration:400,hideAnimation:"slideUp",hideDuration:200,offsetY:2,offsetX:0},r=function(t){return $("<"+t+">")},p=function(t,n){var o;return o=function(){},o.prototype=t,$.extend(!0,new o,n)},i=r("div").addClass(""+e+"Corner"),s=function(t){var n;return t.is("[type=radio]")&&(n=t.parents("form:first").find("[type=radio]").filter(function(n,o){return $(o).attr("name")===t.attr("name")}),t=n.first()),t},h=function(t){var n;if(t&&t.css){n=t.cssElem,n||(n=r("style"),$("head").append(n),t.cssElem=n);try{return n.html(t.css)}catch(o){return n[0].styleSheet.cssText=t.css}}},o=function(){function t(t,n,o){"string"==typeof o&&(o={color:o}),this.options=p(u,$.isPlainObject(o)?o:{}),this.loadCSS(),this.loadHTML(),this.wrapper=$(c.core.html),this.wrapper.data(e,this),this.arrow=this.wrapper.find("."+e+"Arrow"),this.container=this.wrapper.find("."+e+"Container"),this.container.append(this.userContainer),t&&t.length?(this.elementType=t.attr("type"),this.originalElement=t,this.elem=s(t),this.elem.data(e,this),this.elem.before(this.wrapper)):(this.options.arrowShow=!1,i.prepend(this.wrapper)),this.container.hide(),this.run(n)}return t.prototype.loadCSS=function(t){var n;return t||(n=this.options.style,t=this.getStyle(n)),h(t)},t.prototype.loadHTML=function(){var t;if(t=this.getStyle(),this.userContainer=$(t.html),this.text=this.userContainer.find("[data-notify=text]"),0===this.text.length)throw"style: "+name+" HTML is missing the: data-notify='text' attribute";return this.text.addClass(""+e+"Text")},t.prototype.show=function(t,n){var o,e,i,r;return null==n&&(n=$.noop),r=this.container.parent().parents(":hidden").length>0,e=this.container.add(this.arrow),o=[],r&&t?i="show":r&&!t?i="hide":!r&&t?(i=this.options.showAnimation,o.push(this.options.showDuration)):r||t||(i=this.options.hideAnimation,o.push(this.options.hideDuration)),o.push(n),e[i].apply(e,o)},t.prototype.updatePosition=function(){var t,n,o,e;if(this.elem){switch(t=this.elem.position(),n=this.wrapper.position(),e=this.getPosition(),o={top:0,left:0},e){case"bottom":o.top+=this.elem.outerHeight();break;case"right":o.left+=this.elem.width();break;default:throw"Unknown position: "+e}return navigator.userAgent.match(/MSIE/)||(o.top+=t.top-n.top),o.left+=t.left-n.left,o.top+=this.options.offsetY,o.left+=this.options.offsetX,this.updateArrow(o,e),this.container.css(o)}},t.prototype.updateArrow=function(t,n){var o,e,i;if(!this.options.arrowShow||"radio"===this.elementType)return this.arrow.hide(),undefined;e=arrowDirs[n],i=this.options.arrowSize,this.arrow.css("border-"+n,i+"px solid "+this.getColor()),this.arrow.css(t);for(o in arrowDirs)o!==e&&o!==n&&this.arrow.css("border-"+o,i+"px solid transparent");switch(n){case"bottom":t.top+=i;break;case"right":t.left+=i}return this.arrow.show()},t.prototype.getPosition=function(){var t,n;if(n=this.options.position,t=l(n),0===t.length&&t.push("b"),(1===t.length&&"l"===t[0]||"r"===t[0])&&t.push("m"),1===t.length&&t.push("l"),!this.options.autoReposition)return t;throw"Not implemented"},t.prototype.getColor=function(){var t;return t=this.getStyle().colors,t&&t[this.options.color]||this.options.colors[this.options.color]||this.options.color},t.prototype.getStyle=function(t){var n;if(t||(t=this.options.style),bootstrapDetected&&!t&&(t="bootstrap"),t||(t="default"),n=c.user[t],!n)throw"Missing style: "+t;return n},t.prototype.updateStyle=function(){var t=this;return this.wrapper.find("[data-notify-style]").each(function(n,o){return $(o).attr("style",$(o).attr("data-notify-style").replace(/\{\{\s*color\s*\}\}/gi,t.getColor()))})},t.prototype.run=function(t,n){var o,e=this;return $.isPlainObject(n)?$.extend(this.options,n):"string"===$.type(n)&&(this.options.color=n),this.container&&!t?(this.show(!1),undefined):this.container||t?("string"===$.type(t)?this.text.html(t.replace("\n","
")):this.text.empty().append(t),this.updatePosition(),this.show(!0),this.options.autoHide?(clearTimeout(this.autohideTimer),o=setTimeout(function(){return e.show(!1)},this.options.autoHideDelay)):undefined):undefined},t.prototype.destroy=function(){var t=this;return this.show(!1,function(){return t.wrapper.remove()})},t}(),$(function(){return $("body").append(i),$("link").each(function(){var t,n;return n=$(this).attr("href"),n.match(/bootstrap/)?(t=!0,!1):undefined}),h(c.core),$(n).on("click","."+e+"Wrapper",function(){var t;return(t=$(this).data(e))?t.elem?t.show(!1):t.destroy():undefined})}),$[d]=function(t,n,e){return t&&t.nodeName||t.jquery?$(t)[d](n,e):(e=n,n=t,new o(null,n,e)),t},$[d].options=function(t){return $.extend(u,t)},$[d].styles=function(t){return $.extend(!0,c.user,t)},$.fn[d]=function(t,n){return $(this).each(function(){var i;return i=s($(this)).data(e),i?i.run(t,n):new o($(this),t,n)}),this}})(window,document); \ No newline at end of file diff --git a/examples/position.html b/examples/position.html new file mode 100644 index 0000000..11915d2 --- /dev/null +++ b/examples/position.html @@ -0,0 +1,28 @@ + + + + + + + + + +
+ + + + + + \ No newline at end of file diff --git a/index.html b/index.html deleted file mode 100644 index 5d73cea..0000000 --- a/index.html +++ /dev/null @@ -1,26 +0,0 @@ - - - - - -
- - - -
- - \ No newline at end of file diff --git a/src/notify.coffee b/src/notify.coffee index 0bf958a..580eb41 100644 --- a/src/notify.coffee +++ b/src/notify.coffee @@ -5,15 +5,25 @@ pluginName = 'notify' className = '__'+pluginName -#opposites -arrowDirs = - top: 'bottom' - bottom: 'top' - left: 'right' - right: 'left' - middle: null - center: null +#positions mapped to opposites +vPositions = + t: 'b' + m: null + b: 't' +hPositions = + l: 'r' + c: null + r: 'l' +parsePosition = (str) -> + pos = [] + $.each str.split(/\W+/), (i,word) -> + w = word.toLowerCase()[0] + pos.push w if vPositions[w] + pos.push w if hPositions[w] + pos + +#built-in styles styles = core: html: """ @@ -60,7 +70,7 @@ styles = } .#{className}Arrow { - margin-top: #{2 + (if document.documentMode is 5 then (size*-4) else 0)}px; + margin-top: 2px; position: absolute; z-index: 2; margin-left: 10px; @@ -100,7 +110,7 @@ styles = bootstrap: html: """
- +
""" css: """ @@ -116,8 +126,6 @@ styles = colors: red: '#eed3d7' - -bootstrapDetected = false #overridable options pluginOptions = autoHide: false @@ -146,9 +154,7 @@ pluginOptions = #parents: { '.ui-dialog': 5001 } # plugin helpers -createElem = (tag) -> - $ "<#{tag}>" - +createElem = (tag) -> $ "<#{tag}>" inherit = (a, b) -> F = () -> F.prototype = a @@ -312,10 +318,22 @@ class Notification @arrow.show() getPosition: -> - return @options.position if @options.position - if @elem.position().left + @elem.width() + @wrapper.width() < $(window).width() - return 'right' - return 'bottom' + text = @options.position + pos = parsePosition text + + #if unspecified - choose bottom left + pos.push 'b' if pos.length is 0 + pos.push 'm' if pos.length is 1 and pos[0] is 'l' or pos[0] is 'r' + pos.push 'l' if pos.length is 1 + + unless @options.autoReposition + return pos + + # if @elem.position().left + @elem.width() + @wrapper.width() < $(window).width() + # return 'right' + # return 'bottom' + + throw "Not implemented" getColor: -> styleColors = @getStyle().colors @@ -336,8 +354,9 @@ class Notification #update colors @wrapper.find('[data-notify-style]').each (i,e) => $(e).attr 'style', $(e).attr('data-notify-style'). - replace(/\{\{\s*color\s*\}\}/ig, @getColor()). - replace(/\{\{\s*position\s*\}\}/ig, @getPosition()) + replace(/\{\{\s*color\s*\}\}/ig, @getColor()) + # . + # replace(/\{\{\s*position\s*\}\}/ig, @getPosition()) #run plugin run: (data, options) -> @@ -409,7 +428,8 @@ $[pluginName] = (elem, data, options) -> else options = data data = elem - new Notification null, data, options + new Notification null, data, options + elem # publicise methods $[pluginName].options = (options) -> @@ -426,4 +446,5 @@ $.fn[pluginName] = (data, options) -> inst.run data, options else new Notification $(@), data, options + @