您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

426 行
13 KiB

  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.wrapper = $("<div>").appendTo(this.parent).get(0);
  159. this.grid = new _f.FormGrid(this);
  160. if(this.frm)this.frm.grids[this.frm.grids.length] = this;
  161. this.grid.make_buttons();
  162. // description
  163. if(this.df.description) {
  164. this.desc_area = $a(this.wrapper, 'div', 'help small',
  165. {marginBottom:'9px', marginTop:'0px'}, this.df.description)
  166. }
  167. }
  168. }
  169. _f.TableField.prototype.refresh = function() {
  170. if(!this.grid)return;
  171. // hide / show grid
  172. var st = this.get_status();
  173. if(!this.df['default'])
  174. this.df['default']='';
  175. this.grid.can_add_rows = false;
  176. this.grid.can_edit = false
  177. if(st=='Write') {
  178. if(cur_frm.editable && this.perm[this.df.permlevel] && this.perm[this.df.permlevel][WRITE]) {
  179. this.grid.can_edit = true;
  180. if(this.df['default'].toLowerCase()!='no toolbar')
  181. this.grid.can_add_rows = true;
  182. }
  183. // submitted or cancelled
  184. if(cur_frm.editable && cur_frm.doc.docstatus > 0) {
  185. if(this.df.allow_on_submit && cur_frm.doc.docstatus==1) {
  186. this.grid.can_edit = true;
  187. if(this.df['default'].toLowerCase()=='no toolbar') {
  188. this.grid.can_add_rows = false;
  189. } else {
  190. this.grid.can_add_rows = true;
  191. }
  192. } else {
  193. this.grid.can_add_rows = false;
  194. this.grid.can_edit = false;
  195. }
  196. }
  197. if(this.df['default'].toLowerCase()=='no add rows') {
  198. this.grid.can_add_rows = false;
  199. }
  200. }
  201. if(st=='Write' || st=="Read") {
  202. $(this.wrapper).toggle(true);
  203. this.grid.show();
  204. } else {
  205. $(this.wrapper).toggle(false);
  206. this.grid.hide();
  207. }
  208. this.grid.refresh();
  209. }
  210. _f.TableField.prototype.set = function(v) { }; // nothing
  211. _f.TableField.prototype.set_input = function(v) { }; // nothing
  212. // ==============================================================
  213. _f.CodeField = function() { };
  214. _f.CodeField.prototype = new Field();
  215. _f.CodeField.prototype.make_input = function() {
  216. var me = this;
  217. this.label_span.innerHTML = this.df.label;
  218. if(this.df.fieldtype=='Text Editor') {
  219. $(this.input_area).css({"min-height":"360px"});
  220. this.input = $a(this.input_area, 'text_area', '', {fontSize:'12px'});
  221. this.myid = wn.dom.set_unique_id(this.input);
  222. // setup tiny mce
  223. $(me.input).tinymce({
  224. // Location of TinyMCE script
  225. script_url : 'lib/js/lib/tiny_mce_3.5.7/tiny_mce.js',
  226. // General options
  227. theme : "advanced",
  228. plugins : "style,inlinepopups,table,advimage",
  229. extended_valid_elements: "div[id|dir|class|align|style]",
  230. // w/h
  231. width: '100%',
  232. height: '360px',
  233. // buttons
  234. theme_advanced_buttons1 : "bold,italic,underline,hr,|,justifyleft,justifycenter,|,formatselect,fontsizeselect,|,bullist,numlist,|,outdent,indent,|,link,unlink,|,forecolor,backcolor,|,code",
  235. theme_advanced_buttons2 : "",
  236. theme_advanced_buttons3 : "",
  237. theme_advanced_toolbar_location : "top",
  238. theme_advanced_toolbar_align : "left",
  239. theme_advanced_statusbar_location: "none",
  240. theme_advanced_path: false,
  241. valid_elements : "@[id|class|style|title|dir<ltr?rtl|lang|xml::lang|onclick|ondblclick|"
  242. + "onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|"
  243. + "onkeydown|onkeyup],a[rel|rev|charset|hreflang|tabindex|accesskey|type|"
  244. + "name|href|target|title|class|onfocus|onblur],strong/b,em/i,strike,u,"
  245. + "#p,-ol[type|compact],-ul[type|compact],-li,br,img[longdesc|usemap|"
  246. + "src|border|alt=|title|hspace|vspace|width|height|align],-sub,-sup,"
  247. + "-blockquote,-table[border=0|cellspacing|cellpadding|width|frame|rules|"
  248. + "height|align|summary|bgcolor|background|bordercolor],-tr[rowspan|width|"
  249. + "height|align|valign|bgcolor|background|bordercolor],tbody,thead,tfoot,"
  250. + "#td[colspan|rowspan|width|height|align|valign|bgcolor|background|bordercolor"
  251. + "|scope],#th[colspan|rowspan|width|height|align|valign|scope],caption,-div,"
  252. + "-span,-code,-pre,address,-h1,-h2,-h3,-h4,-h5,-h6,hr[size|noshade],-font[face"
  253. + "|size|color],dd,dl,dt,cite,abbr,acronym,del[datetime|cite],ins[datetime|cite],"
  254. + "object[classid|width|height|codebase|*],param[name|value|_value],embed[type|width"
  255. + "|height|src|*],script[src|type],map[name],area[shape|coords|href|alt|target],bdo,"
  256. + "button,col[align|char|charoff|span|valign|width],colgroup[align|char|charoff|span|"
  257. + "valign|width],dfn,fieldset,form[action|accept|accept-charset|enctype|method],"
  258. + "input[accept|alt|checked|disabled|maxlength|name|readonly|size|src|type|value|placeholder],"
  259. + "kbd,label[for],legend,noscript,optgroup[label|disabled],option[disabled|label|selected|value],"
  260. + "q[cite],samp,select[disabled|multiple|name|size],small,"
  261. + "textarea[cols|rows|disabled|name|readonly],tt,var,big",
  262. content_css: "lib/js/lib/tiny_mce_3.5.7/custom_content.css?q=1",
  263. oninit: function() { me.init_editor(); }
  264. });
  265. this.input.set_input = function(v) {
  266. if(me.editor) {
  267. me.editor.setContent(v);
  268. } else {
  269. $(me.input).val(v);
  270. }
  271. }
  272. this.input.onchange = function() {
  273. me.set(me.editor.getContent());
  274. me.run_trigger();
  275. }
  276. this.get_value = function() {
  277. return me.editor && me.editor.getContent(); // tinyMCE
  278. }
  279. } else {
  280. // setup ace
  281. wn.require('lib/js/lib/ace/ace.js');
  282. $(this.input_area).css('border','1px solid #aaa');
  283. this.pre = $("<pre style='position: relative; height: 400px; \
  284. width: 100%; padding: 0px; border-radius: 0px;\
  285. margin: 0px; background-color: #fff;'>").appendTo(this.input_area).get(0);
  286. this.input = {};
  287. this.myid = wn.dom.set_unique_id(this.pre);
  288. this.editor = ace.edit(this.myid);
  289. if(me.df.options=='Markdown' || me.df.options=='HTML') {
  290. wn.require('lib/js/lib/ace/mode-html.js');
  291. var HTMLMode = require("ace/mode/html").Mode;
  292. me.editor.getSession().setMode(new HTMLMode());
  293. }
  294. else if(me.df.options=='Javascript') {
  295. wn.require('lib/js/lib/ace/mode-javascript.js');
  296. var JavascriptMode = require("ace/mode/javascript").Mode;
  297. me.editor.getSession().setMode(new JavascriptMode());
  298. }
  299. else if(me.df.options=='Python') {
  300. wn.require('lib/js/lib/ace/mode-python.js');
  301. var PythonMode = require("ace/mode/python").Mode;
  302. me.editor.getSession().setMode(new PythonMode());
  303. }
  304. this.input.set_input = function(v) {
  305. // during field refresh in run trigger, set_input is called
  306. // if called during on_change, setting doesn't make sense
  307. // and causes cursor to shift back to first position
  308. if(me.changing_value) return;
  309. me.setting_value = true;
  310. me.editor.getSession().setValue(v);
  311. me.setting_value = false;
  312. }
  313. this.get_value = function() {
  314. return me.editor.getSession().getValue(); // tinyMCE
  315. }
  316. $(cur_frm.wrapper).bind('render_complete', function() {
  317. me.editor.resize();
  318. me.editor.getSession().on('change', function() {
  319. if(me.setting_value) return;
  320. var val = me.get_value();
  321. if(locals[cur_frm.doctype][cur_frm.docname][me.df.fieldname] != val) {
  322. me.set(me.get_value());
  323. me.changing_value = true;
  324. me.run_trigger();
  325. me.changing_value = false;
  326. }
  327. })
  328. });
  329. }
  330. }
  331. _f.CodeField.prototype.init_editor = function() {
  332. // attach onchange methods
  333. var me = this;
  334. this.editor = tinymce.get(this.myid);
  335. this.editor.onKeyUp.add(function(ed, e) {
  336. me.set(ed.getContent());
  337. });
  338. this.editor.onPaste.add(function(ed, e) {
  339. me.set(ed.getContent());
  340. });
  341. this.editor.onSetContent.add(function(ed, e) {
  342. me.set(ed.getContent());
  343. });
  344. // reset content
  345. var c = locals[cur_frm.doctype][cur_frm.docname][this.df.fieldname];
  346. if(cur_frm && c) {
  347. this.editor.setContent(c);
  348. }
  349. }
  350. _f.CodeField.prototype.set_disp = function(val) {
  351. $y(this.disp_area, {width:'90%'})
  352. if(this.df.fieldtype=='Text Editor') {
  353. this.disp_area.innerHTML = val;
  354. } else {
  355. this.disp_area.innerHTML = '<textarea class="code_text" readonly=1>'+val+'</textarea>';
  356. }
  357. }
  358. // ======================================================================================