positioning and hiding correctly

This commit is contained in:
Jaime Pillora
2013-04-05 12:26:27 +11:00
parent 1d9c3f9ce9
commit d22c0c7422
3 changed files with 86 additions and 66 deletions
+38 -33
View File
@@ -21,7 +21,7 @@ arrowDirs = {
styles = { styles = {
core: { core: {
html: "<div class=\"" + className + "Wrapper\">\n <div class=\"" + className + "Arrow\"></div>\n <div class=\"" + className + "Container\"></div>\n</div>", html: "<div class=\"" + className + "Wrapper\">\n <div class=\"" + className + "Arrow\"></div>\n <div class=\"" + className + "Container\"></div>\n</div>",
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)) + ";\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: " + (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"
}, },
user: { user: {
"default": { "default": {
@@ -117,7 +117,7 @@ Notification = (function() {
this.loadCSS(); this.loadCSS();
this.loadHTML(); this.loadHTML();
this.wrapper = $(styles.core.html); this.wrapper = $(styles.core.html);
this.wrapper.data(pluginName, this); this.wrapper.data(className, this);
this.arrow = this.wrapper.find("." + className + "Arrow"); this.arrow = this.wrapper.find("." + className + "Arrow");
this.container = this.wrapper.find("." + className + "Container"); this.container = this.wrapper.find("." + className + "Container");
this.container.append(this.userContainer); this.container.append(this.userContainer);
@@ -125,7 +125,7 @@ Notification = (function() {
this.elementType = elem.attr('type'); this.elementType = elem.attr('type');
this.originalElement = elem; this.originalElement = elem;
this.elem = getAnchorElement(elem); this.elem = getAnchorElement(elem);
this.elem.data(pluginName, this); this.elem.data(className, this);
this.elem.before(this.wrapper); this.elem.before(this.wrapper);
} else { } else {
this.options.arrowShow = false; this.options.arrowShow = false;
@@ -155,73 +155,78 @@ Notification = (function() {
}; };
Notification.prototype.show = function(show) { Notification.prototype.show = function(show) {
var hidden; var elems, hidden;
hidden = this.container.parent().parents(':hidden').length > 0; hidden = this.container.parent().parents(':hidden').length > 0;
elems = this.container.add(this.arrow);
if (hidden && show) { if (hidden && show) {
this.container.show(); elems.show();
} }
if (hidden && !show) { if (hidden && !show) {
this.container.hide(); elems.hide();
} }
if (!hidden && show) { if (!hidden && show) {
this.container[this.options.showAnimation](this.options.showDuration); elems[this.options.showAnimation](this.options.showDuration);
} }
if (!hidden && !show) { if (!hidden && !show) {
return this.container[this.options.hideAnimation](this.options.hideDuration); return elems[this.options.hideAnimation](this.options.hideDuration);
} }
}; };
Notification.prototype.updatePosition = function() { Notification.prototype.updatePosition = function() {
var arrowPosition, elementPosition, height, left, mainPosition, position; var elementPosition, mainPosition, p, position;
if (!this.elem) { if (!this.elem) {
return; return;
} }
elementPosition = this.elem.position(); elementPosition = this.elem.position();
mainPosition = this.wrapper.position(); mainPosition = this.wrapper.position();
position = this.getPosition(); position = this.getPosition();
arrowPosition = this.updateArrow(position); p = {
left = 0; top: 0,
height = 0; left: 0
};
switch (position) { switch (position) {
case 'bottom': case 'bottom':
height = this.elem.outerHeight(); p.top += this.elem.outerHeight();
break; break;
case 'right': case 'right':
left = this.elem.width(); p.left += this.elem.width();
break; break;
default: default:
throw "Unknown position: " + position; throw "Unknown position: " + position;
} }
left += elementPosition.left - mainPosition.left;
if (!navigator.userAgent.match(/MSIE/)) { if (!navigator.userAgent.match(/MSIE/)) {
height += elementPosition.top - mainPosition.top; p.top += elementPosition.top - mainPosition.top;
} }
return this.container.css({ p.left += elementPosition.left - mainPosition.left;
top: height + this.options.offsetY + arrowPosition.top, p.top += this.options.offsetY;
left: left + this.options.offsetX + arrowPosition.left p.left += this.options.offsetX;
}); this.updateArrow(p, position);
return this.container.css(p);
}; };
Notification.prototype.updateArrow = function(position) { Notification.prototype.updateArrow = function(p, position) {
var d, dir, offsets, size; var d, dir, size;
offsets = {
top: 0,
left: 0
};
if (!(this.options.arrowShow && this.elementType !== 'radio')) { if (!(this.options.arrowShow && this.elementType !== 'radio')) {
this.arrow.hide(); this.arrow.hide();
return offsets; return;
} }
dir = arrowDirs[position]; dir = arrowDirs[position];
size = this.options.arrowSize; size = this.options.arrowSize;
this.arrow.css('border-' + position, size + 'px solid ' + this.getColor()); this.arrow.css('border-' + position, size + 'px solid ' + this.getColor());
this.arrow.css(p);
for (d in arrowDirs) { for (d in arrowDirs) {
if (d !== dir && d !== position) { if (d !== dir && d !== position) {
this.arrow.css('border-' + d, size + 'px solid transparent'); this.arrow.css('border-' + d, size + 'px solid transparent');
} }
} }
this.arrow.show(); switch (position) {
return offsets; case 'bottom':
p.top += size;
break;
case 'right':
p.left += size;
}
return this.arrow.show();
}; };
Notification.prototype.getPosition = function() { Notification.prototype.getPosition = function() {
@@ -303,7 +308,7 @@ $(function() {
$("link").each(function() { $("link").each(function() {
var src; var src;
src = $(this).attr('href'); src = $(this).attr('href');
if (src.match(/bootstrap[^\/]*\.css/)) { if (src.match(/bootstrap/)) {
bootstrapDetected = true; bootstrapDetected = true;
return false; return false;
} }
@@ -311,7 +316,7 @@ $(function() {
insertCSS(styles.core); insertCSS(styles.core);
return $(document).on('click', "." + className + "Wrapper", function() { return $(document).on('click', "." + className + "Wrapper", function() {
var inst; var inst;
inst = $(this).data(pluginName); inst = $(this).data(className);
if (inst) { if (inst) {
return inst.show(false); return inst.show(false);
} }
@@ -332,14 +337,14 @@ $[pluginName].options = function(options) {
return $.extend(pluginOptions, options); return $.extend(pluginOptions, options);
}; };
$[pluginName].addStyle = function(s) { $[pluginName].styles = function(s) {
return $.extend(true, styles.user, s); return $.extend(true, styles.user, s);
}; };
$.fn[pluginName] = function(data, options) { $.fn[pluginName] = function(data, options) {
return $(this).each(function() { return $(this).each(function() {
var inst; var inst;
inst = getAnchorElement($(this)).data(pluginName); inst = getAnchorElement($(this)).data(className);
if (inst) { if (inst) {
return inst.run(data, options); return inst.run(data, options);
} else { } else {
+1 -1
View File
File diff suppressed because one or more lines are too long
+46 -31
View File
@@ -57,7 +57,7 @@ styles =
} }
.#{className}Arrow { .#{className}Arrow {
margin-top: #{2 + (if document.documentMode is 5 then (size*-4) else 0)}; margin-top: #{2 + (if document.documentMode is 5 then (size*-4) else 0)}px;
position: absolute; position: absolute;
z-index: 2; z-index: 2;
margin-left: 10px; margin-left: 10px;
@@ -190,7 +190,7 @@ class Notification
@loadHTML() @loadHTML()
@wrapper = $(styles.core.html) @wrapper = $(styles.core.html)
@wrapper.data pluginName, @ @wrapper.data className, @
@arrow = @wrapper.find ".#{className}Arrow" @arrow = @wrapper.find ".#{className}Arrow"
@container = @wrapper.find ".#{className}Container" @container = @wrapper.find ".#{className}Container"
@container.append @userContainer @container.append @userContainer
@@ -199,7 +199,7 @@ class Notification
@elementType = elem.attr('type') @elementType = elem.attr('type')
@originalElement = elem @originalElement = elem
@elem = getAnchorElement(elem) @elem = getAnchorElement(elem)
@elem.data pluginName, @ @elem.data className, @
# add into dom above elem # add into dom above elem
@elem.before @wrapper @elem.before @wrapper
else else
@@ -226,11 +226,16 @@ class Notification
@text.addClass "#{className}Text" @text.addClass "#{className}Text"
show: (show) -> show: (show) ->
hidden = @container.parent().parents(':hidden').length > 0 hidden = @container.parent().parents(':hidden').length > 0
@container.show() if hidden and show
@container.hide() if hidden and not show elems = @container.add @arrow
@container[@options.showAnimation] @options.showDuration if not hidden and show elems.show() if hidden and show
@container[@options.hideAnimation] @options.hideDuration if not hidden and not show elems.hide() if hidden and not show
elems[@options.showAnimation] @options.showDuration if not hidden and show
elems[@options.hideAnimation] @options.hideDuration if not hidden and not show
updatePosition: -> updatePosition: ->
return unless @elem return unless @elem
@@ -240,47 +245,57 @@ class Notification
# #
position = @getPosition() position = @getPosition()
arrowPosition = @updateArrow(position)
#start calculations #start calculations
left = 0 p =
height = 0 top: 0
left: 0
switch position switch position
when 'bottom' when 'bottom'
height = @elem.outerHeight() p.top += @elem.outerHeight()
when 'right' when 'right'
left = @elem.width() p.left += @elem.width()
else else
throw "Unknown position: #{position}" throw "Unknown position: #{position}"
#elem vs wrapper corrections #elem vs wrapper corrections
left += elementPosition.left - mainPosition.left p.top += (elementPosition.top - mainPosition.top) unless navigator.userAgent.match /MSIE/
height += (elementPosition.top - mainPosition.top) unless navigator.userAgent.match /MSIE/ p.left += elementPosition.left - mainPosition.left
#set with user offset, with arrow offet #set user offset
@container.css p.top += @options.offsetY
top: height + @options.offsetY + arrowPosition.top p.left += @options.offsetX
left: left + @options.offsetX + arrowPosition.left
updateArrow: (position) -> @updateArrow p, position
offsets = {top: 0, left: 0} # if @setCSS
# @container.stop().animate(p)
# else
@container.css p
# @setCSS = true
updateArrow: (p, position) ->
unless @options.arrowShow and @elementType isnt 'radio' unless @options.arrowShow and @elementType isnt 'radio'
@arrow.hide() @arrow.hide()
return offsets return
dir = arrowDirs[position] dir = arrowDirs[position]
size = @options.arrowSize size = @options.arrowSize
@arrow.css 'border-' + position, size + 'px solid ' + @getColor() @arrow.css 'border-' + position, size + 'px solid ' + @getColor()
@arrow.css p
for d of arrowDirs for d of arrowDirs
if d isnt dir and d isnt position if d isnt dir and d isnt position
@arrow.css 'border-' + d, size + 'px solid transparent' @arrow.css 'border-' + d, size + 'px solid transparent'
@arrow.show() switch position
offsets when 'bottom'
p.top += size
when 'right'
p.left += size
@arrow.show()
getPosition: -> getPosition: ->
return @options.position if @options.position return @options.position if @options.position
@@ -343,22 +358,24 @@ class Notification
@show false @show false
, @options.autoHideDelay , @options.autoHideDelay
#when ready, bind permanent hide listener #when ready
$ -> $ ->
#corner notification container
$("body").append cornerElem $("body").append cornerElem
#auto-detect bootstrap #auto-detect bootstrap
$("link").each -> $("link").each ->
src = $(@).attr 'href' src = $(@).attr 'href'
if src.match /bootstrap[^\/]*\.css/ if src.match /bootstrap/
bootstrapDetected = true bootstrapDetected = true
return false return false
#add core styles #add core styles
insertCSS styles.core insertCSS styles.core
#watch all notifications clicks #watch all notifications clicks
$(document).on 'click', ".#{className}Wrapper", -> $(document).on 'click', ".#{className}Wrapper", ->
inst = $(@).data pluginName inst = $(@).data className
inst.show false if inst inst.show false if inst
# publicise jquery plugin # publicise jquery plugin
@@ -373,19 +390,17 @@ $[pluginName] = (elem, data, options) ->
data = elem data = elem
new Notification null, data, options new Notification null, data, options
# publicise options method # publicise methods
$[pluginName].options = (options) -> $[pluginName].options = (options) ->
$.extend pluginOptions, options $.extend pluginOptions, options
$[pluginName].addStyle = (s) -> $[pluginName].styles = (s) ->
$.extend true, styles.user, s $.extend true, styles.user, s
# $( ... ).pluginName( { .. } ) creates a cached instance on each # $( ... ).pluginName( { .. } ) creates a cached instance on each
# selected item with custom options for just that instance
# return alert "$.fn#{pluginName} already defined" if $.fn[pluginName]?
$.fn[pluginName] = (data, options) -> $.fn[pluginName] = (data, options) ->
$(@).each -> $(@).each ->
inst = getAnchorElement($(@)).data pluginName inst = getAnchorElement($(@)).data className
if inst if inst
inst.run data, options inst.run data, options
else else