annotate DataTables-1.9.4/examples/examples_support/jquery.jeditable.js @ 6:42076db43d42 draft

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