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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. if((!this.perm[this.df.permlevel]) || (!this.perm[this.df.permlevel][READ]) || this.df.hidden) {
  30. // no display
  31. return;
  32. }
  33. this.cell = this.frm.layout.addcell(this.df.width);
  34. $y(this.cell.wrapper, {padding: '8px'});
  35. _f.cur_col_break_width = this.df.width;
  36. var fn = this.df.fieldname?this.df.fieldname:this.df.label;
  37. // header
  38. if(this.df&&this.df.label){
  39. this.label = $a(this.cell.wrapper, 'div', '', '', this.df.label);
  40. }
  41. }
  42. _f.ColumnBreak.prototype.refresh = function(layout) {
  43. if(!this.cell)return; // no perm
  44. var fn = this.df.fieldname?this.df.fieldname:this.df.label;
  45. if(fn) {
  46. this.df = get_field(this.doctype, fn, this.docname);
  47. // hidden
  48. if(this.set_hidden!=this.df.hidden) {
  49. if(this.df.hidden)
  50. this.cell.hide();
  51. else
  52. this.cell.show();
  53. this.set_hidden = this.df.hidden;
  54. }
  55. }
  56. }
  57. // ======================================================================================
  58. _f.SectionBreak = function() {
  59. this.set_input = function() { };
  60. }
  61. _f.SectionBreak.prototype.make_row = function() {
  62. this.row = this.df.label ? this.frm.layout.addrow() : this.frm.layout.addsubrow();
  63. }
  64. _f.SectionBreak.prototype.make_collapsible = function(head) {
  65. var me = this;
  66. var div = $a(head,'div','',{paddingBottom: '3px', borderBottom:'1px solid #AAA'});
  67. if(cur_frm.meta.in_dialog) $y(div, {marginLeft:'8px'});
  68. // checkbox
  69. this.chk = $a_input(div, 'checkbox',null,{marginRight:'8px'})
  70. // label
  71. if(this.df.label) {
  72. this.label = $a(div, 'h3', '', {display:'inline'}, this.df.label);
  73. }
  74. // description
  75. var d = this.df.description;
  76. if(d) {
  77. this.desc_area = $a(div, 'span', 'field_description', {marginLeft:'8px'});
  78. this.desc_area.innerHTML = d.substr(0,50) + (d.length > 50 ? '...' : '');
  79. }
  80. // back to top
  81. var span = $a(div, 'i', 'icon-arrow-up', {cssFloat:'right', marginRight:'8px',
  82. cursor:'pointer', verticalAlign:'middle', marginTop:'7px'})
  83. span.title = 'Go to top';
  84. span.onclick = function() { scroll(0, 0); }
  85. // exp / collapse
  86. this.chk.onclick = function() {
  87. if(this.checked) me.expand();
  88. else me.collapse();
  89. }
  90. this.expand = function() {
  91. $(me.row.main_body).slideDown();
  92. }
  93. this.collapse = function() {
  94. $(me.row.main_body).slideUp();
  95. }
  96. // hide by default
  97. if(me.frm.section_count) {
  98. $dh(this.row.main_body);
  99. } else {
  100. this.chk.checked = true;
  101. }
  102. }
  103. // ======================================================================================
  104. _f.SectionBreak.prototype.make_simple_section = function(with_header) {
  105. this.wrapper = $a(this.row.main_head, 'div', '', {margin:'8px 8px 0px 0px'});
  106. var me = this;
  107. // colour
  108. if(this.df.colour) {
  109. var col = this.df.colour.split(':')[1];
  110. if(col!='FFF') {
  111. $y(this.row.sub_wrapper, { margin:'8px', padding: '0px' ,backgroundColor: ('#' + col)} );
  112. }
  113. }
  114. if(with_header) {
  115. if(this.df.label && this.df.options!='Simple') {
  116. this.make_collapsible(this.wrapper);
  117. } else {
  118. // divider
  119. $y(this.wrapper, {paddingBottom:'4px'});
  120. if(this.df.label) {
  121. $a(this.wrapper, 'h3', '', {}, this.df.label);
  122. }
  123. }
  124. }
  125. // indent
  126. $y(this.row.body, { marginLeft:'17px' });
  127. }
  128. // ======================================================================================
  129. _f.SectionBreak.prototype.add_to_sections = function() {
  130. this.sec_id = this.frm.sections.length;
  131. this.frm.sections[this.sec_id] = this;
  132. this.frm.sections_by_label[this.df.label] = this;
  133. }
  134. _f.cur_sec_header = null;
  135. _f.SectionBreak.prototype.make_body = function() {
  136. if((!this.perm[this.df.permlevel]) || (!this.perm[this.df.permlevel][READ]) || this.df.hidden) {
  137. // no display
  138. return;
  139. }
  140. var me = this;
  141. if(this.df){
  142. this.make_row();
  143. this.make_simple_section(1, 1);
  144. }
  145. }
  146. // ======================================================================================
  147. _f.SectionBreak.prototype.refresh = function(layout) {
  148. var fn = this.df.fieldname?this.df.fieldname:this.df.label;
  149. if(fn)
  150. this.df = get_field(this.doctype, fn, this.docname);
  151. // hidden
  152. if(this.set_hidden!=this.df.hidden) {
  153. if(this.df.hidden) {
  154. if(this.frm.meta.section_style=='Tabbed') {
  155. $dh(this.mytab);
  156. } else if(this.tray_item)
  157. this.tray_item.hide();
  158. if(this.row)this.row.hide();
  159. } else {
  160. if(this.frm.meta.section_style=='Tabbed') {
  161. $di(this.mytab);
  162. } else if(this.tray_item)
  163. this.tray_item.show();
  164. if(this.expanded)this.row.show();
  165. }
  166. this.set_hidden = this.df.hidden;
  167. }
  168. }
  169. // Image field definition
  170. // ======================================================================================
  171. _f.ImageField = function() { this.images = {}; }
  172. _f.ImageField.prototype = new Field();
  173. _f.ImageField.prototype.onmake = function() {
  174. this.no_img = $a(this.wrapper, 'div','no_img');
  175. this.no_img.innerHTML = "No Image";
  176. $dh(this.no_img);
  177. }
  178. _f.ImageField.prototype.get_image_src = function(doc) {
  179. if(doc.file_list) {
  180. file = doc.file_list.split(',');
  181. // if image
  182. extn = file[0].split('.');
  183. extn = extn[extn.length - 1].toLowerCase();
  184. var img_extn_list = ['gif', 'jpg', 'bmp', 'jpeg', 'jp2', 'cgm', 'ief', 'jpm', 'jpx', 'png', 'tiff', 'jpe', 'tif'];
  185. if(in_list(img_extn_list, extn)) {
  186. var src = webnotes.request.url + "?cmd=downloadfile&file_id="+file[1];
  187. }
  188. } else {
  189. var src = "";
  190. }
  191. return src;
  192. }
  193. _f.ImageField.prototype.onrefresh = function() {
  194. var me = this;
  195. if(!this.images[this.docname]) this.images[this.docname] = $a(this.wrapper, 'img');
  196. else $di(this.images[this.docname]);
  197. var img = this.images[this.docname]
  198. // hide all other
  199. for(var dn in this.images) if(dn!=this.docname)$dh(this.images[dn]);
  200. var doc = locals[this.frm.doctype][this.frm.docname];
  201. if(!this.df.options) var src = this.get_image_src(doc);
  202. else var src = webnotes.request.url + '?cmd=get_file&fname='+this.df.options+"&__account="+account_id + (__sid150 ? ("&sid150="+__sid150) : '');
  203. if(src) {
  204. $dh(this.no_img);
  205. if(img.getAttribute('src')!=src) img.setAttribute('src',src);
  206. canvas = this.wrapper;
  207. canvas.img = this.images[this.docname];
  208. canvas.style.overflow = "auto";
  209. $w(canvas, "100%");
  210. if(!this.col_break_width)this.col_break_width = '100%';
  211. var allow_width = cint(1000 * (cint(this.col_break_width)-10) / 100);
  212. if((!img.naturalWidth) || cint(img.naturalWidth)>allow_width)
  213. $w(img, allow_width + 'px');
  214. } else {
  215. $ds(this.no_img);
  216. }
  217. }
  218. _f.ImageField.prototype.set_disp = function (val) { }
  219. _f.ImageField.prototype.set = function (val) { }
  220. // Table
  221. // ======================================================================================
  222. _f.TableField = function() { };
  223. _f.TableField.prototype = new Field();
  224. _f.TableField.prototype.with_label = 0;
  225. _f.TableField.prototype.make_body = function() {
  226. if(this.perm[this.df.permlevel] && this.perm[this.df.permlevel][READ]) {
  227. // add comment area
  228. if(this.df.description) {
  229. this.desc_area = $a(this.parent, 'div', 'field_description', '', this.df.description)
  230. }
  231. this.grid = new _f.FormGrid(this);
  232. if(this.frm)this.frm.grids[this.frm.grids.length] = this;
  233. this.grid.make_buttons();
  234. }
  235. }
  236. _f.TableField.prototype.refresh = function() {
  237. if(!this.grid)return;
  238. // hide / show grid
  239. var st = this.get_status();
  240. if(!this.df['default'])
  241. this.df['default']='';
  242. this.grid.can_add_rows = false;
  243. this.grid.can_edit = false
  244. if(st=='Write') {
  245. if(cur_frm.editable && this.perm[this.df.permlevel] && this.perm[this.df.permlevel][WRITE]) {
  246. this.grid.can_edit = true;
  247. if(this.df['default'].toLowerCase()!='no toolbar')
  248. this.grid.can_add_rows = true;
  249. }
  250. // submitted or cancelled
  251. if(cur_frm.editable && cur_frm.doc.docstatus > 0) {
  252. if(this.df.allow_on_submit && cur_frm.doc.docstatus==1) {
  253. this.grid.can_edit = true;
  254. if(this.df['default'].toLowerCase()=='no toolbar') {
  255. this.grid.can_add_rows = false;
  256. } else {
  257. this.grid.can_add_rows = true;
  258. }
  259. } else {
  260. this.grid.can_add_rows = false;
  261. this.grid.can_edit = false;
  262. }
  263. }
  264. if(this.df['default'].toLowerCase()=='no add rows') {
  265. this.grid.can_add_rows = false;
  266. }
  267. }
  268. //if(this.old_status!=st) {
  269. if(st=='Write') {
  270. // nothing
  271. this.grid.show();
  272. } else if(st=='Read') {
  273. this.grid.show();
  274. } else {
  275. this.grid.hide();
  276. }
  277. // this.old_status = st; // save this if next time
  278. //}
  279. this.grid.refresh();
  280. }
  281. _f.TableField.prototype.set = function(v) { }; // nothing
  282. _f.TableField.prototype.set_input = function(v) { }; // nothing
  283. // ==============================================================
  284. _f.CodeField = function() { };
  285. _f.CodeField.prototype = new Field();
  286. _f.CodeField.prototype.make_input = function() {
  287. var me = this;
  288. this.label_span.innerHTML = this.df.label;
  289. this.input = $a(this.input_area, 'textarea','code_text',{fontSize:'12px'});
  290. this.myid = wn.dom.set_unique_id(this.input);
  291. this.input.set_input = function(v) {
  292. if(me.editor) {
  293. me.editor.setContent(v);
  294. } else {
  295. me.input.value = v;
  296. me.input.innerHTML = v;
  297. }
  298. }
  299. this.input.onchange = function() {
  300. if(me.editor) {
  301. //me.set(tinymce.get(me.myid).getContent());
  302. } else {
  303. me.set(me.input.value);
  304. }
  305. me.run_trigger();
  306. }
  307. this.get_value = function() {
  308. if(me.editor) {
  309. return me.editor.getContent(); // tinyMCE
  310. } else {
  311. return this.input.value;
  312. }
  313. }
  314. if(this.df.fieldtype=='Text Editor') {
  315. // setup tiny mce
  316. $(me.input).tinymce({
  317. // Location of TinyMCE script
  318. script_url : 'lib/js/legacy/tiny_mce_33/tiny_mce.js',
  319. // General options
  320. theme : "advanced",
  321. plugins : "style,inlinepopups,table",
  322. extended_valid_elements: "div[id|dir|class|align|style]",
  323. // w/h
  324. width: '100%',
  325. height: '360px',
  326. // buttons
  327. theme_advanced_buttons1 : "bold,italic,underline,strikethrough,hr,|,justifyleft,justifycenter,justifyright,|,formatselect,fontselect,fontsizeselect",
  328. theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,code,|,forecolor,backcolor,|,tablecontrols",
  329. theme_advanced_buttons3 : "",
  330. theme_advanced_toolbar_location : "top",
  331. theme_advanced_toolbar_align : "left",
  332. content_css: "js/tiny_mce_33/custom_content.css",
  333. oninit: function() { me.init_editor(); }
  334. });
  335. } else {
  336. $y(me.input, {fontFamily:'Courier, Fixed'});
  337. }
  338. }
  339. _f.CodeField.prototype.init_editor = function() {
  340. // attach onchange methods
  341. var me = this;
  342. this.editor = tinymce.get(this.myid);
  343. this.editor.onKeyUp.add(function(ed, e) {
  344. me.set(ed.getContent());
  345. });
  346. this.editor.onPaste.add(function(ed, e) {
  347. me.set(ed.getContent());
  348. });
  349. this.editor.onSetContent.add(function(ed, e) {
  350. me.set(ed.getContent());
  351. });
  352. // reset content
  353. var c = locals[cur_frm.doctype][cur_frm.docname][this.df.fieldname];
  354. if(cur_frm && c) {
  355. this.editor.setContent(c);
  356. }
  357. }
  358. _f.CodeField.prototype.set_disp = function(val) {
  359. $y(this.disp_area, {width:'90%'})
  360. if(this.df.fieldtype=='Text Editor') {
  361. this.disp_area.innerHTML = val;
  362. } else {
  363. this.disp_area.innerHTML = '<textarea class="code_text" readonly=1>'+val+'</textarea>';
  364. }
  365. }
  366. // ======================================================================================