2013-04-04 13:13:08 +11:00
|
|
|
|
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
|
|
#plugin constants
|
|
|
|
|
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-31 13:18:43 +10:00
|
|
|
hPositions = ['l','c','r']
|
|
|
|
|
vPositions = ['t','m','b']
|
|
|
|
|
mainPositions = ['t','b','l','r']
|
2013-05-29 16:59:19 +10:00
|
|
|
#positions mapped to opposites
|
2013-05-30 16:35:16 +10:00
|
|
|
opposites =
|
2013-05-29 16:59:19 +10:00
|
|
|
t: 'b'
|
|
|
|
|
m: null
|
|
|
|
|
b: 't'
|
|
|
|
|
l: 'r'
|
|
|
|
|
c: null
|
|
|
|
|
r: 'l'
|
2013-04-04 13:13:08 +11:00
|
|
|
|
2013-05-29 16:59:19 +10:00
|
|
|
parsePosition = (str) ->
|
|
|
|
|
pos = []
|
|
|
|
|
$.each str.split(/\W+/), (i,word) ->
|
2013-05-30 16:35:16 +10:00
|
|
|
w = word.toLowerCase().charAt(0)
|
|
|
|
|
pos.push w if positions[w]
|
2013-05-29 16:59:19 +10:00
|
|
|
pos
|
|
|
|
|
|
|
|
|
|
#built-in styles
|
2013-04-04 13:48:42 +11:00
|
|
|
styles =
|
|
|
|
|
core:
|
|
|
|
|
html: """
|
2013-04-04 14:02:55 +11:00
|
|
|
<div class="#{className}Wrapper">
|
2013-05-31 13:18:43 +10:00
|
|
|
<div class="#{className}Debug"></div>
|
2013-04-05 11:53:48 +11:00
|
|
|
<div class="#{className}Arrow"></div>
|
|
|
|
|
<div class="#{className}Container"></div>
|
2013-04-04 14:02:55 +11:00
|
|
|
</div>
|
2013-04-04 13:48:42 +11:00
|
|
|
"""
|
|
|
|
|
css: """
|
2013-04-04 16:58:22 +11:00
|
|
|
.#{className}Corner {
|
|
|
|
|
position: fixed;
|
|
|
|
|
top: 0;
|
|
|
|
|
right: 0;
|
|
|
|
|
margin: 5px;
|
|
|
|
|
z-index: 1050;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.#{className}Corner .#{className}Wrapper,
|
|
|
|
|
.#{className}Corner .#{className}Container {
|
|
|
|
|
position: relative;
|
|
|
|
|
display: block;
|
|
|
|
|
height: inherit;
|
|
|
|
|
width: inherit;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-04 13:48:42 +11:00
|
|
|
.#{className}Wrapper {
|
|
|
|
|
z-index: 1;
|
|
|
|
|
position: absolute;
|
|
|
|
|
display: inline-block;
|
|
|
|
|
height: 0;
|
|
|
|
|
width: 0;
|
2013-05-31 13:18:43 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.#{className}Debug {
|
|
|
|
|
position: absolute;
|
|
|
|
|
border: 3px solid red;
|
|
|
|
|
height: 0;
|
|
|
|
|
width: 0;
|
2013-04-04 13:48:42 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.#{className}Container {
|
|
|
|
|
display: none;
|
|
|
|
|
z-index: 1;
|
|
|
|
|
position: absolute;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.#{className}Text {
|
|
|
|
|
position: relative;
|
|
|
|
|
}
|
2013-04-05 11:53:48 +11:00
|
|
|
|
|
|
|
|
.#{className}Arrow {
|
|
|
|
|
position: absolute;
|
|
|
|
|
z-index: 2;
|
|
|
|
|
width: 0;
|
|
|
|
|
height: 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-04 13:48:42 +11:00
|
|
|
"""
|
|
|
|
|
user:
|
|
|
|
|
default:
|
|
|
|
|
html: """
|
2013-04-04 16:58:22 +11:00
|
|
|
<div class="#{className}Default"
|
|
|
|
|
data-notify-style="
|
|
|
|
|
color: {{color}};
|
|
|
|
|
border-color: {{color}};
|
|
|
|
|
">
|
2013-05-30 16:35:16 +10:00
|
|
|
<span data-notify-text></span>
|
2013-04-04 14:02:55 +11:00
|
|
|
</div>
|
2013-04-04 13:48:42 +11:00
|
|
|
"""
|
|
|
|
|
css: """
|
|
|
|
|
.#{className}Default {
|
|
|
|
|
background: #fff;
|
|
|
|
|
font-size: 11px;
|
|
|
|
|
box-shadow: 0 0 6px #000;
|
|
|
|
|
-moz-box-shadow: 0 0 6px #000;
|
|
|
|
|
-webkit-box-shadow: 0 0 6px #000;
|
|
|
|
|
padding: 4px 10px 4px 8px;
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
border-style: solid;
|
|
|
|
|
border-width: 2px;
|
|
|
|
|
-moz-border-radius: 6px;
|
|
|
|
|
-webkit-border-radius: 6px;
|
|
|
|
|
white-space: nowrap;
|
|
|
|
|
}
|
|
|
|
|
"""
|
2013-04-04 13:13:08 +11:00
|
|
|
|
2013-04-04 13:48:42 +11:00
|
|
|
bootstrap:
|
|
|
|
|
html: """
|
2013-04-04 16:58:22 +11:00
|
|
|
<div class="alert alert-error #{className}Bootstrap">
|
2013-05-29 16:59:19 +10:00
|
|
|
<strong data-notify-text></strong>
|
2013-04-04 16:58:22 +11:00
|
|
|
</div>
|
2013-04-04 13:48:42 +11:00
|
|
|
"""
|
|
|
|
|
css: """
|
2013-04-04 16:58:22 +11:00
|
|
|
.#{className}Bootstrap {
|
|
|
|
|
white-space: nowrap;
|
2013-04-05 15:34:55 +11:00
|
|
|
margin: 5px !important;
|
2013-04-05 11:53:48 +11:00
|
|
|
padding-left: 25px !important;
|
|
|
|
|
background-repeat: no-repeat;
|
|
|
|
|
background-position: 3px 7px;
|
|
|
|
|
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=);
|
2013-04-04 13:48:42 +11:00
|
|
|
}
|
|
|
|
|
"""
|
2013-04-04 16:58:22 +11:00
|
|
|
colors:
|
|
|
|
|
red: '#eed3d7'
|
2013-04-04 13:13:08 +11:00
|
|
|
|
|
|
|
|
#overridable options
|
|
|
|
|
pluginOptions =
|
2013-04-04 16:58:22 +11:00
|
|
|
autoHide: false
|
|
|
|
|
autoHideDelay: 2000
|
2013-05-31 13:18:43 +10:00
|
|
|
arrowShow: true
|
2013-04-04 13:13:08 +11:00
|
|
|
arrowSize: 5
|
2013-04-05 15:09:44 +11:00
|
|
|
position: 'bottom'
|
2013-04-04 13:48:42 +11:00
|
|
|
# Default style
|
2013-05-30 16:35:16 +10:00
|
|
|
style: 'default'
|
2013-04-04 13:13:08 +11:00
|
|
|
# Default color
|
|
|
|
|
color: 'red'
|
|
|
|
|
# Color mappings
|
|
|
|
|
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
|
2013-04-04 13:13:08 +11:00
|
|
|
# Gap between main and element
|
2013-05-31 13:18:43 +10:00
|
|
|
offsetY: 0
|
2013-04-05 11:53:48 +11:00
|
|
|
offsetX: 0
|
2013-04-04 13:13:08 +11:00
|
|
|
#TODO add z-index watches
|
|
|
|
|
#parents: { '.ui-dialog': 5001 }
|
|
|
|
|
|
|
|
|
|
# plugin helpers
|
2013-05-30 16:35:16 +10:00
|
|
|
createElem = (tag) ->
|
|
|
|
|
$ "<#{tag}></#{tag}>"
|
2013-04-05 11:53:48 +11:00
|
|
|
inherit = (a, b) ->
|
|
|
|
|
F = () ->
|
|
|
|
|
F.prototype = a
|
|
|
|
|
$.extend true, new F(), b
|
2013-04-04 13:13:08 +11:00
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
# container for element-less notifications
|
2013-04-16 15:57:38 +10:00
|
|
|
cornerElem = createElem("div").addClass("#{className}Corner")
|
2013-04-04 16:58:22 +11:00
|
|
|
|
|
|
|
|
#gets first on n radios, and gets the fancy stylised input for hidden inputs
|
2013-04-04 13:13:08 +11:00
|
|
|
getAnchorElement = (element) ->
|
|
|
|
|
#choose the first of n radios
|
|
|
|
|
if element.is('[type=radio]')
|
|
|
|
|
radios = element.parents('form:first').find('[type=radio]').filter (i, e) ->
|
|
|
|
|
$(e).attr('name') is element.attr('name')
|
|
|
|
|
element = radios.first()
|
|
|
|
|
#custom-styled inputs - find thier real element
|
|
|
|
|
element
|
|
|
|
|
|
2013-05-31 13:18:43 +10:00
|
|
|
incr = (obj, pos, val) ->
|
2013-05-30 16:35:16 +10:00
|
|
|
# console.log "incr ---- #{pos} #{val} (#{typeof val})"
|
|
|
|
|
if typeof val is 'string'
|
|
|
|
|
val = parseInt val, 10
|
|
|
|
|
else if typeof val isnt 'number'
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
return if isNaN val
|
|
|
|
|
|
|
|
|
|
opp = positions[opposites[pos.charAt(0)]]
|
|
|
|
|
temp = pos
|
|
|
|
|
|
|
|
|
|
#use the opposite if exists
|
|
|
|
|
if obj[opp] isnt `undefined`
|
|
|
|
|
pos = positions[opp.charAt(0)]
|
|
|
|
|
val *= -1
|
|
|
|
|
|
|
|
|
|
if obj[pos] is `undefined`
|
|
|
|
|
obj[pos] = val
|
|
|
|
|
else
|
|
|
|
|
obj[pos] += val
|
|
|
|
|
null
|
|
|
|
|
|
2013-05-31 13:18:43 +10:00
|
|
|
realign = (alignment, inner, outer) ->
|
|
|
|
|
return if alignment in ['l','t']
|
|
|
|
|
0
|
|
|
|
|
else if alignment in ['c','m']
|
|
|
|
|
outer/2 - inner/2
|
|
|
|
|
else if alignment in ['r','b']
|
|
|
|
|
outer - inner
|
|
|
|
|
throw "Invalid alignment"
|
|
|
|
|
|
2013-04-05 11:53:48 +11:00
|
|
|
insertCSS = (style) ->
|
|
|
|
|
return unless style and style.css
|
|
|
|
|
elem = style.cssElem
|
2013-05-30 16:35:16 +10:00
|
|
|
return if elem
|
|
|
|
|
elem = createElem("style")
|
|
|
|
|
$("head").append elem
|
|
|
|
|
style.cssElem = elem
|
2013-04-05 11:53:48 +11:00
|
|
|
|
2013-04-16 15:57:38 +10:00
|
|
|
try
|
|
|
|
|
elem.html style.css
|
|
|
|
|
catch e #ie fix
|
|
|
|
|
elem[0].styleSheet.cssText = style.css
|
|
|
|
|
|
2013-04-04 13:13:08 +11:00
|
|
|
#define plugin
|
2013-04-04 16:58:22 +11:00
|
|
|
class Notification
|
2013-04-04 13:13:08 +11:00
|
|
|
|
|
|
|
|
#setup instance variables
|
2013-04-04 16:58:22 +11:00
|
|
|
constructor: (elem, data, options) ->
|
2013-04-05 11:53:48 +11:00
|
|
|
options = {color: options} if typeof options is 'string'
|
|
|
|
|
@options = inherit pluginOptions, if $.isPlainObject(options) then options else {}
|
2013-04-04 13:48:42 +11:00
|
|
|
|
|
|
|
|
#load user css into dom
|
|
|
|
|
@loadCSS()
|
2013-04-04 16:58:22 +11:00
|
|
|
#load user html into @userContainer
|
2013-04-04 13:48:42 +11:00
|
|
|
@loadHTML()
|
|
|
|
|
|
|
|
|
|
@wrapper = $(styles.core.html)
|
2013-04-05 12:26:27 +11:00
|
|
|
@wrapper.data className, @
|
2013-04-05 11:53:48 +11:00
|
|
|
@arrow = @wrapper.find ".#{className}Arrow"
|
2013-04-04 14:02:55 +11:00
|
|
|
@container = @wrapper.find ".#{className}Container"
|
|
|
|
|
@container.append @userContainer
|
2013-04-04 13:13:08 +11:00
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
if elem and elem.length
|
|
|
|
|
@elementType = elem.attr('type')
|
|
|
|
|
@originalElement = elem
|
|
|
|
|
@elem = getAnchorElement(elem)
|
2013-04-05 12:26:27 +11:00
|
|
|
@elem.data className, @
|
2013-04-04 16:58:22 +11:00
|
|
|
# add into dom above elem
|
|
|
|
|
@elem.before @wrapper
|
|
|
|
|
else
|
|
|
|
|
# @options.autoHide = true
|
|
|
|
|
@options.arrowShow = false
|
|
|
|
|
cornerElem.prepend @wrapper
|
2013-04-04 13:13:08 +11:00
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
@container.hide()
|
|
|
|
|
@run(data)
|
2013-04-04 13:13:08 +11:00
|
|
|
|
2013-04-04 13:48:42 +11:00
|
|
|
loadCSS: (style) ->
|
2013-04-05 11:53:48 +11:00
|
|
|
unless style
|
|
|
|
|
name = @options.style
|
|
|
|
|
style = @getStyle(name)
|
|
|
|
|
#insert
|
|
|
|
|
insertCSS style
|
2013-04-04 13:48:42 +11:00
|
|
|
|
2013-04-11 17:22:27 +10:00
|
|
|
loadHTML: ->
|
|
|
|
|
style = @getStyle()
|
2013-04-04 14:02:55 +11:00
|
|
|
@userContainer = $(style.html)
|
2013-05-30 16:35:16 +10:00
|
|
|
@text = @userContainer.find '[data-notify-text]'
|
2013-04-04 13:48:42 +11:00
|
|
|
if @text.length is 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
|
|
|
@text.addClass "#{className}Text"
|
|
|
|
|
|
2013-04-05 15:34:55 +11:00
|
|
|
show: (show, callback = $.noop) ->
|
2013-04-05 12:26:27 +11:00
|
|
|
|
2013-04-04 13:48:42 +11:00
|
|
|
hidden = @container.parent().parents(':hidden').length > 0
|
2013-04-05 12:26:27 +11:00
|
|
|
|
|
|
|
|
elems = @container.add @arrow
|
2013-04-05 15:34:55 +11:00
|
|
|
args = []
|
|
|
|
|
|
|
|
|
|
if hidden and show
|
|
|
|
|
fn = 'show'
|
|
|
|
|
else if hidden and not show
|
|
|
|
|
fn = 'hide'
|
|
|
|
|
else if not hidden and show
|
|
|
|
|
fn = @options.showAnimation
|
|
|
|
|
args.push @options.showDuration
|
|
|
|
|
else if not hidden and not show
|
|
|
|
|
fn = @options.hideAnimation
|
|
|
|
|
args.push @options.hideDuration
|
2013-05-31 13:18:43 +10:00
|
|
|
else
|
|
|
|
|
return callback()
|
2013-04-05 15:34:55 +11:00
|
|
|
|
|
|
|
|
args.push callback
|
|
|
|
|
|
2013-04-16 15:57:38 +10:00
|
|
|
elems[fn].apply elems, args
|
2013-04-04 13:13:08 +11:00
|
|
|
|
2013-04-05 11:53:48 +11:00
|
|
|
updatePosition: ->
|
|
|
|
|
return unless @elem
|
2013-05-30 16:35:16 +10:00
|
|
|
|
2013-05-31 13:18:43 +10:00
|
|
|
#grab some dimensions
|
|
|
|
|
elemPos = @elem.position()
|
|
|
|
|
elemH = @elem.outerHeight()
|
|
|
|
|
elemW = @elem.outerWidth()
|
|
|
|
|
wrapPos = @wrapper.position()
|
|
|
|
|
contH = @container.height()
|
|
|
|
|
contW = @container.width()
|
|
|
|
|
|
|
|
|
|
#get user defined position
|
2013-04-05 11:53:48 +11:00
|
|
|
position = @getPosition()
|
2013-05-31 13:18:43 +10:00
|
|
|
console.log position
|
|
|
|
|
pMain = position[0]
|
|
|
|
|
pAlign = position[1]
|
|
|
|
|
pArrow = position[2] or pAlign
|
2013-05-30 16:35:16 +10:00
|
|
|
|
2013-04-05 11:53:48 +11:00
|
|
|
#start calculations
|
2013-05-31 13:18:43 +10:00
|
|
|
mainFull = positions[pMain]
|
|
|
|
|
opp = opposites[pMain]
|
|
|
|
|
oppFull = positions[opp]
|
|
|
|
|
#initial positioning
|
|
|
|
|
css = {}
|
|
|
|
|
css[oppFull] = if pMain is 'b' then elemH else
|
|
|
|
|
if pMain is 'r' then elemW else 0
|
2013-04-05 11:53:48 +11:00
|
|
|
|
2013-05-31 13:18:43 +10:00
|
|
|
#correct for elem-wrapper offset
|
|
|
|
|
# unless navigator.userAgent.match /MSIE/
|
|
|
|
|
# incr css, 'top', (elemPos.top - wrapPos.top)
|
|
|
|
|
incr css, 'left', elemPos.left - wrapPos.left
|
2013-04-05 11:53:48 +11:00
|
|
|
|
2013-05-31 13:18:43 +10:00
|
|
|
#correct for left margin
|
|
|
|
|
margin = parseInt @elem.css("margin-left"), 10
|
|
|
|
|
incr css, 'left', margin if margin
|
|
|
|
|
#correct for inline top padding
|
|
|
|
|
if /^inline/.test @elem.css('display')
|
|
|
|
|
padding = parseInt @elem.css("padding-top"), 10
|
|
|
|
|
incr css, 'top', -padding if padding
|
|
|
|
|
#user defined offset
|
|
|
|
|
incr css, 'top', @options.offsetY
|
|
|
|
|
incr css, 'left', @options.offsetX
|
2013-04-05 11:53:48 +11:00
|
|
|
|
2013-05-31 13:18:43 +10:00
|
|
|
#calculate arrow
|
|
|
|
|
if not @options.arrowShow
|
2013-04-05 11:53:48 +11:00
|
|
|
@arrow.hide()
|
2013-05-31 13:18:43 +10:00
|
|
|
else
|
|
|
|
|
size = @options.arrowSize
|
|
|
|
|
arrowCss = $.extend {}, css
|
|
|
|
|
#build arrow
|
|
|
|
|
for pos in mainPositions
|
|
|
|
|
posFull = positions[pos]
|
|
|
|
|
continue if pos is opp
|
|
|
|
|
color = if posFull is mainFull then @getColor() else 'transparent'
|
|
|
|
|
arrowCss["border-#{posFull}"] = "#{size}px solid #{color}"
|
|
|
|
|
#add some room for the arrow
|
|
|
|
|
incr css, positions[opp], size
|
|
|
|
|
#add styles
|
|
|
|
|
@arrow.css(arrowCss).show()
|
2013-04-05 11:53:48 +11:00
|
|
|
|
2013-05-31 13:18:43 +10:00
|
|
|
#calculate alignment
|
|
|
|
|
if pMain in vPositions
|
|
|
|
|
incr css, 'left', realign(pAlign, contW, elemW)
|
|
|
|
|
if pMain in hPositions
|
|
|
|
|
incr css, 'top', realign(pAlign, contH, elemH)
|
2013-04-05 11:53:48 +11:00
|
|
|
|
2013-05-31 13:18:43 +10:00
|
|
|
#apply css
|
|
|
|
|
css.display = 'block' if @container.is(":visible")
|
|
|
|
|
@container.removeAttr('style').css css
|
2013-04-05 11:53:48 +11:00
|
|
|
|
|
|
|
|
getPosition: ->
|
2013-05-29 16:59:19 +10:00
|
|
|
text = @options.position
|
|
|
|
|
pos = parsePosition text
|
|
|
|
|
|
|
|
|
|
#if unspecified - choose bottom left
|
2013-05-31 13:18:43 +10:00
|
|
|
pos[0] = 'b' if pos.length is 0
|
|
|
|
|
|
|
|
|
|
if pos[0] not in mainPositions
|
|
|
|
|
throw "Must be one of [#{mainPositions}]"
|
|
|
|
|
|
|
|
|
|
if pos.length is 1 or
|
|
|
|
|
(pos[0] in vPositions and pos[1] not in hPositions) or
|
|
|
|
|
(pos[0] in hPositions and pos[1] not in vPositions)
|
|
|
|
|
pos[1] = if pos[0] in hPositions then 'm' else 'l'
|
2013-05-29 16:59:19 +10:00
|
|
|
|
|
|
|
|
unless @options.autoReposition
|
|
|
|
|
return pos
|
|
|
|
|
|
|
|
|
|
# if @elem.position().left + @elem.width() + @wrapper.width() < $(window).width()
|
|
|
|
|
# return 'right'
|
|
|
|
|
# return 'bottom'
|
|
|
|
|
|
|
|
|
|
throw "Not implemented"
|
2013-04-04 13:13:08 +11:00
|
|
|
|
|
|
|
|
getColor: ->
|
2013-04-04 16:58:22 +11:00
|
|
|
styleColors = @getStyle().colors
|
|
|
|
|
return (styleColors and
|
|
|
|
|
styleColors[@options.color]) or
|
|
|
|
|
@options.colors[@options.color] or
|
|
|
|
|
@options.color
|
2013-04-04 13:13:08 +11:00
|
|
|
|
2013-04-04 13:48:42 +11:00
|
|
|
getStyle: (name) ->
|
|
|
|
|
name = @options.style unless name
|
2013-04-05 11:53:48 +11:00
|
|
|
name = 'default' unless name
|
2013-04-04 13:48:42 +11:00
|
|
|
style = styles.user[name]
|
|
|
|
|
throw "Missing style: #{name}" unless style
|
|
|
|
|
style
|
|
|
|
|
|
2013-04-05 11:53:48 +11:00
|
|
|
updateStyle: ->
|
|
|
|
|
#update colors
|
|
|
|
|
@wrapper.find('[data-notify-style]').each (i,e) =>
|
|
|
|
|
$(e).attr 'style', $(e).attr('data-notify-style').
|
2013-05-29 16:59:19 +10:00
|
|
|
replace(/\{\{\s*color\s*\}\}/ig, @getColor())
|
|
|
|
|
# .
|
|
|
|
|
# replace(/\{\{\s*position\s*\}\}/ig, @getPosition())
|
2013-04-05 11:53:48 +11:00
|
|
|
|
2013-04-04 13:13:08 +11:00
|
|
|
#run plugin
|
2013-04-04 16:58:22 +11:00
|
|
|
run: (data, options) ->
|
2013-04-04 13:13:08 +11:00
|
|
|
#update options
|
|
|
|
|
if $.isPlainObject(options)
|
|
|
|
|
$.extend @options, options
|
|
|
|
|
#shortcut special case
|
|
|
|
|
else if $.type(options) is 'string'
|
|
|
|
|
@options.color = options
|
|
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
if @container and not data
|
|
|
|
|
@show false #hide
|
2013-04-04 13:13:08 +11:00
|
|
|
return
|
2013-04-04 16:58:22 +11:00
|
|
|
else if not @container and not data
|
2013-04-04 13:13:08 +11:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
#update content
|
2013-04-04 16:58:22 +11:00
|
|
|
if $.type(data) is 'string'
|
|
|
|
|
@text.html data.replace('\n', '<br/>')
|
2013-04-04 13:13:08 +11:00
|
|
|
else
|
2013-04-04 16:58:22 +11:00
|
|
|
@text.empty().append(data)
|
2013-04-04 14:02:55 +11:00
|
|
|
|
2013-04-05 11:53:48 +11:00
|
|
|
@updatePosition()
|
2013-04-04 13:13:08 +11:00
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
@show true
|
2013-04-04 13:13:08 +11:00
|
|
|
|
|
|
|
|
#autohide
|
2013-04-04 16:58:22 +11:00
|
|
|
if @options.autoHide
|
|
|
|
|
clearTimeout @autohideTimer
|
2013-05-31 13:18:43 +10:00
|
|
|
@autohideTimer = setTimeout =>
|
2013-04-04 16:58:22 +11:00
|
|
|
@show false
|
2013-04-04 13:13:08 +11:00
|
|
|
, @options.autoHideDelay
|
|
|
|
|
|
2013-04-05 15:34:55 +11:00
|
|
|
destroy: ->
|
|
|
|
|
@show false, =>
|
|
|
|
|
@wrapper.remove()
|
|
|
|
|
|
2013-04-05 12:26:27 +11:00
|
|
|
#when ready
|
2013-04-04 13:13:08 +11:00
|
|
|
$ ->
|
2013-04-05 12:26:27 +11:00
|
|
|
#corner notification container
|
2013-04-04 16:58:22 +11:00
|
|
|
$("body").append cornerElem
|
|
|
|
|
|
|
|
|
|
#auto-detect bootstrap
|
|
|
|
|
$("link").each ->
|
|
|
|
|
src = $(@).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 12:26:27 +11:00
|
|
|
|
2013-04-04 13:48:42 +11:00
|
|
|
#add core styles
|
2013-04-05 11:53:48 +11:00
|
|
|
insertCSS styles.core
|
|
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
#watch all notifications clicks
|
|
|
|
|
$(document).on 'click', ".#{className}Wrapper", ->
|
2013-04-05 12:26:27 +11:00
|
|
|
inst = $(@).data className
|
2013-04-05 15:34:55 +11:00
|
|
|
return unless inst
|
|
|
|
|
if inst.elem
|
|
|
|
|
inst.show false
|
|
|
|
|
else
|
|
|
|
|
inst.destroy()
|
2013-04-04 13:13:08 +11:00
|
|
|
|
|
|
|
|
# publicise jquery plugin
|
|
|
|
|
# return alert "$.#{pluginName} already defined" if $[pluginName]?
|
|
|
|
|
# $.pluginName( { ... } ) changes options for all instances
|
2013-04-04 16:58:22 +11:00
|
|
|
$[pluginName] = (elem, data, options) ->
|
2013-04-16 15:57:38 +10:00
|
|
|
if (elem and elem.nodeName) or elem.jquery
|
2013-04-04 16:58:22 +11:00
|
|
|
$(elem)[pluginName](data, options)
|
|
|
|
|
else
|
|
|
|
|
options = data
|
|
|
|
|
data = elem
|
2013-05-29 16:59:19 +10:00
|
|
|
new Notification null, data, options
|
|
|
|
|
elem
|
2013-04-04 13:13:08 +11:00
|
|
|
|
2013-04-05 12:26:27 +11:00
|
|
|
# publicise methods
|
2013-04-04 13:13:08 +11:00
|
|
|
$[pluginName].options = (options) ->
|
|
|
|
|
$.extend pluginOptions, options
|
|
|
|
|
|
2013-04-05 12:26:27 +11:00
|
|
|
$[pluginName].styles = (s) ->
|
2013-04-04 13:48:42 +11:00
|
|
|
$.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 13:13:08 +11:00
|
|
|
# $( ... ).pluginName( { .. } ) creates a cached instance on each
|
2013-04-04 16:58:22 +11:00
|
|
|
$.fn[pluginName] = (data, options) ->
|
2013-04-04 13:13:08 +11:00
|
|
|
$(@).each ->
|
2013-05-30 16:35:16 +10:00
|
|
|
inst = getAnchorElement($(@)).data(className)
|
2013-04-04 16:58:22 +11:00
|
|
|
if inst
|
|
|
|
|
inst.run data, options
|
2013-04-04 13:13:08 +11:00
|
|
|
else
|
2013-04-04 16:58:22 +11:00
|
|
|
new Notification $(@), data, options
|
2013-05-30 16:35:16 +10:00
|
|
|
|
2013-05-29 16:59:19 +10:00
|
|
|
@
|
2013-04-04 13:13:08 +11:00
|
|
|
|