1
+ /*
2
+ * Editly script
3
+ * http://griddly.com
4
+ * Copyright 2013-2015 Chris Hynes and Data Research Group, Inc.
5
+ * Licensed under MIT (https://github.com/programcsharp/griddly/blob/master/LICENSE)
6
+ *
7
+ * WARNING: Don't edit this file -- it'll be overwitten when you upgrade.
8
+ *
9
+ */
10
+
11
+ ! function ( $ )
12
+ {
13
+ "use strict" ; // jshint ;_;
14
+
15
+ var Editly = function ( element , options )
16
+ {
17
+ this . $element = $ ( element ) ;
18
+ this . options = options ;
19
+ this . create ( ) ;
20
+ } ;
21
+
22
+ var ARROW_LEFT = 37 , ARROW_UP = 38 , ARROW_RIGHT = 39 , ARROW_DOWN = 40 , ENTER = 13 , ESC = 27 , TAB = 9 ;
23
+
24
+ var serializeObject = function ( $elements )
25
+ {
26
+ // http://stackoverflow.com/a/1186309/8037
27
+ var data = { } ;
28
+
29
+ $ . each ( $elements . serializeArray ( ) , function ( )
30
+ {
31
+ if ( data [ this . name ] !== undefined )
32
+ {
33
+ if ( ! data [ this . name ] . push )
34
+ data [ this . name ] = [ data [ this . name ] ] ;
35
+
36
+ data [ this . name ] . push ( this . value || '' ) ;
37
+ }
38
+ else
39
+ {
40
+ data [ this . name ] = this . value || '' ;
41
+ }
42
+ } ) ;
43
+
44
+ return data ;
45
+ } ;
46
+
47
+ Editly . prototype = {
48
+ constructor : Editly ,
49
+
50
+ // create and bind
51
+ create : function ( )
52
+ {
53
+ var self = this ;
54
+ var active = null ;
55
+ //var url = this.$element.data("griddly-url");
56
+
57
+ //this.options.url = url;
58
+
59
+ this . $element . find ( "tbody td" ) . each ( function ( )
60
+ {
61
+ if ( ! this . tabIndex || this . tabIndex < 0 )
62
+ this . tabIndex = 0 ;
63
+ } ) ;
64
+
65
+ var movement = function ( element , keycode )
66
+ {
67
+ if ( keycode === ARROW_RIGHT )
68
+ return element . next ( 'td' ) ;
69
+ else if ( keycode === ARROW_LEFT )
70
+ return element . prev ( 'td' ) ;
71
+ else if ( keycode === ARROW_UP )
72
+ return element . parent ( ) . prev ( ) . children ( ) . eq ( element . index ( ) ) ;
73
+ else if ( keycode === ARROW_DOWN )
74
+ return element . parent ( ) . next ( ) . children ( ) . eq ( element . index ( ) ) ;
75
+
76
+ return [ ] ;
77
+ } ;
78
+
79
+ var showEditor = function ( select )
80
+ {
81
+ active = self . $element . find ( 'td:focus' ) ;
82
+
83
+ if ( active . length )
84
+ {
85
+ var template = self . options . editors [ active [ 0 ] . cellIndex ] ;
86
+ var editor = template . is ( ":input" ) ? template : template . find ( ":input" ) ;
87
+
88
+ var validator = editor . parents ( "form" ) . data ( 'validator' )
89
+
90
+ //validator.settings.showErrors = function (errorMap, errorList)
91
+ //{
92
+
93
+ //};
94
+
95
+ //validator.settings.errorPlacement = function (error, element)
96
+ //{
97
+ // $(template).popover(
98
+ // {
99
+ // container: "body",
100
+ // content: error.html(),
101
+ // html: true
102
+ // }).popover("show");
103
+ //};
104
+
105
+ template
106
+ . show ( )
107
+ . offset ( active . offset ( ) )
108
+ . width ( template == editor ? active . width ( ) : active . outerWidth ( ) )
109
+ . height ( template == editor ? active . height ( ) : active . outerHeight ( ) ) ;
110
+
111
+ editor . val ( active . text ( ) )
112
+ . removeClass ( 'error' )
113
+ . css ( active . css ( self . options . cloneProperties ) )
114
+ . height ( active . height ( ) )
115
+ . focus ( )
116
+ . off ( "blur" )
117
+ . on ( "blur" , function ( )
118
+ {
119
+ active . text ( editor . val ( ) ) ;
120
+ template . hide ( ) ;
121
+ template . popover ( "hide" ) ;
122
+ } )
123
+ . off ( "keydown" )
124
+ . on ( "keydown" , function ( e )
125
+ {
126
+ if ( e . which === ENTER )
127
+ {
128
+ //active.text(editor.val());
129
+ //setActiveText();
130
+
131
+ template . hide ( ) ;
132
+ active . focus ( ) ;
133
+
134
+ e . preventDefault ( ) ;
135
+ e . stopPropagation ( ) ;
136
+ }
137
+ else if ( e . which === ESC )
138
+ {
139
+ editor . val ( active . text ( ) ) ;
140
+
141
+ e . preventDefault ( ) ;
142
+ e . stopPropagation ( ) ;
143
+
144
+ template . hide ( ) ;
145
+ active . focus ( ) ;
146
+ }
147
+ else if ( e . which === TAB )
148
+ {
149
+ active . focus ( ) ;
150
+
151
+ // Have to move here because dropdown eats it somehow if not
152
+ var possibleMove = movement ( active , e . shiftKey ? ARROW_LEFT : ARROW_RIGHT ) ;
153
+
154
+ if ( possibleMove . length > 0 )
155
+ {
156
+ possibleMove . focus ( ) ;
157
+
158
+ e . preventDefault ( ) ;
159
+ e . stopPropagation ( ) ;
160
+ }
161
+ }
162
+ else if ( this . selectionEnd - this . selectionStart === this . value . length ||
163
+ ( this . tagName == "SELECT" && ( e . which == ARROW_RIGHT || e . which === ARROW_LEFT ) ) )
164
+ {
165
+ var possibleMove = movement ( active , e . which ) ;
166
+
167
+ if ( possibleMove . length > 0 )
168
+ {
169
+ possibleMove . focus ( ) ;
170
+
171
+ e . preventDefault ( ) ;
172
+ e . stopPropagation ( ) ;
173
+ }
174
+ }
175
+ } ) ;
176
+
177
+ if ( template == editor )
178
+ editor . width ( active . width ( ) ) ;
179
+
180
+ if ( editor . valid ( ) )
181
+ {
182
+ validator . settings . unhighlight ( editor ) ;
183
+
184
+ // TODO: do we need this if app has proper bs highlight method?
185
+ editor . parents ( ".form-group" ) . removeClass ( "has-error" ) ;
186
+ }
187
+ else
188
+ {
189
+ validator . settings . highlight ( editor ) ;
190
+
191
+ // TODO: do we need this if app has proper bs highlight method?
192
+ editor . parents ( ".form-group" ) . addClass ( "has-error" ) ;
193
+
194
+ window . setTimeout ( function ( )
195
+ {
196
+ $ ( template )
197
+ . popover (
198
+ {
199
+ container : "body" ,
200
+ content : function ( ) { return validator . errorList [ 0 ] . message ; }
201
+ } )
202
+ . popover ( "show" ) ;
203
+ } , 100 ) ;
204
+ }
205
+
206
+ if ( select )
207
+ {
208
+ editor . select ( ) ;
209
+ }
210
+ }
211
+ } ;
212
+
213
+ $ ( this . $element ) . on ( "click keypress dblclick" , $ . proxy ( function ( e )
214
+ {
215
+ showEditor ( true ) ;
216
+ } , this ) ) ;
217
+
218
+ $ ( this . $element ) . on ( "keydown" , $ . proxy ( function ( e )
219
+ {
220
+ var prevent = true ,
221
+ possibleMove = movement ( $ ( e . target ) , e . which ) ;
222
+
223
+ if ( possibleMove . length > 0 )
224
+ {
225
+ possibleMove . focus ( ) ;
226
+ }
227
+ else if ( e . which === ENTER )
228
+ {
229
+ showEditor ( false ) ;
230
+ }
231
+ else if ( e . which === 17 || e . which === 91 || e . which === 93 )
232
+ {
233
+ showEditor ( true ) ;
234
+ prevent = false ;
235
+ }
236
+ else
237
+ {
238
+ prevent = false ;
239
+ }
240
+
241
+ if ( prevent )
242
+ {
243
+ e . stopPropagation ( ) ;
244
+ e . preventDefault ( ) ;
245
+ }
246
+ } , this ) ) ;
247
+ } ,
248
+
249
+ // destroy and unbind
250
+ destroy : function ( )
251
+ {
252
+
253
+ }
254
+ } ;
255
+
256
+ $ . fn . editly = function ( option , parameter )
257
+ {
258
+ var value ;
259
+ var args = arguments ;
260
+
261
+ this . each ( function ( )
262
+ {
263
+ var data = $ ( this ) . data ( 'editly' ) ,
264
+ options = typeof option == 'object' && option ;
265
+
266
+ // initialize editly
267
+ if ( ! data )
268
+ {
269
+ var instanceOptions = $ . extend ( { } , $ . fn . editly . defaults , options ) ;
270
+
271
+ $ ( this ) . data ( 'editly' , ( data = new Editly ( this , instanceOptions ) ) ) ;
272
+ }
273
+
274
+ // call editly method
275
+ if ( typeof option == 'string' )
276
+ {
277
+ value = data [ option ] . apply ( data , Array . prototype . slice . call ( args , 1 ) ) ;
278
+ }
279
+ } ) ;
280
+
281
+ if ( value !== undefined )
282
+ return value ;
283
+ else
284
+ return this ;
285
+ } ;
286
+
287
+ $ . fn . editly . defaults =
288
+ {
289
+ cloneProperties : [ 'padding' , 'padding-top' , 'padding-bottom' , 'padding-left' , 'padding-right' ,
290
+ 'text-align' , 'font' , 'font-size' , 'font-family' , 'font-weight' ] //,
291
+ //'border', 'border-top', 'border-bottom', 'border-left', 'border-right']
292
+
293
+ } ;
294
+
295
+ $ ( function ( )
296
+ {
297
+ $ ( "[data-role=editly]" ) . editly ( ) ;
298
+ } ) ;
299
+ } ( window . jQuery ) ;
0 commit comments