1
+ /** Notify.js - v0.0.1 - 2013/04/04
2
+ * http://notifyjs.com/
3
+ * Copyright (c) 2013 Jaime Pillora - MIT
4
+ */
5
+ ( function ( window , document , undefined ) {
6
+ 'use strict' ;
7
+
8
+ var Options , Prompt , arrowDirs , className , coreStyle , create , getAnchorElement , pluginName , pluginOptions , userStyles ;
9
+
10
+ pluginName = 'notify' ;
11
+
12
+ className = '__notify' ;
13
+
14
+ arrowDirs = {
15
+ top : 'bottom' ,
16
+ bottom : 'top' ,
17
+ left : 'right' ,
18
+ right : 'left'
19
+ } ;
20
+
21
+ coreStyle = {
22
+ html : "<div class=\"" + className + "Wrapper\">\n <div class=\"" + className + "Main\">\n <div class=\"" + className + "Content\">\n </div>\n </div>\n</div>" ,
23
+ css : "." + className + "Wrapper {\n z-index: 1;\n position: absolute;\n display: inline-block;\n height: 0;\n width: 0;\n}\n\n." + className + "Main {\n display: none;\n z-index: 1;\n position: absolute;\n cursor: pointer;\n}\n\n." + className + "Content {\n background: #fff;\n position: relative;\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}"
24
+ } ;
25
+
26
+ userStyles = {
27
+ "default" : {
28
+ html : "<span>test</span>" ,
29
+ css : "body {\n test: 42\n}"
30
+ } ,
31
+ bootstrap : {
32
+ html : "<span>test</span>" ,
33
+ css : "body {\n test: 42\n}"
34
+ }
35
+ } ;
36
+
37
+ pluginOptions = {
38
+ autoHidePrompt : false ,
39
+ autoHideDelay : 10000 ,
40
+ arrowShow : true ,
41
+ arrowSize : 5 ,
42
+ arrowPosition : 'top' ,
43
+ color : 'red' ,
44
+ colors : {
45
+ red : '#ee0101' ,
46
+ green : '#33be40' ,
47
+ black : '#393939' ,
48
+ blue : '#00f'
49
+ } ,
50
+ showAnimation : 'fadeIn' ,
51
+ showDuration : 200 ,
52
+ hideAnimation : 'fadeOut' ,
53
+ hideDuration : 600 ,
54
+ gap : 2
55
+ } ;
56
+
57
+ create = function ( tag ) {
58
+ return $ ( document . createElement ( tag ) ) ;
59
+ } ;
60
+
61
+ Options = function ( options ) {
62
+ if ( $ . isPlainObject ( options ) ) {
63
+ return $ . extend ( this , options ) ;
64
+ }
65
+ } ;
66
+
67
+ Options . prototype = pluginOptions ;
68
+
69
+ getAnchorElement = function ( element ) {
70
+ var fBefore , radios ;
71
+ if ( element . is ( '[type=radio]' ) ) {
72
+ radios = element . parents ( 'form:first' ) . find ( '[type=radio]' ) . filter ( function ( i , e ) {
73
+ return $ ( e ) . attr ( 'name' ) === element . attr ( 'name' ) ;
74
+ } ) ;
75
+ element = radios . first ( ) ;
76
+ }
77
+ fBefore = element . prev ( ) ;
78
+ if ( fBefore . is ( 'span.styled,span.OBS_checkbox' ) ) {
79
+ element = fBefore ;
80
+ }
81
+ return element ;
82
+ } ;
83
+
84
+ Prompt = ( function ( ) {
85
+
86
+ function Prompt ( elem , node , options ) {
87
+ if ( $ . type ( options ) === 'string' ) {
88
+ options = {
89
+ color : options
90
+ } ;
91
+ }
92
+ this . options = new Options ( $ . isPlainObject ( options ) ? options : { } ) ;
93
+ this . elementType = elem . attr ( 'type' ) ;
94
+ this . originalElement = elem ;
95
+ this . elem = getAnchorElement ( elem ) ;
96
+ this . elem . data ( pluginName , this ) ;
97
+ this . wrapper = $ ( coreStyle . html ) ;
98
+ this . main = this . wrapper . find ( "." + className + "Main" ) ;
99
+ this . content = this . main . find ( "." + className + "Content" ) ;
100
+ this . elem . before ( this . wrapper ) ;
101
+ this . main . css ( this . calculateCSS ( ) ) ;
102
+ this . run ( node ) ;
103
+ }
104
+
105
+ Prompt . prototype . buildArrow = function ( ) {
106
+ var alt , d , dir , showArrow , size ;
107
+ dir = this . options . arrowPosition ;
108
+ size = this . options . arrowSize ;
109
+ alt = arrowDirs [ dir ] ;
110
+ this . arrow = create ( "div" ) ;
111
+ this . arrow . addClass ( className + 'Arrow' ) . css ( {
112
+ 'margin-top' : 2 + ( document . documentMode === 5 ? size * - 4 : 0 ) ,
113
+ 'position' : 'relative' ,
114
+ 'z-index' : '2' ,
115
+ 'margin-left' : 10 ,
116
+ 'width' : 0 ,
117
+ 'height' : 0
118
+ } ) . css ( 'border-' + alt , size + 'px solid ' + this . getColor ( ) ) ;
119
+ for ( d in arrowDirs ) {
120
+ if ( d !== dir && d !== alt ) {
121
+ this . arrow . css ( 'border-' + d , size + 'px solid transparent' ) ;
122
+ }
123
+ }
124
+ showArrow = this . options . arrowShow && this . elementType !== 'radio' ;
125
+ if ( showArrow ) {
126
+ return this . arrow . show ( ) ;
127
+ } else {
128
+ return this . arrow . hide ( ) ;
129
+ }
130
+ } ;
131
+
132
+ Prompt . prototype . showMain = function ( show ) {
133
+ var hidden ;
134
+ hidden = this . main . parent ( ) . parents ( ':hidden' ) . length > 0 ;
135
+ if ( hidden && show ) {
136
+ this . main . show ( ) ;
137
+ }
138
+ if ( hidden && ! show ) {
139
+ this . main . hide ( ) ;
140
+ }
141
+ if ( ! hidden && show ) {
142
+ this . main [ this . options . showAnimation ] ( this . options . showDuration ) ;
143
+ }
144
+ if ( ! hidden && ! show ) {
145
+ return this . main [ this . options . hideAnimation ] ( this . options . hideDuration ) ;
146
+ }
147
+ } ;
148
+
149
+ Prompt . prototype . calculateCSS = function ( ) {
150
+ var elementPosition , height , left , mainPosition ;
151
+ elementPosition = this . elem . position ( ) ;
152
+ mainPosition = this . main . parent ( ) . position ( ) ;
153
+ height = this . elem . outerHeight ( ) ;
154
+ left = elementPosition . left - mainPosition . left ;
155
+ if ( ! navigator . userAgent . match ( / M S I E / ) ) {
156
+ height += elementPosition . top - mainPosition . top ;
157
+ }
158
+ return {
159
+ top : height + this . options . gap ,
160
+ left : left
161
+ } ;
162
+ } ;
163
+
164
+ Prompt . prototype . getColor = function ( ) {
165
+ return this . options . colors [ this . options . color ] || this . options . color ;
166
+ } ;
167
+
168
+ Prompt . prototype . run = function ( node , options ) {
169
+ var t ;
170
+ if ( $ . isPlainObject ( options ) ) {
171
+ $ . extend ( this . options , options ) ;
172
+ } else if ( $ . type ( options ) === 'string' ) {
173
+ this . options . color = options ;
174
+ }
175
+ if ( this . main && ! node ) {
176
+ this . showMain ( false ) ;
177
+ return ;
178
+ } else if ( ! this . main && ! node ) {
179
+ return ;
180
+ }
181
+ if ( $ . type ( node ) === 'string' ) {
182
+ this . content . html ( node . replace ( '\n' , '<br/>' ) ) ;
183
+ } else {
184
+ this . content . empty ( ) . append ( node ) ;
185
+ }
186
+ this . content . css ( {
187
+ 'color' : this . getColor ( ) ,
188
+ 'border-color' : this . getColor ( )
189
+ } ) ;
190
+ if ( this . arrow ) {
191
+ this . arrow . remove ( ) ;
192
+ }
193
+ this . buildArrow ( ) ;
194
+ this . content . before ( this . arrow ) ;
195
+ this . showMain ( true ) ;
196
+ if ( this . options . autoHidePrompt ) {
197
+ clearTimeout ( this . elem . data ( 'mainTimer' ) ) ;
198
+ t = setTimeout ( function ( ) {
199
+ return this . showMain ( false ) ;
200
+ } , this . options . autoHideDelay ) ;
201
+ return this . elem . data ( 'mainTimer' , t ) ;
202
+ }
203
+ } ;
204
+
205
+ return Prompt ;
206
+
207
+ } ) ( ) ;
208
+
209
+ $ ( function ( ) {
210
+ $ ( "head" ) . append ( create ( "style" ) . html ( coreStyle . css ) ) ;
211
+ return $ ( document ) . on ( 'click' , "." + className , function ( ) {
212
+ var inst ;
213
+ inst = getAnchorElement ( $ ( this ) ) . data ( pluginName ) ;
214
+ if ( inst != null ) {
215
+ return inst . showMain ( false ) ;
216
+ }
217
+ } ) ;
218
+ } ) ;
219
+
220
+ $ [ pluginName ] = function ( elem , node , options ) {
221
+ return $ ( elem ) [ pluginName ] ( node , options ) ;
222
+ } ;
223
+
224
+ $ [ pluginName ] . options = function ( options ) {
225
+ return $ . extend ( pluginOptions , options ) ;
226
+ } ;
227
+
228
+ $ [ pluginName ] . addStyle = function ( s ) {
229
+ return $ . extend ( true , userStyles , s ) ;
230
+ } ;
231
+
232
+ $ . fn [ pluginName ] = function ( node , options ) {
233
+ return $ ( this ) . each ( function ( ) {
234
+ var inst ;
235
+ inst = getAnchorElement ( $ ( this ) ) . data ( pluginName ) ;
236
+ if ( inst != null ) {
237
+ return inst . run ( node , options ) ;
238
+ } else {
239
+ return new Prompt ( $ ( this ) , node , options ) ;
240
+ }
241
+ } ) ;
242
+ } ;
243
+
244
+ } ( window , document ) ) ;
0 commit comments