Files
jquery-notify/src/notify.coffee
T

342 lines
8.7 KiB
CoffeeScript
Raw Normal View History

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
arrowDirs =
top: 'bottom'
bottom: 'top'
left: 'right'
right: 'left'
2013-04-04 13:48:42 +11:00
styles =
core:
html: """
2013-04-04 14:02:55 +11:00
<div class="#{className}Wrapper">
<div class="#{className}Container">
</div>
</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-04-04 16:58:22 +11:00
opacity: 0.85;
2013-04-04 13:48:42 +11:00
}
.#{className}Container {
display: none;
z-index: 1;
position: absolute;
cursor: pointer;
}
.#{className}Text {
position: relative;
}
"""
user:
default:
html: """
2013-04-04 16:58:22 +11:00
<div class="#{className}Default"
data-notify-style="
color: {{color}};
border-color: {{color}};
">
2013-04-04 14:02:55 +11:00
<span data-notify="text"></span>
</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">
<strong>Warning!</strong> <span data-notify="text"></span>
</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-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-04-04 13:13:08 +11:00
arrowShow: true
arrowSize: 5
arrowPosition: 'top'
2013-04-04 13:48:42 +11:00
# Default style
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-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 between main and element
gap: 2
#TODO add z-index watches
#parents: { '.ui-dialog': 5001 }
# plugin helpers
create = (tag) ->
$ document.createElement(tag)
# inherit plugin options
2013-04-04 13:48:42 +11:00
Options = (options) -> $.extend @, options
2013-04-04 13:13:08 +11:00
Options:: = pluginOptions
2013-04-04 16:58:22 +11:00
# container for element-less notifications
cornerElem = create("div").addClass("#{className}Corner")
#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
fBefore = element.prev()
element = fBefore if fBefore.is('span.styled,span.OBS_checkbox')
element
#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-04 13:13:08 +11:00
options = {color: options} if $.type(options) is 'string'
@options = new Options 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-04 16:58:22 +11:00
@wrapper.data pluginName, @
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)
@elem.data pluginName, @
# add into dom above elem
@elem.before @wrapper
@container.css @calculateCSS()
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) ->
name = @options.style
style = @getStyle(name)
elem = style.cssElem
if elem
elem.html style.css
else
elem = create("style").attr('id', "#{name}-styles").html style.css
$("head").append elem
style.cssElem = elem
loadHTML: (style) ->
style = @getStyle(name)
2013-04-04 14:02:55 +11:00
@userContainer = $(style.html)
@text = @userContainer.find '[data-notify=text]'
2013-04-04 13:48:42 +11:00
if @text.length is 0
throw "style: #{name} HTML is missing the: data-notify='text' attribute"
@text.addClass "#{className}Text"
2013-04-04 13:13:08 +11:00
buildArrow: ->
dir = @options.arrowPosition
size = @options.arrowSize
alt = arrowDirs[dir]
@arrow = create("div")
@arrow.addClass(className + 'Arrow').css(
'margin-top': 2 + (if document.documentMode is 5 then (size*-4) else 0)
'position': 'relative'
'z-index': '2'
'margin-left': 10
'width': 0
'height': 0
).css(
'border-' + alt, size + 'px solid ' + @getColor()
)
for d of arrowDirs
@arrow.css 'border-' + d, size + 'px solid transparent' if d isnt dir and d isnt alt
showArrow = @options.arrowShow and @elementType isnt 'radio'
if showArrow then @arrow.show() else @arrow.hide()
2013-04-04 16:58:22 +11:00
show: (show) ->
2013-04-04 13:48:42 +11:00
hidden = @container.parent().parents(':hidden').length > 0
@container.show() if hidden and show
@container.hide() if hidden and not show
@container[@options.showAnimation] @options.showDuration if not hidden and show
@container[@options.hideAnimation] @options.hideDuration if not hidden and not show
2013-04-04 13:13:08 +11:00
calculateCSS: () ->
elementPosition = @elem.position()
2013-04-04 13:48:42 +11:00
mainPosition = @container.parent().position()
2013-04-04 13:13:08 +11:00
height = @elem.outerHeight()
left = elementPosition.left - mainPosition.left
height += (elementPosition.top - mainPosition.top) unless navigator.userAgent.match /MSIE/
return {
top: height + @options.gap
left: left
}
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
style = styles.user[name]
throw "Missing style: #{name}" unless style
style
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
color = @getColor()
@container.find('[data-notify-style]').each ->
s = $(@).attr 'data-notify-style'
2013-04-04 16:58:22 +11:00
$(@).attr 'style', s.replace /\{\{\s*color\s*\}\}/ig, color
2013-04-04 13:13:08 +11:00
@arrow.remove() if @arrow
@buildArrow()
2013-04-04 14:02:55 +11:00
@container.prepend @arrow
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
autohideTimer = setTimeout =>
@show false
2013-04-04 13:13:08 +11:00
, @options.autoHideDelay
#when ready, bind permanent hide listener
$ ->
2013-04-04 16:58:22 +11:00
$("body").append cornerElem
#auto-detect bootstrap
$("link").each ->
src = $(@).attr 'href'
if src.match /bootstrap[^\/]*\.css/
$[pluginName].options {style:'bootstrap'}
return false
2013-04-04 13:48:42 +11:00
#add core styles
$("head").append(create("style").html(styles.core.css))
2013-04-04 16:58:22 +11:00
#watch all notifications clicks
$(document).on 'click', ".#{className}Wrapper", ->
inst = $(@).data pluginName
inst.show false if inst
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) ->
if elem instanceof HTMLElement or
elem.jquery
$(elem)[pluginName](data, options)
else
options = data
data = elem
new Notification null, data, options
2013-04-04 13:13:08 +11:00
# publicise options method
$[pluginName].options = (options) ->
$.extend pluginOptions, options
$[pluginName].addStyle = (s) ->
2013-04-04 13:48:42 +11:00
$.extend true, styles.user, s
2013-04-04 13:13:08 +11:00
# $( ... ).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]?
2013-04-04 16:58:22 +11:00
$.fn[pluginName] = (data, options) ->
2013-04-04 13:13:08 +11:00
$(@).each ->
inst = getAnchorElement($(@)).data pluginName
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-04-04 13:13:08 +11:00