2013-04-04 13:13:08 +11:00
|
|
|
|
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
|
|
#plugin constants
|
|
|
|
|
pluginName = 'notify'
|
2013-06-03 12:28:03 +10:00
|
|
|
className = pluginName+'js'
|
2013-04-04 13:13:08 +11:00
|
|
|
|
2013-06-03 12:28:03 +10:00
|
|
|
# ================================
|
|
|
|
|
# POSITIONING
|
|
|
|
|
# ================================
|
2013-05-30 16:35:16 +10:00
|
|
|
|
2013-06-02 23:19:34 +10:00
|
|
|
positions =
|
2013-05-30 16:35:16 +10:00
|
|
|
t: 'top'
|
|
|
|
|
m: 'middle'
|
|
|
|
|
b: 'bottom'
|
|
|
|
|
l: 'left'
|
|
|
|
|
c: 'center'
|
|
|
|
|
r: 'right'
|
2013-06-03 09:15:58 +10:00
|
|
|
hAligns = ['l','c','r']
|
|
|
|
|
vAligns = ['t','m','b']
|
2013-05-31 13:18:43 +10:00
|
|
|
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
|
|
|
|
|
|
2013-06-03 12:28:03 +10:00
|
|
|
# ================================
|
|
|
|
|
# STYLES
|
|
|
|
|
# ================================
|
|
|
|
|
styles = {}
|
|
|
|
|
|
|
|
|
|
coreStyle =
|
|
|
|
|
name: 'core'
|
|
|
|
|
html: """
|
|
|
|
|
<div class="#{className}-wrapper">
|
|
|
|
|
<div class="#{className}-arrow"></div>
|
|
|
|
|
<div class="#{className}-container"></div>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# <div class="#{className}Debug"></div>
|
|
|
|
|
|
|
|
|
|
# .#{className}Debug {
|
|
|
|
|
# position: absolute;
|
|
|
|
|
# border: 3px solid red;
|
|
|
|
|
# height: 0;
|
|
|
|
|
# width: 0;
|
|
|
|
|
# }
|
|
|
|
|
|
|
|
|
|
css: """
|
|
|
|
|
.#{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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.#{className}-wrapper {
|
|
|
|
|
z-index: 1;
|
|
|
|
|
position: absolute;
|
|
|
|
|
display: inline-block;
|
|
|
|
|
height: 0;
|
|
|
|
|
width: 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.#{className}-container {
|
|
|
|
|
display: none;
|
|
|
|
|
z-index: 1;
|
|
|
|
|
position: absolute;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.#{className}-text {
|
|
|
|
|
position: relative;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.#{className}-arrow {
|
|
|
|
|
position: absolute;
|
|
|
|
|
z-index: 2;
|
|
|
|
|
width: 0;
|
|
|
|
|
height: 0;
|
|
|
|
|
}
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
defaultStyleName = "bootstrap"
|
|
|
|
|
defaultStyle =
|
|
|
|
|
html: """
|
|
|
|
|
<div>
|
|
|
|
|
<span data-notify-text></span>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
classes:
|
|
|
|
|
base: """
|
|
|
|
|
padding: 8px 35px 8px 14px;
|
|
|
|
|
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
|
|
|
|
|
background-color: #fcf8e3;
|
|
|
|
|
border: 1px solid #fbeed5;
|
|
|
|
|
-webkit-border-radius: 4px;
|
|
|
|
|
-moz-border-radius: 4px;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
white-space: nowrap;
|
|
|
|
|
"""
|
|
|
|
|
error: """
|
|
|
|
|
color: #B94A48;
|
|
|
|
|
background-color: #F2DEDE;
|
|
|
|
|
border-color: #EED3D7;
|
|
|
|
|
padding-left: 25px;
|
|
|
|
|
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=);
|
|
|
|
|
"""
|
|
|
|
|
info: ""
|
|
|
|
|
warn: ""
|
|
|
|
|
|
|
|
|
|
addStyle = (name, def) ->
|
|
|
|
|
if styles[name]
|
|
|
|
|
alert "#{pluginName}: style '#{name}' already defined"
|
|
|
|
|
return
|
|
|
|
|
def.name = name
|
|
|
|
|
styles[name] = def
|
|
|
|
|
|
|
|
|
|
return if def.cssElem
|
|
|
|
|
|
|
|
|
|
cssText = ""
|
|
|
|
|
if def.classes
|
|
|
|
|
$.each def.classes, (subClassName, css) ->
|
|
|
|
|
cssText += """
|
|
|
|
|
.#{className}-#{def.name}-#{subClassName} {
|
|
|
|
|
#{css}
|
|
|
|
|
}\n
|
|
|
|
|
"""
|
|
|
|
|
if def.css
|
|
|
|
|
cssText += """
|
|
|
|
|
/* styles for #{def.name} */
|
|
|
|
|
#{def.css}
|
2013-04-04 13:48:42 +11:00
|
|
|
"""
|
2013-06-03 09:15:58 +10:00
|
|
|
|
2013-06-03 12:28:03 +10:00
|
|
|
return unless cssText
|
|
|
|
|
def.cssElem = insertCSS cssText
|
|
|
|
|
def.cssElem.attr('data-notify-style', def.name)
|
2013-06-03 09:15:58 +10:00
|
|
|
|
2013-06-03 12:28:03 +10:00
|
|
|
insertCSS = (cssText) ->
|
|
|
|
|
elem = createElem("style")
|
|
|
|
|
$("head").append elem
|
|
|
|
|
try
|
|
|
|
|
elem.html cssText
|
|
|
|
|
catch e #ie fix
|
|
|
|
|
elem[0].styleSheet.cssText = cssText
|
|
|
|
|
elem
|
2013-06-03 09:15:58 +10:00
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
|
2013-06-03 12:28:03 +10:00
|
|
|
# ================================
|
|
|
|
|
# OPTIONS
|
|
|
|
|
# ================================
|
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-06-03 12:28:03 +10:00
|
|
|
arrowColor: 'border'
|
2013-04-05 15:09:44 +11:00
|
|
|
position: 'bottom'
|
2013-04-04 13:48:42 +11:00
|
|
|
# Default style
|
2013-06-03 12:28:03 +10:00
|
|
|
style: 'bootstrap'
|
|
|
|
|
cls: 'error'
|
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 }
|
|
|
|
|
|
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-06-03 12:28:03 +10:00
|
|
|
options = (opts) ->
|
|
|
|
|
$.extend pluginOptions, opts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ================================
|
|
|
|
|
# DOM MANIPULATION
|
|
|
|
|
# ================================
|
|
|
|
|
|
|
|
|
|
# plugin helpers
|
|
|
|
|
createElem = (tag) ->
|
|
|
|
|
$ "<#{tag}></#{tag}>"
|
|
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
# container for element-less notifications
|
2013-06-03 12:28:03 +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
|
|
|
|
|
|
2013-06-02 23:19:34 +10:00
|
|
|
opp = positions[opposites[pos.charAt(0)]]
|
2013-05-30 16:35:16 +10:00
|
|
|
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
|
|
|
|
2013-06-03 12:28:03 +10:00
|
|
|
# ================================
|
|
|
|
|
# NOTIFY CLASS
|
|
|
|
|
# ================================
|
2013-04-16 15:57:38 +10:00
|
|
|
|
2013-04-04 13:13:08 +11:00
|
|
|
#define plugin
|
2013-04-04 16:58:22 +11:00
|
|
|
class Notification
|
2013-06-02 23:19:34 +10:00
|
|
|
|
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
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
#load user html into @userContainer
|
2013-04-04 13:48:42 +11:00
|
|
|
@loadHTML()
|
|
|
|
|
|
2013-06-03 12:28:03 +10:00
|
|
|
@wrapper = $(coreStyle.html)
|
2013-04-05 12:26:27 +11:00
|
|
|
@wrapper.data className, @
|
2013-06-03 12:28:03 +10:00
|
|
|
@arrow = @wrapper.find ".#{className}-arrow"
|
|
|
|
|
@container = @wrapper.find ".#{className}-container"
|
2013-04-04 14:02:55 +11:00
|
|
|
@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-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-06-03 12:28:03 +10:00
|
|
|
@text.addClass "#{className}-text"
|
2013-04-04 13:48:42 +11:00
|
|
|
|
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-06-02 23:19:34 +10:00
|
|
|
|
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-06-02 23:19:34 +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()
|
2013-06-03 09:15:58 +10:00
|
|
|
elemIH = @elem.innerHeight()
|
|
|
|
|
elemIW = @elem.innerWidth()
|
2013-05-31 13:18:43 +10:00
|
|
|
wrapPos = @wrapper.position()
|
|
|
|
|
contH = @container.height()
|
|
|
|
|
contW = @container.width()
|
|
|
|
|
|
|
|
|
|
#get user defined position
|
2013-04-05 11:53:48 +11:00
|
|
|
position = @getPosition()
|
2013-06-03 09:15:58 +10:00
|
|
|
|
2013-06-03 12:28:03 +10:00
|
|
|
console.log position
|
|
|
|
|
|
2013-05-31 13:18:43 +10:00
|
|
|
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-06-02 23:19:34 +10: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-06-03 12:28:03 +10:00
|
|
|
style = @getStyle()
|
|
|
|
|
|
|
|
|
|
# if style.css
|
|
|
|
|
|
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
|
2013-06-03 09:15:58 +10:00
|
|
|
arrowSize = @options.arrowSize
|
2013-05-31 13:18:43 +10:00
|
|
|
arrowCss = $.extend {}, css
|
|
|
|
|
#build arrow
|
|
|
|
|
for pos in mainPositions
|
|
|
|
|
posFull = positions[pos]
|
|
|
|
|
continue if pos is opp
|
2013-06-03 12:28:03 +10:00
|
|
|
color = if posFull is mainFull then 'black' else 'transparent'
|
2013-06-03 09:15:58 +10:00
|
|
|
arrowCss["border-#{posFull}"] = "#{arrowSize}px solid #{color}"
|
2013-05-31 13:18:43 +10:00
|
|
|
#add some room for the arrow
|
2013-06-03 09:15:58 +10:00
|
|
|
incr css, positions[opp], arrowSize
|
2013-04-05 11:53:48 +11:00
|
|
|
|
2013-06-03 09:15:58 +10:00
|
|
|
incr arrowCss, positions[pAlign], arrowSize*2 if pAlign in mainPositions
|
|
|
|
|
|
|
|
|
|
#calculate container alignment
|
|
|
|
|
if pMain in vAligns
|
2013-05-31 13:18:43 +10:00
|
|
|
incr css, 'left', realign(pAlign, contW, elemW)
|
2013-06-03 09:15:58 +10:00
|
|
|
incr arrowCss, 'left', realign(pAlign, arrowSize, elemIW) if arrowCss
|
|
|
|
|
else if pMain in hAligns
|
2013-05-31 13:18:43 +10:00
|
|
|
incr css, 'top', realign(pAlign, contH, elemH)
|
2013-06-03 09:15:58 +10:00
|
|
|
incr arrowCss, 'top', realign(pAlign, arrowSize, elemIH) if arrowCss
|
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-06-03 09:15:58 +10:00
|
|
|
@arrow.removeAttr('style').css(arrowCss) if arrowCss
|
2013-04-05 11:53:48 +11:00
|
|
|
|
|
|
|
|
getPosition: ->
|
2013-05-29 16:59:19 +10:00
|
|
|
text = @options.position
|
|
|
|
|
pos = parsePosition text
|
|
|
|
|
|
2013-06-03 09:15:58 +10:00
|
|
|
#validate position
|
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}]"
|
2013-06-03 12:28:03 +10:00
|
|
|
|
2013-06-03 09:15:58 +10:00
|
|
|
#validate alignment
|
2013-05-31 13:18:43 +10:00
|
|
|
if pos.length is 1 or
|
2013-06-03 09:15:58 +10:00
|
|
|
(pos[0] in vAligns and pos[1] not in hAligns) or
|
|
|
|
|
(pos[0] in hAligns and pos[1] not in vAligns)
|
|
|
|
|
pos[1] = if pos[0] in hAligns 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
|
|
|
|
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-06-03 12:28:03 +10:00
|
|
|
style = styles[name]
|
2013-04-04 13:48:42 +11:00
|
|
|
throw "Missing style: #{name}" unless style
|
|
|
|
|
style
|
|
|
|
|
|
2013-06-03 12:28:03 +10:00
|
|
|
updateStyleClasses: ->
|
|
|
|
|
cls = ['base']
|
|
|
|
|
|
|
|
|
|
if $.isArray @options.cls
|
|
|
|
|
cls = cls.concat @options.cls
|
|
|
|
|
else if @options.cls
|
|
|
|
|
cls.push @options.cls
|
|
|
|
|
|
|
|
|
|
style = @getStyle()
|
|
|
|
|
cls = $.map(cls, (n) -> "#{className}-#{style.name}-#{n}").join ' '
|
|
|
|
|
|
|
|
|
|
@userContainer.attr 'class', cls
|
|
|
|
|
|
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)
|
2013-06-02 23:19:34 +10:00
|
|
|
$.extend @options, options
|
2013-04-04 13:13:08 +11:00
|
|
|
#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-06-03 12:28:03 +10:00
|
|
|
#set styles
|
|
|
|
|
@updateStyleClasses()
|
|
|
|
|
|
|
|
|
|
#position
|
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-06-03 12:28:03 +10:00
|
|
|
|
|
|
|
|
# ================================
|
|
|
|
|
# API
|
|
|
|
|
# ================================
|
|
|
|
|
|
|
|
|
|
# $.pluginName( { ... } ) changes options for all instances
|
|
|
|
|
$[pluginName] = (elem, data, options) ->
|
|
|
|
|
if (elem and elem.nodeName) or elem.jquery
|
|
|
|
|
$(elem)[pluginName](data, options)
|
|
|
|
|
else
|
|
|
|
|
options = data
|
|
|
|
|
data = elem
|
|
|
|
|
new Notification null, data, options
|
|
|
|
|
elem
|
|
|
|
|
|
|
|
|
|
# $( ... ).pluginName( { .. } ) creates a cached instance on each
|
|
|
|
|
$.fn[pluginName] = (data, options) ->
|
|
|
|
|
$(@).each ->
|
|
|
|
|
inst = getAnchorElement($(@)).data(className)
|
|
|
|
|
if inst
|
|
|
|
|
inst.run data, options
|
|
|
|
|
else
|
|
|
|
|
new Notification $(@), data, options
|
|
|
|
|
@
|
|
|
|
|
|
|
|
|
|
#extra
|
|
|
|
|
$.extend $[pluginName], { options, addStyle }
|
|
|
|
|
|
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-06-03 12:28:03 +10:00
|
|
|
#insert default style
|
|
|
|
|
insertCSS(coreStyle.css).attr('data-notify-style', 'core')
|
|
|
|
|
#insert default style
|
|
|
|
|
$[pluginName].addStyle(defaultStyleName, defaultStyle)
|
2013-04-05 11:53:48 +11:00
|
|
|
|
2013-04-04 16:58:22 +11:00
|
|
|
#watch all notifications clicks
|
2013-06-03 12:28:03 +10:00
|
|
|
$(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
|
|
|
|