You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

form_fields.js 12 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. //
  23. // Form Input
  24. // ======================================================================================
  25. _f.ColumnBreak = function() {
  26. this.set_input = function() { };
  27. }
  28. _f.ColumnBreak.prototype.make_body = function() {
  29. this.cell = this.frm.layout.addcell(this.df.width);
  30. $y(this.cell.wrapper, {padding: '8px'});
  31. _f.cur_col_break_width = this.df.width;
  32. var fn = this.df.fieldname?this.df.fieldname:this.df.label;
  33. // header
  34. if(this.df&&this.df.label){
  35. this.label = $a(this.cell.wrapper, 'h4', '', '', this.df.label);
  36. }
  37. }
  38. _f.ColumnBreak.prototype.refresh = function(layout) {
  39. //if(!this.cell)return; // no perm
  40. var hidden = 0;
  41. // we generate column breaks, but hide it based on perms/hidden value
  42. if((!this.perm[this.df.permlevel]) || (!this.perm[this.df.permlevel][READ]) ||
  43. this.df.hidden) {
  44. // do not display, as no permission
  45. hidden = 1;
  46. }
  47. // hidden
  48. if(this.set_hidden!=hidden) {
  49. if(hidden)
  50. this.cell.hide();
  51. else
  52. this.cell.show();
  53. this.set_hidden = hidden;
  54. }
  55. }
  56. // ======================================================================================
  57. _f.SectionBreak = function() {
  58. this.fields = [];
  59. this.set_input = function() { };
  60. this.make_row = function() {
  61. this.row = this.df.label ? this.frm.layout.addrow() : this.frm.layout.addsubrow();
  62. }
  63. }
  64. _f.SectionBreak.prototype.make_body = function() {
  65. var me = this;
  66. this.make_row();
  67. if(this.df.label) {
  68. if(!this.df.description)
  69. this.df.description = '';
  70. $(this.row.main_head).html(repl('<div class="form-section-head">\
  71. <h4 class="head">%(label)s</h4>\
  72. <div class="help small" \
  73. style="margin-top: 4px; margin-bottom: 8px;">%(description)s</div>\
  74. </div>', this.df));
  75. } else {
  76. // simple
  77. $(this.wrapper).html('<div class="form-section-head"></div>');
  78. }
  79. // collapse section
  80. this.section_collapse = function() {
  81. $(me.row.main_head).find('.head')
  82. .html('<i class="icon-chevron-right"></i> \
  83. <a href="#" onclick="return false;">Show "' + me.df.label + '"</a>');
  84. $(me.row.main_body).toggle(false);
  85. }
  86. // expand section
  87. this.section_expand = function(no_animation) {
  88. $(me.row.main_head).find('.head')
  89. .html('<h3><i class="icon-chevron-down" style="vertical-align: middle; margin-bottom: 2px"></i> '
  90. + me.df.label + '</h3>');
  91. if(no_animation)
  92. $(me.row.main_body).toggle(true);
  93. else
  94. $(me.row.main_body).slideDown();
  95. }
  96. }
  97. _f.SectionBreak.prototype.has_data = function() {
  98. // return true if
  99. // 1. any field in the section is mandatory & not set as default
  100. // 2. any field in the section has data that is not default
  101. // 3. if table, table has rows
  102. var me = this;
  103. for(var i in me.fields) {
  104. var f = me.fields[i];
  105. var v = f.get_value ? f.get_value() : null;
  106. // value that is not default
  107. defaultval = f.df['default'] || sys_defaults[f.fieldname] || user_defaults[f.fieldname];
  108. if(v && v != defaultval) {
  109. return true;
  110. }
  111. // unfilled mandatory field
  112. if(f.df.reqd && !v) {
  113. return true;
  114. }
  115. // filled table
  116. if(f.df.fieldtype=='Table') {
  117. if(f.grid.get_children().length || f.df.reqd) {
  118. return true;
  119. }
  120. }
  121. }
  122. return false;
  123. }
  124. _f.SectionBreak.prototype.refresh = function(from_form) {
  125. var hidden = 0;
  126. // we generate section breaks, but hide it based on perms/hidden value
  127. if((!this.perm[this.df.permlevel]) || (!this.perm[this.df.permlevel][READ]) || this.df.hidden) {
  128. // no display
  129. hidden = 1;
  130. }
  131. if(hidden) {
  132. if(this.row)this.row.hide();
  133. } else {
  134. if(this.row)this.row.show();
  135. }
  136. }
  137. // Image field definition
  138. // ======================================================================================
  139. _f.ImageField = function() { this.images = {}; }
  140. _f.ImageField.prototype = new Field();
  141. _f.ImageField.prototype.onrefresh = function() {
  142. $(this.label_span).toggle(false);
  143. $(this.wrapper).find("img").remove();
  144. if(this.df.options && this.frm.doc[this.df.options]) {
  145. $("<img src='files/"+this.frm.doc[this.df.options]+"' style='max-width: 70%;'>")
  146. .appendTo(this.wrapper);
  147. }
  148. }
  149. _f.ImageField.prototype.set_disp = function (val) { }
  150. _f.ImageField.prototype.set = function (val) { }
  151. // Table
  152. // ======================================================================================
  153. _f.TableField = function() { };
  154. _f.TableField.prototype = new Field();
  155. _f.TableField.prototype.with_label = 0;
  156. _f.TableField.prototype.make_body = function() {
  157. if(this.perm[this.df.permlevel] && this.perm[this.df.permlevel][READ]) {
  158. this.grid = new _f.FormGrid(this);
  159. if(this.frm)this.frm.grids[this.frm.grids.length] = this;
  160. this.grid.make_buttons();
  161. // description
  162. if(this.df.description) {
  163. this.desc_area = $a(this.parent, 'div', 'help small',
  164. {marginBottom:'9px', marginTop:'0px'}, this.df.description)
  165. }
  166. }
  167. }
  168. _f.TableField.prototype.refresh = function() {
  169. if(!this.grid)return;
  170. // hide / show grid
  171. var st = this.get_status();
  172. if(!this.df['default'])
  173. this.df['default']='';
  174. this.grid.can_add_rows = false;
  175. this.grid.can_edit = false
  176. if(st=='Write') {
  177. if(cur_frm.editable && this.perm[this.df.permlevel] && this.perm[this.df.permlevel][WRITE]) {
  178. this.grid.can_edit = true;
  179. if(this.df['default'].toLowerCase()!='no toolbar')
  180. this.grid.can_add_rows = true;
  181. }
  182. // submitted or cancelled
  183. if(cur_frm.editable && cur_frm.doc.docstatus > 0) {
  184. if(this.df.allow_on_submit && cur_frm.doc.docstatus==1) {
  185. this.grid.can_edit = true;
  186. if(this.df['default'].toLowerCase()=='no toolbar') {
  187. this.grid.can_add_rows = false;
  188. } else {
  189. this.grid.can_add_rows = true;
  190. }
  191. } else {
  192. this.grid.can_add_rows = false;
  193. this.grid.can_edit = false;
  194. }
  195. }
  196. if(this.df['default'].toLowerCase()=='no add rows') {
  197. this.grid.can_add_rows = false;
  198. }
  199. }
  200. //if(this.old_status!=st) {
  201. if(st=='Write') {
  202. // nothing
  203. this.grid.show();
  204. } else if(st=='Read') {
  205. this.grid.show();
  206. } else {
  207. this.grid.hide();
  208. }
  209. // this.old_status = st; // save this if next time
  210. //}
  211. this.grid.refresh();
  212. }
  213. _f.TableField.prototype.set = function(v) { }; // nothing
  214. _f.TableField.prototype.set_input = function(v) { }; // nothing
  215. // ==============================================================
  216. _f.CodeField = function() { };
  217. _f.CodeField.prototype = new Field();
  218. _f.CodeField.prototype.make_input = function() {
  219. var me = this;
  220. this.label_span.innerHTML = this.df.label;
  221. if(this.df.fieldtype=='Text Editor') {
  222. $(this.input_area).css({"min-height":"360px"});
  223. this.input = $a(this.input_area, 'text_area', '', {fontSize:'12px'});
  224. this.myid = wn.dom.set_unique_id(this.input);
  225. // setup tiny mce
  226. $(me.input).tinymce({
  227. // Location of TinyMCE script
  228. script_url : 'lib/js/lib/tiny_mce_3.5.7/tiny_mce.js',
  229. // General options
  230. theme : "advanced",
  231. plugins : "style,inlinepopups,table,advimage",
  232. extended_valid_elements: "div[id|dir|class|align|style]",
  233. // w/h
  234. width: '100%',
  235. height: '360px',
  236. // buttons
  237. theme_advanced_buttons1 : "bold,italic,underline,strikethrough,hr,|,justifyleft,justifycenter,justifyright,|,formatselect,fontselect,fontsizeselect,|,image",
  238. theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,code,|,forecolor,backcolor,|,tablecontrols",
  239. theme_advanced_buttons3 : "",
  240. theme_advanced_toolbar_location : "top",
  241. theme_advanced_toolbar_align : "left",
  242. theme_advanced_path: false,
  243. content_css: "lib/js/lib/tiny_mce_3.5.7/custom_content.css?q=1",
  244. oninit: function() { me.init_editor(); }
  245. });
  246. this.input.set_input = function(v) {
  247. if(me.editor) {
  248. me.editor.setContent(v);
  249. } else {
  250. $(me.input).val(v);
  251. }
  252. }
  253. this.input.onchange = function() {
  254. me.set(me.editor.getContent());
  255. me.run_trigger();
  256. }
  257. this.get_value = function() {
  258. return me.editor && me.editor.getContent(); // tinyMCE
  259. }
  260. } else {
  261. // setup ace
  262. wn.require('lib/js/lib/ace/ace.js');
  263. $(this.input_area).css('border','1px solid #aaa');
  264. this.pre = $("<pre style='position: relative; height: 400px; \
  265. width: 100%; padding: 0px; border-radius: 0px;\
  266. margin: 0px; background-color: #fff;'>").appendTo(this.input_area).get(0);
  267. this.input = {};
  268. this.myid = wn.dom.set_unique_id(this.pre);
  269. this.editor = ace.edit(this.myid);
  270. if(me.df.options=='Markdown' || me.df.options=='HTML') {
  271. wn.require('lib/js/lib/ace/mode-html.js');
  272. var HTMLMode = require("ace/mode/html").Mode;
  273. me.editor.getSession().setMode(new HTMLMode());
  274. }
  275. else if(me.df.options=='Javascript') {
  276. wn.require('lib/js/lib/ace/mode-javascript.js');
  277. var JavascriptMode = require("ace/mode/javascript").Mode;
  278. me.editor.getSession().setMode(new JavascriptMode());
  279. }
  280. else if(me.df.options=='Python') {
  281. wn.require('lib/js/lib/ace/mode-python.js');
  282. var PythonMode = require("ace/mode/python").Mode;
  283. me.editor.getSession().setMode(new PythonMode());
  284. }
  285. this.input.set_input = function(v) {
  286. // during field refresh in run trigger, set_input is called
  287. // if called during on_change, setting doesn't make sense
  288. // and causes cursor to shift back to first position
  289. if(me.changing_value) return;
  290. me.setting_value = true;
  291. me.editor.getSession().setValue(v);
  292. me.setting_value = false;
  293. }
  294. this.get_value = function() {
  295. return me.editor.getSession().getValue(); // tinyMCE
  296. }
  297. $(cur_frm.wrapper).bind('render_complete', function() {
  298. me.editor.resize();
  299. me.editor.getSession().on('change', function() {
  300. if(me.setting_value) return;
  301. var val = me.get_value();
  302. if(locals[cur_frm.doctype][cur_frm.docname][me.df.fieldname] != val) {
  303. me.set(me.get_value());
  304. me.changing_value = true;
  305. me.run_trigger();
  306. me.changing_value = false;
  307. }
  308. })
  309. });
  310. }
  311. }
  312. _f.CodeField.prototype.init_editor = function() {
  313. // attach onchange methods
  314. var me = this;
  315. this.editor = tinymce.get(this.myid);
  316. this.editor.onKeyUp.add(function(ed, e) {
  317. me.set(ed.getContent());
  318. });
  319. this.editor.onPaste.add(function(ed, e) {
  320. me.set(ed.getContent());
  321. });
  322. this.editor.onSetContent.add(function(ed, e) {
  323. me.set(ed.getContent());
  324. });
  325. // reset content
  326. var c = locals[cur_frm.doctype][cur_frm.docname][this.df.fieldname];
  327. if(cur_frm && c) {
  328. this.editor.setContent(c);
  329. }
  330. }
  331. _f.CodeField.prototype.set_disp = function(val) {
  332. $y(this.disp_area, {width:'90%'})
  333. if(this.df.fieldtype=='Text Editor') {
  334. this.disp_area.innerHTML = val;
  335. } else {
  336. this.disp_area.innerHTML = '<textarea class="code_text" readonly=1>'+val+'</textarea>';
  337. }
  338. }
  339. // ======================================================================================