jquery.jeditable.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. +-----------------------------------------------------------------------+
  3. | Copyright (c) 2006-2007 Mika Tuupola, Dylan Verheul |
  4. | All rights reserved. |
  5. | |
  6. | Redistribution and use in source and binary forms, with or without |
  7. | modification, are permitted provided that the following conditions |
  8. | are met: |
  9. | |
  10. | o Redistributions of source code must retain the above copyright |
  11. | notice, this list of conditions and the following disclaimer. |
  12. | o Redistributions in binary form must reproduce the above copyright |
  13. | notice, this list of conditions and the following disclaimer in the |
  14. | documentation and/or other materials provided with the distribution.|
  15. | |
  16. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  17. | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  18. | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
  19. | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
  20. | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
  21. | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
  22. | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
  23. | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
  24. | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
  25. | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
  26. | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
  27. | |
  28. +-----------------------------------------------------------------------+
  29. */
  30. /* $Id: jquery.jeditable.js 235 2007-09-25 08:34:09Z tuupola $ */
  31. /**
  32. * jQuery inplace editor plugin (version 1.4.2)
  33. *
  34. * Based on editable by Dylan Verheul <dylan@dyve.net>
  35. * http://www.dyve.net/jquery/?editable
  36. *
  37. * @name jEditable
  38. * @type jQuery
  39. * @param String target POST URL or function name to send edited content
  40. * @param Hash options additional options
  41. * @param String options[name] POST parameter name of edited content
  42. * @param String options[id] POST parameter name of edited div id
  43. * @param Hash options[submitdata] Extra parameters to send when submitting edited content.
  44. * @param String options[type] text, textarea or select
  45. * @param Integer options[rows] number of rows if using textarea
  46. * @param Integer options[cols] number of columns if using textarea
  47. * @param Mixed options[height] 'auto', 'none' or height in pixels
  48. * @param Mixed options[width] 'auto', 'none' or width in pixels
  49. * @param String options[loadurl] URL to fetch external content before editing
  50. * @param String options[loadtype] Request type for load url. Should be GET or POST.
  51. * @param String options[loadtext] Text to display while loading external content.
  52. * @param Hash options[loaddata] Extra parameters to pass when fetching content before editing.
  53. * @param String options[data] Or content given as paramameter.
  54. * @param String options[indicator] indicator html to show when saving
  55. * @param String options[tooltip] optional tooltip text via title attribute
  56. * @param String options[event] jQuery event such as 'click' of 'dblclick'
  57. * @param String options[onblur] 'cancel', 'submit' or 'ignore'
  58. * @param String options[submit] submit button value, empty means no button
  59. * @param String options[cancel] cancel button value, empty means no button
  60. * @param String options[cssclass] CSS class to apply to input form. 'inherit' to copy from parent.
  61. * @param String options[style] Style to apply to input form 'inherit' to copy from parent.
  62. * @param String options[select] true or false, when true text is highlighted
  63. *
  64. */
  65. jQuery.fn.editable = function(target, options, callback) {
  66. /* prevent elem has no properties error */
  67. if (this.length === 0) {
  68. return(this);
  69. }
  70. var settings = {
  71. target : target,
  72. name : 'value',
  73. id : 'id',
  74. type : 'text',
  75. width : 'auto',
  76. height : 'auto',
  77. event : 'click',
  78. onblur : 'cancel',
  79. loadtype : 'GET',
  80. loadtext : 'Loading...',
  81. loaddata : {},
  82. submitdata : {}
  83. };
  84. if(options) {
  85. jQuery.extend(settings, options);
  86. }
  87. /* setup some functions */
  88. var plugin = jQuery.editable.types[settings.type].plugin || function() { };
  89. var submit = jQuery.editable.types[settings.type].submit || function() { };
  90. var buttons = jQuery.editable.types[settings.type].buttons
  91. || jQuery.editable.types['defaults'].buttons;
  92. var content = jQuery.editable.types[settings.type].content
  93. || jQuery.editable.types['defaults'].content;
  94. var element = jQuery.editable.types[settings.type].element
  95. || jQuery.editable.types['defaults'].element;
  96. callback = callback || function() { };
  97. jQuery(this).attr('title', settings.tooltip);
  98. /* temporary fix for auto width and height */
  99. settings.autowidth = 'auto' == settings.width;
  100. settings.autoheight = 'auto' == settings.height;
  101. jQuery(this)[settings.event](function(e) {
  102. /* save this to self because this changes when scope changes */
  103. var self = this;
  104. /* prevent throwing an exeption if edit field is clicked again */
  105. if (self.editing) {
  106. return;
  107. }
  108. /* figure out how wide and tall we are */
  109. if (settings.width != 'none') {
  110. settings.width =
  111. settings.autowidth ? jQuery(self).width() : settings.width;
  112. }
  113. if (settings.height != 'none') {
  114. settings.height =
  115. settings.autoheight ? jQuery(self).height() : settings.height;
  116. }
  117. self.editing = true;
  118. self.revert = jQuery(self).html();
  119. self.innerHTML = '';
  120. /* create the form object */
  121. var f = document.createElement('form');
  122. /* apply css or style or both */
  123. if (settings.cssclass) {
  124. if ('inherit' == settings.cssclass) {
  125. jQuery(f).attr('class', jQuery(self).attr('class'));
  126. } else {
  127. jQuery(f).attr('class', settings.cssclass);
  128. }
  129. }
  130. if (settings.style) {
  131. if ('inherit' == settings.style) {
  132. jQuery(f).attr('style', jQuery(self).attr('style'));
  133. /* IE needs the second line or display wont be inherited */
  134. jQuery(f).css('display', jQuery(self).css('display'));
  135. } else {
  136. jQuery(f).attr('style', settings.style);
  137. }
  138. }
  139. /* Add main input element to form and store it in i. */
  140. var i = element.apply(f, [settings, self]);
  141. /* maintain bc with 1.1.1 and earlier versions */
  142. if (settings.getload) {
  143. settings.loadurl = settings.getload;
  144. settings.loadtype = 'GET';
  145. } else if (settings.postload) {
  146. settings.loadurl = settings.postload;
  147. settings.loadtype = 'POST';
  148. }
  149. /* set input content via POST, GET, given data or existing value */
  150. if (settings.loadurl) {
  151. var t = setTimeout(function() {
  152. i.disabled = true;
  153. content.apply(f, [settings.loadtext, settings, self]);
  154. }, 100);
  155. var loaddata = {};
  156. loaddata[settings.id] = self.id;
  157. if (jQuery.isFunction(settings.loaddata)) {
  158. jQuery.extend(loaddata, settings.loaddata.apply(self, [self.revert, settings]));
  159. } else {
  160. jQuery.extend(loaddata, settings.loaddata);
  161. }
  162. jQuery.ajax({
  163. type : settings.loadtype,
  164. url : settings.loadurl,
  165. data : loaddata,
  166. success: function(string) {
  167. window.clearTimeout(t);
  168. content.apply(f, [string, settings, self]);
  169. i.disabled = false;
  170. }
  171. });
  172. } else if (settings.data) {
  173. var str = settings.data;
  174. if (jQuery.isFunction(settings.data)) {
  175. var str = settings.data.apply(self, [self.revert, settings]);
  176. }
  177. content.apply(f, [str, settings, self]);
  178. } else {
  179. content.apply(f, [self.revert, settings, self]);
  180. }
  181. i.name = settings.name;
  182. /* add buttons to the form */
  183. buttons.apply(f, [settings, self]);
  184. /* add created form to self */
  185. self.appendChild(f);
  186. /* highlight input contents when requested */
  187. if (settings.select) {
  188. i.select();
  189. }
  190. /* attach 3rd party plugin if requested */
  191. plugin.apply(f, [settings, self]);
  192. /* focus to first visible form element */
  193. jQuery(":input:visible:enabled:first", f).focus();
  194. /* discard changes if pressing esc */
  195. jQuery(i).keydown(function(e) {
  196. if (e.keyCode == 27) {
  197. e.preventDefault();
  198. reset();
  199. }
  200. });
  201. /* discard, submit or nothing with changes when clicking outside */
  202. /* do nothing is usable when navigating with tab */
  203. var t;
  204. if ('cancel' == settings.onblur) {
  205. jQuery(i).blur(function(e) {
  206. t = setTimeout(reset, 500);
  207. });
  208. } else if ('submit' == settings.onblur) {
  209. jQuery(i).blur(function(e) {
  210. jQuery(f).submit();
  211. });
  212. } else {
  213. jQuery(i).blur(function(e) {
  214. /* TODO: maybe something here */
  215. });
  216. }
  217. jQuery(f).submit(function(e) {
  218. if (t) {
  219. clearTimeout(t);
  220. }
  221. /* do no submit */
  222. e.preventDefault();
  223. /* if this input type has a call before submit hook, call it */
  224. submit.apply(f, [settings, self]);
  225. /* check if given target is function */
  226. if (jQuery.isFunction(settings.target)) {
  227. var str = settings.target.apply(self, [jQuery(i).val(), settings]);
  228. self.innerHTML = str;
  229. self.editing = false;
  230. callback.apply(self, [self.innerHTML, settings]);
  231. } else {
  232. /* add edited content and id of edited element to POST */
  233. var submitdata = {};
  234. submitdata[i.name] = jQuery(i).val();
  235. submitdata[settings.id] = self.id;
  236. /* add extra data to be POST:ed */
  237. if (jQuery.isFunction(settings.submitdata)) {
  238. jQuery.extend(submitdata, settings.submitdata.apply(self, [self.revert, settings]));
  239. } else {
  240. jQuery.extend(submitdata, settings.submitdata);
  241. }
  242. /* show the saving indicator */
  243. jQuery(self).html(settings.indicator);
  244. jQuery.post(settings.target, submitdata, function(str) {
  245. self.innerHTML = str;
  246. self.editing = false;
  247. callback.apply(self, [self.innerHTML, settings]);
  248. });
  249. }
  250. return false;
  251. });
  252. function reset() {
  253. self.innerHTML = self.revert;
  254. self.editing = false;
  255. }
  256. });
  257. return(this);
  258. };
  259. /**
  260. *
  261. */
  262. jQuery.editable = {
  263. types: {
  264. defaults: {
  265. element : function(settings, original) {
  266. var input = jQuery('<input type="hidden">');
  267. jQuery(this).append(input);
  268. return(input);
  269. },
  270. content : function(string, settings, original) {
  271. jQuery(':input:first', this).val(string);
  272. },
  273. buttons : function(settings, original) {
  274. if (settings.submit) {
  275. var submit = jQuery('<input type="submit">');
  276. submit.val(settings.submit);
  277. jQuery(this).append(submit);
  278. }
  279. if (settings.cancel) {
  280. var cancel = jQuery('<input type="button">');
  281. cancel.val(settings.cancel);
  282. jQuery(this).append(cancel);
  283. jQuery(cancel).click(function() {
  284. jQuery(original).html(original.revert);
  285. original.editing = false;
  286. });
  287. }
  288. }
  289. },
  290. text: {
  291. element : function(settings, original) {
  292. var input = jQuery('<input>');
  293. if (settings.width != 'none') { input.width(settings.width); }
  294. if (settings.height != 'none') { input.height(settings.height); }
  295. /* https://bugzilla.mozilla.org/show_bug.cgi?id=236791 */
  296. //input[0].setAttribute('autocomplete','off');
  297. input.attr('autocomplete','off');
  298. jQuery(this).append(input);
  299. return(input);
  300. }
  301. },
  302. textarea: {
  303. element : function(settings, original) {
  304. var textarea = jQuery('<textarea>');
  305. if (settings.rows) {
  306. textarea.attr('rows', settings.rows);
  307. } else {
  308. textarea.height(settings.height);
  309. }
  310. if (settings.cols) {
  311. textarea.attr('cols', settings.cols);
  312. } else {
  313. textarea.width(settings.width);
  314. }
  315. jQuery(this).append(textarea);
  316. return(textarea);
  317. }
  318. },
  319. select: {
  320. element : function(settings, original) {
  321. var select = jQuery('<select>');
  322. jQuery(this).append(select);
  323. return(select);
  324. },
  325. content : function(string, settings, original) {
  326. /* IE borks if we do not store select in separate variable. */
  327. var select = jQuery('select', this);
  328. if (String == string.constructor) {
  329. eval ("var json = " + string);
  330. for (var key in json) {
  331. if ('selected' == key) {
  332. continue;
  333. }
  334. var option = $('<option>').val(key).append(json[key]);
  335. select.append(option);
  336. }
  337. /* TODO: leave only this to content so IE works too! */
  338. /*
  339. select.children().each(function() {
  340. if (jQuery(this).val() == json['selected']) {
  341. jQuery(this).attr('selected', 'selected');
  342. };
  343. });
  344. */
  345. //setTimeout(function() { jQuery.editable.types.select.iefix(select, json['selected']) }, 1000);
  346. }
  347. jQuery.editable.types.select.iefix(select, json['selected']);
  348. },
  349. iefix : function(select, which) {
  350. console.log(this);
  351. console.log(select);
  352. select.children().each(function() {
  353. if (jQuery(this).val() == which) {
  354. jQuery(this).attr('selected', 'selected');
  355. };
  356. });
  357. }
  358. }
  359. },
  360. /* Add new input type */
  361. addInputType: function(name, input) {
  362. jQuery.editable.types[name] = input;
  363. }
  364. };