-
Notifications
You must be signed in to change notification settings - Fork 54
/
jquery.inlineedit.js
343 lines (281 loc) · 10.6 KB
/
jquery.inlineedit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* jQuery inlineEdit
*
* Copyright (c) 2009 Ca-Phun Ung <caphun at yelotofu dot com>
* Licensed under the MIT (MIT-LICENSE.txt) license.
*
* http://github.com/caphun/jquery.inlineedit/
*
* Inline (in-place) editing.
*/
(function( factory ) {
if ( typeof define === 'function' && define.amd ) {
define( ['jquery'], factory );
} else {
factory( jQuery );
}
}(function( $ ) {
// cached values
var namespace = '.inlineedit',
placeholderClass = 'inlineEdit-placeholder',
events = ['click', 'mouseenter','mouseleave'].join(namespace+' ');
// define inlineEdit method
$.fn.inlineEdit = function( options ) {
this.each( function() {
$.inlineEdit.getInstance( this, options ).initValue();
});
var cbBindings = function( event ) {
bindings.apply( this, [event] );
};
if ($.fn.on) {
$(this.context).on( events, this.selector, cbBindings );
} else {
// legacy support
$(this).live( events, cbBindings );
}
function bindings( event ) {
var widget = $.inlineEdit.getInstance( this, options ),
editableElement = widget.element.find( widget.options.control ),
mutated = !!editableElement.length;
widget.element.removeClass( widget.options.hover );
if ( editableElement[0] != event.target && editableElement.has(event.target).length == 0 ) {
switch ( event.type ) {
case 'click':
widget[ mutated ? 'mutate' : 'init' ]();
break;
case 'mouseover': // jquery 1.4.x
case 'mouseout': // jquery 1.4.x
case 'mouseenter':
case 'mouseleave':
if ( !mutated ) {
widget.hoverClassChange( event );
}
break;
}
}
}
}
// plugin constructor
$.inlineEdit = function( elem, options ) {
// deep extend
this.options = $.extend( true, {}, $.inlineEdit.defaults, options );
// the original element
this.element = $( elem );
}
// plugin instance
$.inlineEdit.getInstance = function( elem, options ) {
return ( $.inlineEdit.initialised( elem ) )
? $( elem ).data( 'widget' + namespace )
: new $.inlineEdit( elem, options );
}
// check if plugin initialised
$.inlineEdit.initialised = function( elem ) {
var init = $( elem ).data( 'init' + namespace );
return init !== undefined && init !== null ? true : false;
}
// plugin defaults
$.inlineEdit.defaults = {
hover: 'ui-state-hover',
editInProgress: 'edit-in-progress',
value: '',
save: '',
cancel: '',
buttons: '<button class="save">save</button> <button class="cancel">cancel</button>',
placeholder: 'Click to edit',
control: 'input',
cancelOnBlur: false,
saveOnBlur: false,
nl2br: true,
debug: false,
mutate: ''
};
// plugin prototypes
$.inlineEdit.prototype = {
// initialisation
init: function() {
// set initialise flag
this.element.data( 'init' + namespace, true );
// initialise value
this.initValue();
// mutate
this.mutate();
// save widget data
this.element.data( 'widget' + namespace, this );
},
initValue: function() {
this.value( $.trim( this.element.data('original-content') || this.element.html() ) || this.options.value );
if ( !this.value() ) {
this.element.html( $( this.placeholderHtml() ) );
} else if ( this.options.value ) {
this.element.html( this.options.value );
}
},
mutate: function() {
var self = this;
// save a copy of self before mutation (useful for cancel)
self.prevValue( self.element.html() );
self
.element
.html( self.mutatedHtml( self.value() ) )
.addClass( self.options.editInProgress )
.find( '.save' )
.bind( 'click', function( event ) {
self.save( self.element, event );
self.change( self.element, event );
return false;
})
.end()
.find( '.cancel' )
.bind( 'click', function( event ) {
self.cancel( self.element, event );
self.change( self.element, event );
return false;
})
.end()
.find( self.options.control )
.bind( 'blur', function( event ) {
if (self.options.cancelOnBlur === true) {
self.cancel( self.element, event );
self.change( self.element, event );
} else if (self.options.saveOnBlur == true){
self.save( self.element, event );
self.change( self.element, event );
}
})
.bind( 'keyup', function( event ) {
switch ( event.keyCode ) {
case 13: // save on ENTER
if (self.options.control !== 'textarea') {
self.save( self.element, event );
self.change( self.element, event );
}
break;
case 27: // cancel on ESC
self.cancel( self.element, event );
self.change( self.element, event );
break;
}
})
.focus()
.end();
// trigger mutate event on mutation (i.e. edit-in-progress)
if (this._callback('mutate', [event, this])) {
this._debug( 'mutate:', 'Current stored value', this.value() );
}
return this;
},
value: function( newValue ) {
if ( arguments.length ) {
var value = newValue == this.placeholderHtml() ? '' : newValue;
this._debug('value:','to save', value);
this.element.data( 'value' + namespace, value && this.encodeHtml( this.nl2br(value) ) );
this._debug('value:','saved', this.element.data( 'value' + namespace ));
}
return this._decodeHtml( this.element.data( 'value' + namespace) );
},
prevValue: function( newValue ) {
if ( arguments.length ) {
var value = newValue === this.options.placeholder ? '' : newValue;
this.element.data('prev_value' + namespace, value);
}
return this.element.data( 'prev_value' + namespace );
},
mutatedHtml: function( value ) {
//console.log('mutatedHtml', value);
return this.controls[ this.options.control ].call( this, value );
},
placeholderHtml: function() {
return '<span class="'+ placeholderClass +'">'+ this.options.placeholder +'</span>';
},
buttonHtml: function( options ) {
var o = $.extend({}, {
before: ' ',
buttons: this.options.buttons,
after: ''
}, options);
return o.before + o.buttons + o.after;
},
save: function( elem, event ) {
var $control = this.element.find( this.options.control ),
hash = {
value: this.encodeHtml( $control.val() )
};
this._debug('save:',"Saving...");
// save value back to control to avoid XSS
$control.val(hash.value);
//if ( ( $.isFunction( this.options.save ) && this.options.save.call( this.element[0], event, hash, this ) ) !== false || !this.options.save ) {
if (this._callback('save', [event, hash, this])) {
this.value( hash.value );
this._debug( 'save:', 'Current stored value', this.value() );
}
},
cancel: function( elem, event ) {
var $control = this.element.find( this.options.control ),
hash = {
value: this.encodeHtml( $control.val() )
};
this._debug('cancel:', "Cancelling...");
//if ( ( $.isFunction( this.options.cancel ) && this.options.cancel.call( this.element[0], event, hash, this ) ) !== false || !this.options.cancel ) {
if (this._callback('cancel', [event, hash, this])) {
this.value( this.prevValue() ); // put back previous value
this._debug( 'cancel:', 'Current stored value', this.value() );
}
},
change: function( elem, event ) {
var self = this;
if ( this.timer ) {
window.clearTimeout( this.timer );
}
this.timer = window.setTimeout( function() {
self._debug( 'change:', 'Change', self.value() );
self.element.html( self.value() || self.placeholderHtml() );
self.element.removeClass( self.options.hover );
self.element.removeClass( self.options.editInProgress );
self._callback( 'change', [event, self] );
self._debug( 'change:', 'Change complete' );
}, 200 );
},
controls: {
textarea: function( value ) {
return '<textarea>'+ this.br2nl(value) +'</textarea>' + this.buttonHtml( { before: '<br />' } );
},
input: function( value ) {
return '<input type="text" value="'+ value.replace(/(\u0022)+/g, '') +'"/>' + this.buttonHtml();
}
},
hoverClassChange: function( event ) {
$( this.element )[ /mouseover|mouseenter/.test( event.type ) ? 'addClass':'removeClass']( this.options.hover );
},
encodeHtml: function( s ) {
var encoding = [
{key: /</g, value: '<'},
{key: />/g, value: '>'},
{key: /"/g, value: '"'}
],
value = s;
$.each(encoding, function(i,n) {
value = value.replace(n.key, n.value);
});
return value;
},
br2nl: function( val ) {
return this.options.nl2br ? val.replace( /<br\s?\/?>/g, "\n" ) : val;
},
nl2br: function( val ) {
return this.options.nl2br ? val.replace( /\n/g, "<br />" ) : val;
},
_debug: function() {
if (this.options && this.options.debug) {
return window.console && console.log.call(console, arguments);
}
},
_callback: function( fn, args ) {
return ($.isFunction( this.options[fn] ) && this.options[fn].apply( this.element[0], args ) ) !== false || !this.options[fn];
},
_decodeHtml: function( encoded ) {
var decoded = encoded.replace(/"/g,'"');
this._debug('_decodeHtml:', decoded);
return decoded;
}
};
}));