editinplace.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. console.log("We are hover (entry) an editkey_textarea");
  54. $('#viewval_' + $(this).attr('id')).addClass("viewval_hover");
  55. },
  56. function () {
  57. console.log("We are no more hover (exit) an editkey_textarea");
  58. $('#viewval_' + $(this).attr('id')).removeClass("viewval_hover");
  59. }
  60. );
  61. $('.editkey_textarea').click(function() {
  62. $('#viewval_' + $(this).attr('id')).click();
  63. });
  64. $('.viewval_textarea.active').click(function() {
  65. $('#viewval_' + $(this).attr('id').substr(8)).hide();
  66. $('#editval_' + $(this).attr('id').substr(8)).show().click();
  67. });
  68. $('.editkey_textarea').click(function() {
  69. $('#viewval_' + $(this).attr('id')).hide();
  70. $('#editval_' + $(this).attr('id')).show().click();
  71. });
  72. }
  73. if (typeof ckeditorConfig != 'undefined') {
  74. $('.editval_ckeditor').editable(urlSaveInPlace, {
  75. type : 'ckeditor',
  76. id : 'field',
  77. onblur : 'ignore',
  78. tooltip : tooltipInPlace,
  79. placeholder : '&nbsp;',
  80. cancel : cancelInPlace,
  81. submit : submitInPlace,
  82. indicator : indicatorInPlace,
  83. ckeditor : {
  84. customConfig: ckeditorConfig,
  85. toolbar: $('#ckeditor_toolbar').val(),
  86. filebrowserBrowseUrl : ckeditorFilebrowserBrowseUrl,
  87. filebrowserImageBrowseUrl : ckeditorFilebrowserImageBrowseUrl,
  88. filebrowserWindowWidth : '900',
  89. filebrowserWindowHeight : '500',
  90. filebrowserImageWindowWidth : '900',
  91. filebrowserImageWindowHeight : '500'
  92. },
  93. submitdata : function(result, settings) {
  94. return getParameters(this, 'ckeditor');
  95. },
  96. callback : function(result, settings) {
  97. getResult(this, result);
  98. },
  99. onreset : function(result, settings) {
  100. getDefault(settings);
  101. }
  102. });
  103. $('.editkey_ckeditor').hover(
  104. function () {
  105. console.log("We are hover (entry) an editkey_ckeditor");
  106. $('#viewval_' + $(this).attr('id')).addClass("viewval_hover");
  107. },
  108. function () {
  109. console.log("We are no more hover (exit) an editkey_ckeditor");
  110. $('#viewval_' + $(this).attr('id')).removeClass("viewval_hover");
  111. }
  112. );
  113. $('.editkey_ckeditor').click(function() {
  114. $( '#viewval_' + $(this).attr('id') ).click();
  115. });
  116. $('.viewval_ckeditor.active').click(function() {
  117. $('#viewval_' + $(this).attr('id').substr(8)).hide();
  118. $('#editval_' + $(this).attr('id').substr(8)).show().click();
  119. });
  120. $('.editkey_ckeditor').click(function() {
  121. $('#viewval_' + $(this).attr('id')).hide();
  122. $('#editval_' + $(this).attr('id')).show().click();
  123. });
  124. }
  125. if ($('.editval_string').length > 0) {
  126. $('.editval_string').editable(urlSaveInPlace, {
  127. type : 'text',
  128. id : 'field',
  129. width : withInPlace, /* Size of string area in px ? */
  130. tooltip : tooltipInPlace,
  131. placeholder : placeholderInPlace,
  132. cancel : cancelInPlace,
  133. submit : submitInPlace,
  134. indicator : indicatorInPlace,
  135. submitdata : function(result, settings) {
  136. return getParameters(this, 'string');
  137. },
  138. callback : function(result, settings) {
  139. getResult(this, result);
  140. },
  141. onreset : function(result, settings) {
  142. getDefault(settings);
  143. }
  144. });
  145. $('.editkey_string').hover(
  146. function () {
  147. console.log("We are hover (entry) an editkey_string");
  148. $('#viewval_' + $(this).attr('id')).addClass("viewval_hover");
  149. },
  150. function () {
  151. console.log("We are no more hover an editkey_string");
  152. $('#viewval_' + $(this).attr('id')).removeClass("viewval_hover");
  153. }
  154. );
  155. $('.editkey_string').click(function() {
  156. $( '#viewval_' + $(this).attr('id') ).click();
  157. });
  158. $('.viewval_string.active').click(function() {
  159. $('#viewval_' + $(this).attr('id').substr(8)).hide();
  160. $('#editval_' + $(this).attr('id').substr(8)).show().click();
  161. });
  162. $('.editkey_string').click(function() {
  163. $('#viewval_' + $(this).attr('id')).hide();
  164. $('#editval_' + $(this).attr('id')).show().click();
  165. });
  166. }
  167. if ($('.editval_numeric').length > 0) {
  168. $('.editval_numeric').editable(urlSaveInPlace, {
  169. type : 'text',
  170. id : 'field',
  171. width : 100,
  172. tooltip : tooltipInPlace,
  173. placeholder : placeholderInPlace,
  174. cancel : cancelInPlace,
  175. submit : submitInPlace,
  176. indicator : indicatorInPlace,
  177. submitdata : function(result, settings) {
  178. return getParameters(this, 'numeric');
  179. },
  180. callback : function(result, settings) {
  181. getResult(this, result);
  182. },
  183. onreset : function(result, settings) {
  184. getDefault(settings);
  185. }
  186. });
  187. $('.editkey_numeric').hover(
  188. function () {
  189. console.log("We are hover an editkey_numeric");
  190. $( '#viewval_' + $(this).attr('id') ).addClass("viewval_hover");
  191. },
  192. function () {
  193. console.log("We are no more hover (exit) an editkey_textarea");
  194. $( '#viewval_' + $(this).attr('id') ).removeClass("viewval_hover");
  195. }
  196. );
  197. $('.editkey_numeric').click(function() {
  198. $( '#viewval_' + $(this).attr('id') ).click();
  199. });
  200. $('.viewval_numeric.active').click(function() {
  201. $('#viewval_' + $(this).attr('id').substr(8)).hide();
  202. $('#editval_' + $(this).attr('id').substr(8)).show().click();
  203. });
  204. $('.editkey_numeric').click(function() {
  205. $('#viewval_' + $(this).attr('id')).hide();
  206. $('#editval_' + $(this).attr('id')).show().click();
  207. });
  208. }
  209. if ($('.editval_datepicker').length > 0) {
  210. $('.editval_datepicker').editable(urlSaveInPlace, {
  211. type : 'datepicker',
  212. id : 'field',
  213. onblur : 'ignore',
  214. tooltip : tooltipInPlace,
  215. placeholder : '&nbsp;',
  216. cancel : cancelInPlace,
  217. submit : submitInPlace,
  218. indicator : indicatorInPlace,
  219. submitdata : function(result, settings) {
  220. return getParameters(this, 'datepicker');
  221. },
  222. callback : function(result, settings) {
  223. getResult(this, result);
  224. },
  225. onreset : function(result, settings) {
  226. getDefault(settings);
  227. }
  228. });
  229. $('.editkey_datepicker').hover(
  230. function () {
  231. console.log("We are hover (entry) editkey_datepicker");
  232. $('#viewval_' + $(this).attr('id')).addClass("viewval_hover");
  233. },
  234. function () {
  235. console.log("We are no more hover (exit) an editkey_datepicker");
  236. $('#viewval_' + $(this).attr('id')).removeClass("viewval_hover");
  237. }
  238. );
  239. $('.viewval_datepicker.active').click(function() {
  240. $('#viewval_' + $(this).attr('id').substr(8)).hide();
  241. $('#editval_' + $(this).attr('id').substr(8)).show().click();
  242. });
  243. $('.editkey_datepicker').click(function() {
  244. $('#viewval_' + $(this).attr('id')).hide();
  245. $('#editval_' + $(this).attr('id')).show().click();
  246. });
  247. }
  248. if ($('.editval_select').length > 0) {
  249. $('.editval_select').editable(urlSaveInPlace, {
  250. type : 'select',
  251. id : 'field',
  252. onblur : 'ignore',
  253. cssclass : 'flat',
  254. tooltip : tooltipInPlace,
  255. placeholder : '&nbsp;',
  256. cancel : cancelInPlace,
  257. submit : submitInPlace,
  258. indicator : indicatorInPlace,
  259. loadurl : urlLoadInPlace,
  260. loaddata : function(result, settings) {
  261. return getParameters(this, 'select');
  262. },
  263. submitdata : function(result, settings) {
  264. return getParameters(this, 'select');
  265. },
  266. callback : function(result, settings) {
  267. getResult(this, result);
  268. },
  269. onreset : function(result, settings) {
  270. getDefault(settings);
  271. }
  272. });
  273. $('.editkey_select').hover(
  274. function () {
  275. console.log("We are hover (entry) an editkey_select");
  276. $('#viewval_' + $(this).attr('id')).addClass("viewval_hover");
  277. },
  278. function () {
  279. console.log("We are no more hover (exit) an editkey_select");
  280. $('#viewval_' + $(this).attr('id')).removeClass("viewval_hover");
  281. }
  282. );
  283. $('.viewval_select.active').click(function() {
  284. $('#viewval_' + $(this).attr('id').substr(8)).hide();
  285. $('#editval_' + $(this).attr('id').substr(8)).show().click();
  286. });
  287. $('.editkey_select').click(function() {
  288. $('#viewval_' + $(this).attr('id')).hide();
  289. $('#editval_' + $(this).attr('id')).show().click();
  290. });
  291. }
  292. // for test only (not stable)
  293. if ($('.editval_autocomplete').length > 0) {
  294. $('.editval_autocomplete').editable(urlSaveInPlace, {
  295. type : 'autocomplete',
  296. id : 'field',
  297. width : 300,
  298. onblur : 'ignore',
  299. tooltip : tooltipInPlace,
  300. placeholder : '&nbsp;',
  301. cancel : cancelInPlace,
  302. submit : submitInPlace,
  303. indicator : indicatorInPlace,
  304. autocomplete : {
  305. source : urlLoadInPlace,
  306. data : function(result, settings) {
  307. return getParameters(this, 'select');
  308. }
  309. },
  310. submitdata : function(result, settings) {
  311. return getParameters(this, 'select');
  312. },
  313. callback : function(result, settings) {
  314. getResult(this, result);
  315. },
  316. onreset : function(result, settings) {
  317. getDefault(settings);
  318. }
  319. });
  320. $('.editkey_autocomplete').hover(
  321. function () {
  322. console.log("We are no more hover (exit) an editkey_autocomplete");
  323. $('#viewval_' + $(this).attr('id')).addClass("viewval_hover");
  324. },
  325. function () {
  326. $('#viewval_' + $(this).attr('id')).removeClass("viewval_hover");
  327. }
  328. );
  329. $('.viewval_autocomplete.active').click(function() {
  330. $('#viewval_' + $(this).attr('id').substr(8)).hide();
  331. $('#editval_' + $(this).attr('id').substr(8)).show().click();
  332. });
  333. $('.editkey_autocomplete').click(function() {
  334. $('#viewval_' + $(this).attr('id')).hide();
  335. $('#editval_' + $(this).attr('id')).show().click();
  336. });
  337. }
  338. function getParameters(obj, type) {
  339. var htmlname = $(obj).attr('id').substr(8);
  340. var element = $('#element_' + htmlname).val();
  341. var table_element = $('#table_element_' + htmlname).val();
  342. var fk_element = $('#fk_element_' + htmlname).val();
  343. var loadmethod = $('#loadmethod_' + htmlname).val();
  344. var savemethod = $('#savemethod_' + htmlname).val();
  345. var ext_element = $('#ext_element_' + htmlname).val();
  346. var timestamp = $('#timestamp').val();
  347. return {
  348. type: type,
  349. element: element,
  350. table_element: table_element,
  351. fk_element: fk_element,
  352. loadmethod: loadmethod,
  353. savemethod: savemethod,
  354. timestamp: timestamp,
  355. ext_element: ext_element
  356. };
  357. }
  358. function getResult(obj, result) {
  359. var res = $.parseJSON(result);
  360. if (res.error) {
  361. $(obj).html(obj.revert);
  362. var htmlname = $(obj).attr('id').substr(8);
  363. var errormsg = $( '#errormsg_' + htmlname ).val();
  364. if (errormsg != undefined) {
  365. $.jnotify(errormsg, "error", true);
  366. } else {
  367. $.jnotify(res.error, "error", true);
  368. }
  369. } else {
  370. var htmlname = $(obj).attr('id').substr(8);
  371. var successmsg = $( '#successmsg_' + htmlname ).val();
  372. if (successmsg != undefined) {
  373. $.jnotify(successmsg, "ok");
  374. }
  375. $(obj).html(res.value);
  376. $(obj).hide();
  377. $('#viewval_' + htmlname).html(res.view);
  378. $('#viewval_' + htmlname).show();
  379. }
  380. }
  381. function getDefault(settings) {
  382. var htmlname = $(settings).attr('id').substr(8);
  383. $('#editval_' + htmlname).hide();
  384. $('#viewval_' + htmlname).show();
  385. }
  386. });