editinplace.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Copyright (C) 2011-2014 Regis Houssin <regis.houssin@capnetworks.com>
  2. // Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. // or see http://www.gnu.org/
  17. //
  18. //
  19. // \file htdocs/core/js/editinplace.js
  20. // \brief File that include javascript functions for edit in place
  21. //
  22. $(document).ready(function() {
  23. var element = $('#jeditable_element').html();
  24. var table_element = $('#jeditable_table_element').html();
  25. var fk_element = $('#jeditable_fk_element').html();
  26. if ($('.editval_textarea').length > 0) {
  27. $('.editval_textarea').editable(urlSaveInPlace, {
  28. type : 'textarea',
  29. rows : $('#textarea_' + $('.editval_textarea').attr('id').substr(8) + '_rows').val(),
  30. cols : $('#textarea_' + $('.editval_textarea').attr('id').substr(8) + '_cols').val(),
  31. id : 'field',
  32. tooltip : tooltipInPlace,
  33. placeholder : '&nbsp;',
  34. cancel : cancelInPlace,
  35. submit : submitInPlace,
  36. indicator : indicatorInPlace,
  37. loadurl : urlLoadInPlace,
  38. loaddata : function(result, settings) {
  39. return getParameters(this, 'textarea');
  40. },
  41. submitdata : function(result, settings) {
  42. return getParameters(this, 'textarea');
  43. },
  44. callback : function(result, settings) {
  45. getResult(this, result);
  46. },
  47. onreset : function(result, settings) {
  48. getDefault(settings);
  49. }
  50. });
  51. $('.editkey_textarea').hover(
  52. function () {
  53. $('#viewval_' + $(this).attr('id')).addClass("viewval_hover");
  54. },
  55. function () {
  56. $('#viewval_' + $(this).attr('id')).removeClass("viewval_hover");
  57. }
  58. );
  59. $('.editkey_textarea').click(function() {
  60. $('#viewval_' + $(this).attr('id')).click();
  61. });
  62. $('.viewval_textarea.active').click(function() {
  63. $('#viewval_' + $(this).attr('id').substr(8)).hide();
  64. $('#editval_' + $(this).attr('id').substr(8)).show().click();
  65. });
  66. $('.editkey_textarea').click(function() {
  67. $('#viewval_' + $(this).attr('id')).hide();
  68. $('#editval_' + $(this).attr('id')).show().click();
  69. });
  70. }
  71. if (typeof ckeditorConfig != 'undefined') {
  72. $('.editval_ckeditor').editable(urlSaveInPlace, {
  73. type : 'ckeditor',
  74. id : 'field',
  75. onblur : 'ignore',
  76. tooltip : tooltipInPlace,
  77. placeholder : '&nbsp;',
  78. cancel : cancelInPlace,
  79. submit : submitInPlace,
  80. indicator : indicatorInPlace,
  81. ckeditor : {
  82. customConfig: ckeditorConfig,
  83. toolbar: $('#ckeditor_toolbar').val(),
  84. filebrowserBrowseUrl : ckeditorFilebrowserBrowseUrl,
  85. filebrowserImageBrowseUrl : ckeditorFilebrowserImageBrowseUrl,
  86. filebrowserWindowWidth : '900',
  87. filebrowserWindowHeight : '500',
  88. filebrowserImageWindowWidth : '900',
  89. filebrowserImageWindowHeight : '500'
  90. },
  91. submitdata : function(result, settings) {
  92. return getParameters(this, 'ckeditor');
  93. },
  94. callback : function(result, settings) {
  95. getResult(this, result);
  96. },
  97. onreset : function(result, settings) {
  98. getDefault(settings);
  99. }
  100. });
  101. $('.editkey_ckeditor').hover(
  102. function () {
  103. $('#viewval_' + $(this).attr('id')).addClass("viewval_hover");
  104. },
  105. function () {
  106. $('#viewval_' + $(this).attr('id')).removeClass("viewval_hover");
  107. }
  108. );
  109. $('.editkey_ckeditor').click(function() {
  110. $( '#viewval_' + $(this).attr('id') ).click();
  111. });
  112. $('.viewval_ckeditor.active').click(function() {
  113. $('#viewval_' + $(this).attr('id').substr(8)).hide();
  114. $('#editval_' + $(this).attr('id').substr(8)).show().click();
  115. });
  116. $('.editkey_ckeditor').click(function() {
  117. $('#viewval_' + $(this).attr('id')).hide();
  118. $('#editval_' + $(this).attr('id')).show().click();
  119. });
  120. }
  121. if ($('.editval_string').length > 0) {
  122. $('.editval_string').editable(urlSaveInPlace, {
  123. type : 'text',
  124. id : 'field',
  125. width : withInPlace, /* Size of string area in px ? */
  126. tooltip : tooltipInPlace,
  127. placeholder : placeholderInPlace,
  128. cancel : cancelInPlace,
  129. submit : submitInPlace,
  130. indicator : indicatorInPlace,
  131. submitdata : function(result, settings) {
  132. return getParameters(this, 'string');
  133. },
  134. callback : function(result, settings) {
  135. getResult(this, result);
  136. },
  137. onreset : function(result, settings) {
  138. getDefault(settings);
  139. }
  140. });
  141. $('.editkey_string').hover(
  142. function () {
  143. $('#viewval_' + $(this).attr('id')).addClass("viewval_hover");
  144. },
  145. function () {
  146. $('#viewval_' + $(this).attr('id')).removeClass("viewval_hover");
  147. }
  148. );
  149. $('.editkey_string').click(function() {
  150. $( '#viewval_' + $(this).attr('id') ).click();
  151. });
  152. $('.viewval_string.active').click(function() {
  153. $('#viewval_' + $(this).attr('id').substr(8)).hide();
  154. $('#editval_' + $(this).attr('id').substr(8)).show().click();
  155. });
  156. $('.editkey_string').click(function() {
  157. $('#viewval_' + $(this).attr('id')).hide();
  158. $('#editval_' + $(this).attr('id')).show().click();
  159. });
  160. }
  161. if ($('.editval_numeric').length > 0) {
  162. $('.editval_numeric').editable(urlSaveInPlace, {
  163. type : 'text',
  164. id : 'field',
  165. width : 100,
  166. tooltip : tooltipInPlace,
  167. placeholder : placeholderInPlace,
  168. cancel : cancelInPlace,
  169. submit : submitInPlace,
  170. indicator : indicatorInPlace,
  171. submitdata : function(result, settings) {
  172. return getParameters(this, 'numeric');
  173. },
  174. callback : function(result, settings) {
  175. getResult(this, result);
  176. },
  177. onreset : function(result, settings) {
  178. getDefault(settings);
  179. }
  180. });
  181. $('.editkey_numeric').hover(
  182. function () {
  183. $( '#viewval_' + $(this).attr('id') ).addClass("viewval_hover");
  184. },
  185. function () {
  186. $( '#viewval_' + $(this).attr('id') ).removeClass("viewval_hover");
  187. }
  188. );
  189. $('.editkey_numeric').click(function() {
  190. $( '#viewval_' + $(this).attr('id') ).click();
  191. });
  192. $('.viewval_numeric.active').click(function() {
  193. $('#viewval_' + $(this).attr('id').substr(8)).hide();
  194. $('#editval_' + $(this).attr('id').substr(8)).show().click();
  195. });
  196. $('.editkey_numeric').click(function() {
  197. $('#viewval_' + $(this).attr('id')).hide();
  198. $('#editval_' + $(this).attr('id')).show().click();
  199. });
  200. }
  201. if ($('.editval_datepicker').length > 0) {
  202. $('.editval_datepicker').editable(urlSaveInPlace, {
  203. type : 'datepicker',
  204. id : 'field',
  205. onblur : 'ignore',
  206. tooltip : tooltipInPlace,
  207. placeholder : '&nbsp;',
  208. cancel : cancelInPlace,
  209. submit : submitInPlace,
  210. indicator : indicatorInPlace,
  211. submitdata : function(result, settings) {
  212. return getParameters(this, 'datepicker');
  213. },
  214. callback : function(result, settings) {
  215. getResult(this, result);
  216. },
  217. onreset : function(result, settings) {
  218. getDefault(settings);
  219. }
  220. });
  221. $('.editkey_datepicker').hover(
  222. function () {
  223. $('#viewval_' + $(this).attr('id')).addClass("viewval_hover");
  224. },
  225. function () {
  226. $('#viewval_' + $(this).attr('id')).removeClass("viewval_hover");
  227. }
  228. );
  229. $('.viewval_datepicker.active').click(function() {
  230. $('#viewval_' + $(this).attr('id').substr(8)).hide();
  231. $('#editval_' + $(this).attr('id').substr(8)).show().click();
  232. });
  233. $('.editkey_datepicker').click(function() {
  234. $('#viewval_' + $(this).attr('id')).hide();
  235. $('#editval_' + $(this).attr('id')).show().click();
  236. });
  237. }
  238. if ($('.editval_select').length > 0) {
  239. $('.editval_select').editable(urlSaveInPlace, {
  240. type : 'select',
  241. id : 'field',
  242. onblur : 'ignore',
  243. cssclass : 'flat',
  244. tooltip : tooltipInPlace,
  245. placeholder : '&nbsp;',
  246. cancel : cancelInPlace,
  247. submit : submitInPlace,
  248. indicator : indicatorInPlace,
  249. loadurl : urlLoadInPlace,
  250. loaddata : function(result, settings) {
  251. return getParameters(this, 'select');
  252. },
  253. submitdata : function(result, settings) {
  254. return getParameters(this, 'select');
  255. },
  256. callback : function(result, settings) {
  257. getResult(this, result);
  258. },
  259. onreset : function(result, settings) {
  260. getDefault(settings);
  261. }
  262. });
  263. $('.editkey_select').hover(
  264. function () {
  265. $('#viewval_' + $(this).attr('id')).addClass("viewval_hover");
  266. },
  267. function () {
  268. $('#viewval_' + $(this).attr('id')).removeClass("viewval_hover");
  269. }
  270. );
  271. $('.viewval_select.active').click(function() {
  272. $('#viewval_' + $(this).attr('id').substr(8)).hide();
  273. $('#editval_' + $(this).attr('id').substr(8)).show().click();
  274. });
  275. $('.editkey_select').click(function() {
  276. $('#viewval_' + $(this).attr('id')).hide();
  277. $('#editval_' + $(this).attr('id')).show().click();
  278. });
  279. }
  280. // for test only (not stable)
  281. if ($('.editval_autocomplete').length > 0) {
  282. $('.editval_autocomplete').editable(urlSaveInPlace, {
  283. type : 'autocomplete',
  284. id : 'field',
  285. width : 300,
  286. onblur : 'ignore',
  287. tooltip : tooltipInPlace,
  288. placeholder : '&nbsp;',
  289. cancel : cancelInPlace,
  290. submit : submitInPlace,
  291. indicator : indicatorInPlace,
  292. autocomplete : {
  293. source : urlLoadInPlace,
  294. data : function(result, settings) {
  295. return getParameters(this, 'select');
  296. }
  297. },
  298. submitdata : function(result, settings) {
  299. return getParameters(this, 'select');
  300. },
  301. callback : function(result, settings) {
  302. getResult(this, result);
  303. },
  304. onreset : function(result, settings) {
  305. getDefault(settings);
  306. }
  307. });
  308. $('.editkey_autocomplete').hover(
  309. function () {
  310. $('#viewval_' + $(this).attr('id')).addClass("viewval_hover");
  311. },
  312. function () {
  313. $('#viewval_' + $(this).attr('id')).removeClass("viewval_hover");
  314. }
  315. );
  316. $('.viewval_autocomplete.active').click(function() {
  317. $('#viewval_' + $(this).attr('id').substr(8)).hide();
  318. $('#editval_' + $(this).attr('id').substr(8)).show().click();
  319. });
  320. $('.editkey_autocomplete').click(function() {
  321. $('#viewval_' + $(this).attr('id')).hide();
  322. $('#editval_' + $(this).attr('id')).show().click();
  323. });
  324. }
  325. function getParameters(obj, type) {
  326. var htmlname = $(obj).attr('id').substr(8);
  327. var element = $('#element_' + htmlname).val();
  328. var table_element = $('#table_element_' + htmlname).val();
  329. var fk_element = $('#fk_element_' + htmlname).val();
  330. var loadmethod = $('#loadmethod_' + htmlname).val();
  331. var savemethod = $('#savemethod_' + htmlname).val();
  332. var ext_element = $('#ext_element_' + htmlname).val();
  333. var timestamp = $('#timestamp').val();
  334. return {
  335. type: type,
  336. element: element,
  337. table_element: table_element,
  338. fk_element: fk_element,
  339. loadmethod: loadmethod,
  340. savemethod: savemethod,
  341. timestamp: timestamp,
  342. ext_element: ext_element
  343. };
  344. }
  345. function getResult(obj, result) {
  346. var res = $.parseJSON(result);
  347. if (res.error) {
  348. $(obj).html(obj.revert);
  349. var htmlname = $(obj).attr('id').substr(8);
  350. var errormsg = $( '#errormsg_' + htmlname ).val();
  351. if (errormsg != undefined) {
  352. $.jnotify(errormsg, "error", true);
  353. } else {
  354. $.jnotify(res.error, "error", true);
  355. }
  356. } else {
  357. var htmlname = $(obj).attr('id').substr(8);
  358. var successmsg = $( '#successmsg_' + htmlname ).val();
  359. if (successmsg != undefined) {
  360. $.jnotify(successmsg, "ok");
  361. }
  362. $(obj).html(res.value);
  363. $(obj).hide();
  364. $('#viewval_' + htmlname).html(res.view);
  365. $('#viewval_' + htmlname).show();
  366. }
  367. }
  368. function getDefault(settings) {
  369. var htmlname = $(settings).attr('id').substr(8);
  370. $('#editval_' + htmlname).hide();
  371. $('#viewval_' + htmlname).show();
  372. }
  373. });