source: main/trunk/model-sites-dev/cambridge-museum/collect/waikato-independent/pre-import/EditableDatabaseTable/WebContent/scripts/jquery.jeditable-1.7.1.js@ 34511

Last change on this file since 34511 was 34511, checked in by davidb, 4 years ago

Evolution of code away from Company model to one that uses RecordHashmap to represent the rows coming out of the JDBC database

File size: 22.4 KB
Line 
1// Sourced from:
2// https://github.com/fossology/fossology
3
4/*
5 * Jeditable - jQuery in place edit plugin
6 *
7 * Copyright (c) 2006-2009 Mika Tuupola, Dylan Verheul
8 * Copyright (c) 2015 Siemens AG
9 *
10 * Licensed under the MIT license:
11 * http://www.opensource.org/licenses/mit-license.php
12 *
13 * Project home:
14 * http://www.appelsiini.net/projects/jeditable
15 *
16 * Based on editable by Dylan Verheul <dylan_at_dyve.net>:
17 * http://www.dyve.net/jquery/?editable
18 *
19 */
20
21/**
22 * Version 1.7.1
23 *
24 * ** means there is basic unit tests for this parameter.
25 *
26 * @name Jeditable
27 * @type jQuery
28 * @param String target (POST) URL or function to send edited content to **
29 * @param Hash options additional options
30 * @param String options[method] method to use to send edited content (POST or PUT) **
31 * @param Function options[callback] Function to run after submitting edited content **
32 * @param String options[name] POST parameter name of edited content
33 * @param String options[id] POST parameter name of edited div id
34 * @param Hash options[submitdata] Extra parameters to send when submitting edited content.
35 * @param String options[type] text, textarea or select (or any 3rd party input type) **
36 * @param Integer options[rows] number of rows if using textarea **
37 * @param Integer options[cols] number of columns if using textarea **
38 * @param Mixed options[height] 'auto', 'none' or height in pixels **
39 * @param Mixed options[width] 'auto', 'none' or width in pixels **
40 * @param String options[loadurl] URL to fetch input content before editing **
41 * @param String options[loadtype] Request type for load url. Should be GET or POST.
42 * @param String options[loadtext] Text to display while loading external content.
43 * @param Mixed options[loaddata] Extra parameters to pass when fetching content before editing.
44 * @param Mixed options[data] Or content given as paramameter. String or function.**
45 * @param String options[indicator] indicator html to show when saving
46 * @param String options[tooltip] optional tooltip text via title attribute **
47 * @param String options[event] jQuery event such as 'click' of 'dblclick' **
48 * @param String options[submit] submit button value, empty means no button **
49 * @param String options[cancel] cancel button value, empty means no button **
50 * @param String options[cssclass] CSS class to apply to input form. 'inherit' to copy from parent. **
51 * @param String options[style] Style to apply to input form 'inherit' to copy from parent. **
52 * @param String options[select] true or false, when true text is highlighted ??
53 * @param String options[placeholder] Placeholder text or html to insert when element is empty. **
54 * @param String options[onblur] 'cancel', 'submit', 'ignore' or function ??
55 *
56 * @param Function options[onsubmit] function(settings, original) { ... } called before submit
57 * @param Function options[onreset] function(settings, original) { ... } called before reset
58 * @param Function options[onerror] function(settings, original, xhr) { ... } called on error
59 *
60 * @param Hash options[ajaxoptions] jQuery Ajax options. See docs.jquery.com.
61 *
62 */
63
64(function($) {
65
66 $.fn.editable = function(target, options) {
67
68 if ('disable' == target) {
69 $(this).data('disabled.editable', true);
70 return;
71 }
72 if ('enable' == target) {
73 $(this).data('disabled.editable', false);
74 return;
75 }
76 if ('destroy' == target) {
77 $(this)
78 .unbind($(this).data('event.editable'))
79 .removeData('disabled.editable')
80 .removeData('event.editable');
81 return;
82 }
83
84 var settings = $.extend({}, $.fn.editable.defaults, {target:target}, options);
85
86 /* setup some functions */
87 var plugin = $.editable.types[settings.type].plugin || function() { };
88 var submit = $.editable.types[settings.type].submit || function() { };
89 var buttons = $.editable.types[settings.type].buttons
90 || $.editable.types['defaults'].buttons;
91 var content = $.editable.types[settings.type].content
92 || $.editable.types['defaults'].content;
93 var element = $.editable.types[settings.type].element
94 || $.editable.types['defaults'].element;
95 var reset = $.editable.types[settings.type].reset
96 || $.editable.types['defaults'].reset;
97 var callback = settings.callback || function() { };
98 var onedit = settings.onedit || function() { };
99 var onsubmit = settings.onsubmit || function() { };
100 var onreset = settings.onreset || function() { };
101 var onerror = settings.onerror || reset;
102
103 /* show tooltip */
104 if (settings.tooltip) {
105 $(this).attr('title', settings.tooltip);
106 }
107
108 settings.autowidth = 'auto' == settings.width;
109 settings.autoheight = 'auto' == settings.height;
110
111 return this.each(function() {
112
113 /* save this to self because this changes when scope changes */
114 var self = this;
115
116 /* inlined block elements lose their width and height after first edit */
117 /* save them for later use as workaround */
118 var savedwidth = $(self).width();
119 var savedheight = $(self).height();
120
121 /* save so it can be later used by $.editable('destroy') */
122 $(this).data('event.editable', settings.event);
123
124 /* if element is empty add something clickable (if requested) */
125 if (!$.trim($(this).html())) {
126 $(this).html(settings.placeholder);
127 }
128
129 $(this).bind(settings.event, function(e) {
130
131 /* abort if disabled for this element */
132 if (true === $(this).data('disabled.editable')) {
133 return;
134 }
135
136 /* prevent throwing an exeption if edit field is clicked again */
137 if (self.editing) {
138 return;
139 }
140
141 /* abort if onedit hook returns false */
142 if (false === onedit.apply(this, [settings, self])) {
143 return;
144 }
145
146 /* prevent default action and bubbling */
147 e.preventDefault();
148 e.stopPropagation();
149
150 /* remove tooltip */
151 if (settings.tooltip) {
152 $(self).removeAttr('title');
153 }
154
155 /* figure out how wide and tall we are, saved width and height */
156 /* are workaround for http://dev.jquery.com/ticket/2190 */
157 if (0 == $(self).width()) {
158 //$(self).css('visibility', 'hidden');
159 settings.width = savedwidth;
160 settings.height = savedheight;
161 } else {
162 if (settings.width != 'none') {
163 settings.width =
164 settings.autowidth ? $(self).width() : settings.width;
165 }
166 if (settings.height != 'none') {
167 settings.height =
168 settings.autoheight ? $(self).height() : settings.height;
169 }
170 }
171 //$(this).css('visibility', '');
172
173 /* remove placeholder text, replace is here because of IE */
174 if ($(this).html().toLowerCase().replace(/(;|")/g, '') ==
175 settings.placeholder.toLowerCase().replace(/(;|")/g, '')) {
176 $(this).html('');
177 }
178
179 self.editing = true;
180 self.revert = $(self).html();
181 self.origin = $(self).text();
182 $(self).html('');
183
184 /* create the form object */
185 var form = $('<form />');
186
187 /* apply css or style or both */
188 if (settings.cssclass) {
189 if ('inherit' == settings.cssclass) {
190 form.attr('class', $(self).attr('class'));
191 } else {
192 form.attr('class', settings.cssclass);
193 }
194 }
195
196 if (settings.style) {
197 if ('inherit' == settings.style) {
198 form.attr('style', $(self).attr('style'));
199 /* IE needs the second line or display wont be inherited */
200 form.css('display', $(self).css('display'));
201 } else {
202 form.attr('style', settings.style);
203 }
204 }
205
206 /* add main input element to form and store it in input */
207 var input = element.apply(form, [settings, self]);
208
209 /* set input content via POST, GET, given data or existing value */
210 var input_content;
211
212 if (settings.loadurl) {
213 var t = setTimeout(function() {
214 input.disabled = true;
215 content.apply(form, [settings.loadtext, settings, self]);
216 }, 100);
217
218 var loaddata = {};
219 loaddata[settings.id] = self.id;
220 if ($.isFunction(settings.loaddata)) {
221 $.extend(loaddata, settings.loaddata.apply(self, [self.revert, settings]));
222 } else {
223 $.extend(loaddata, settings.loaddata);
224 }
225 $.ajax({
226 type : settings.loadtype,
227 url : settings.loadurl,
228 data : loaddata,
229 async : false,
230 success: function(result) {
231 window.clearTimeout(t);
232 input_content = result;
233 input.disabled = false;
234 }
235 });
236 } else if (settings.data) {
237 input_content = settings.data;
238 if ($.isFunction(settings.data)) {
239 input_content = settings.data.apply(self, [self.revert, settings]);
240 }
241 } else {
242 input_content = self.origin;
243 }
244 content.apply(form, [input_content, settings, self]);
245
246 input.attr('name', settings.name);
247
248 /* add buttons to the form */
249 buttons.apply(form, [settings, self]);
250
251 /* add created form to self */
252 $(self).append(form);
253
254 /* attach 3rd party plugin if requested */
255 plugin.apply(form, [settings, self]);
256
257 /* focus to first visible form element */
258 $(':input:visible:enabled:first', form).focus();
259
260 /* highlight input contents when requested */
261 if (settings.select) {
262 input.select();
263 }
264
265 /* discard changes if pressing esc */
266 input.keydown(function(e) {
267 if (e.keyCode == 27) {
268 e.preventDefault();
269 //self.reset();
270 reset.apply(form, [settings, self]);
271 }
272 });
273
274 /* discard, submit or nothing with changes when clicking outside */
275 /* do nothing is usable when navigating with tab */
276 var t;
277 if ('cancel' == settings.onblur) {
278 input.blur(function(e) {
279 /* prevent canceling if submit was clicked */
280 t = setTimeout(function() {
281 reset.apply(form, [settings, self]);
282 }, 500);
283 });
284 } else if ('submit' == settings.onblur) {
285 input.blur(function(e) {
286 /* prevent double submit if submit was clicked */
287 t = setTimeout(function() {
288 form.submit();
289 }, 200);
290 });
291 } else if ($.isFunction(settings.onblur)) {
292 input.blur(function(e) {
293 settings.onblur.apply(self, [input.val(), settings]);
294 });
295 } else {
296 input.blur(function(e) {
297 /* TODO: maybe something here */
298 });
299 }
300
301 form.submit(function(e) {
302
303 if (t) {
304 clearTimeout(t);
305 }
306
307 /* do no submit */
308 e.preventDefault();
309
310 /* call before submit hook. */
311 /* if it returns false abort submitting */
312 if (false !== onsubmit.apply(form, [settings, self])) {
313 /* custom inputs call before submit hook. */
314 /* if it returns false abort submitting */
315 if (false !== submit.apply(form, [settings, self])) {
316
317 /* check if given target is function */
318 if ($.isFunction(settings.target)) {
319 var str = settings.target.apply(self, [input.val(), settings]);
320 $(self).html(str);
321 self.editing = false;
322 callback.apply(self, [self.innerHTML, settings]);
323 /* TODO: this is not dry */
324 if (!$.trim($(self).html())) {
325 $(self).html(settings.placeholder);
326 }
327 } else {
328 /* add edited content and id of edited element to POST */
329 var submitdata = {};
330 submitdata[settings.name] = input.val();
331 submitdata[settings.id] = self.id;
332 /* add extra data to be POST:ed */
333 if ($.isFunction(settings.submitdata)) {
334 $.extend(submitdata, settings.submitdata.apply(self, [self.revert, settings]));
335 } else {
336 $.extend(submitdata, settings.submitdata);
337 }
338
339 /* quick and dirty PUT support */
340 if ('PUT' == settings.method) {
341 submitdata['_method'] = 'put';
342 }
343
344 /* show the saving indicator */
345 $(self).html(settings.indicator);
346
347 /* defaults for ajaxoptions */
348 var ajaxoptions = {
349 type : 'POST',
350 data : submitdata,
351 dataType: 'html',
352 url : settings.target,
353 success : function(result, status) {
354 if (ajaxoptions.dataType == 'html') {
355 $(self).html(result);
356 }
357 self.editing = false;
358 callback.apply(self, [result, settings]);
359 if (!$.trim($(self).html())) {
360 $(self).html(settings.placeholder);
361 }
362 },
363 error : function(xhr, status, error) {
364 onerror.apply(form, [settings, self, xhr]);
365 }
366 };
367
368 /* override with what is given in settings.ajaxoptions */
369 $.extend(ajaxoptions, settings.ajaxoptions);
370 $.ajax(ajaxoptions);
371
372 }
373 }
374 }
375
376 /* show tooltip again */
377 $(self).attr('title', settings.tooltip);
378
379 return false;
380 });
381 });
382
383 /* privileged methods */
384 this.reset = function(form) {
385 /* prevent calling reset twice when blurring */
386 if (this.editing) {
387 /* before reset hook, if it returns false abort reseting */
388 if (false !== onreset.apply(form, [settings, self])) {
389 $(self).html(self.revert);
390 self.editing = false;
391 if (!$.trim($(self).html())) {
392 $(self).html(settings.placeholder);
393 }
394 /* show tooltip again */
395 if (settings.tooltip) {
396 $(self).attr('title', settings.tooltip);
397 }
398 }
399 }
400 };
401 });
402
403 };
404
405
406 $.editable = {
407 types: {
408 defaults: {
409 element : function(settings, original) {
410 var input = $('<input type="hidden"></input>');
411 $(this).append(input);
412 return(input);
413 },
414 content : function(string, settings, original) {
415 $(':input:first', this).val(string);
416 },
417 reset : function(settings, original) {
418 original.reset(this);
419 },
420 buttons : function(settings, original) {
421 var form = this;
422 if (settings.submit) {
423 /* if given html string use that */
424 if (settings.submit.match(/>$/)) {
425 var submit = $(settings.submit).click(function() {
426 if (submit.attr("type") != "submit") {
427 form.submit();
428 }
429 });
430 /* otherwise use button with given string as text */
431 } else {
432 var submit = $('<button type="submit" />');
433 submit.html(settings.submit);
434 }
435 $(this).append(submit);
436 }
437 if (settings.cancel) {
438 /* if given html string use that */
439 if (settings.cancel.match(/>$/)) {
440 var cancel = $(settings.cancel);
441 /* otherwise use button with given string as text */
442 } else {
443 var cancel = $('<button type="cancel" />');
444 cancel.html(settings.cancel);
445 }
446 $(this).append(cancel);
447
448 $(cancel).click(function(event) {
449 //original.reset();
450 if ($.isFunction($.editable.types[settings.type].reset)) {
451 var reset = $.editable.types[settings.type].reset;
452 } else {
453 var reset = $.editable.types['defaults'].reset;
454 }
455 reset.apply(form, [settings, original]);
456 return false;
457 });
458 }
459 }
460 },
461 text: {
462 element : function(settings, original) {
463 var input = $('<input />');
464 if (settings.width != 'none') { input.width(settings.width); }
465 if (settings.height != 'none') { input.height(settings.height); }
466 /* https://bugzilla.mozilla.org/show_bug.cgi?id=236791 */
467 //input[0].setAttribute('autocomplete','off');
468 input.attr('autocomplete','off');
469 $(this).append(input);
470 return(input);
471 }
472 },
473 textarea: {
474 element : function(settings, original) {
475 var textarea = $('<textarea />');
476 if (settings.rows) {
477 textarea.attr('rows', settings.rows);
478 } else if (settings.height != "none") {
479 textarea.height(settings.height);
480 }
481 if (settings.cols) {
482 textarea.attr('cols', settings.cols);
483 } else if (settings.width != "none") {
484 textarea.width(settings.width);
485 }
486 $(this).append(textarea);
487 return(textarea);
488 }
489 },
490 select: {
491 element : function(settings, original) {
492 var select = $('<select />');
493 $(this).append(select);
494 return(select);
495 },
496 content : function(data, settings, original) {
497 /* If it is string assume it is json. */
498 if (String == data.constructor) {
499 eval ('var json = ' + data);
500 } else {
501 /* Otherwise assume it is a hash already. */
502 var json = data;
503 }
504 for (var key in json) {
505 if (!json.hasOwnProperty(key)) {
506 continue;
507 }
508 if ('selected' == key) {
509 continue;
510 }
511 var option = $('<option />').val(key).append(json[key]);
512 $('select', this).append(option);
513 }
514 /* Loop option again to set selected. IE needed this... */
515 $('select', this).children().each(function() {
516 if ($(this).val() == json['selected'] ||
517 $(this).text() == $.trim(original.revert)) {
518 $(this).attr('selected', 'selected');
519 }
520 });
521 }
522 }
523 },
524
525 /* Add new input type */
526 addInputType: function(name, input) {
527 $.editable.types[name] = input;
528 }
529 };
530
531 // publicly accessible defaults
532 $.fn.editable.defaults = {
533 name : 'value',
534 id : 'id',
535 type : 'text',
536 width : 'auto',
537 height : 'auto',
538 event : 'click.editable',
539 onblur : 'cancel',
540 loadtype : 'GET',
541 loadtext : 'Loading...',
542 placeholder: 'Click to edit',
543 loaddata : {},
544 submitdata : {},
545 ajaxoptions: {}
546 };
547
548})(jQuery);
Note: See TracBrowser for help on using the repository browser.