Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

947 rindas
25 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. wn.ui.form.make_control = function(opts) {
  4. var control_class_name = "Control" + opts.df.fieldtype.replace(/ /g, "");
  5. if(wn.ui.form[control_class_name]) {
  6. return new wn.ui.form[control_class_name](opts);
  7. } else {
  8. console.log("Invalid Control Name: " + opts.df.fieldtype);
  9. }
  10. }
  11. // old style
  12. function make_field(docfield, doctype, parent, frm, in_grid, hide_label) { // Factory
  13. return wn.ui.form.make_control({
  14. df: docfield,
  15. doctype: doctype,
  16. parent: parent,
  17. hide_label: hide_label,
  18. frm: frm
  19. });
  20. }
  21. wn.ui.form.Control = Class.extend({
  22. init: function(opts) {
  23. $.extend(this, opts);
  24. this.make();
  25. // if developer_mode=1, show fieldname as tooltip
  26. if(wn.boot.profile && wn.boot.profile.name==="Administrator" &&
  27. wn.boot.developer_mode===1 && this.$wrapper) {
  28. this.$wrapper.attr("title", wn._(this.df.fieldname));
  29. }
  30. },
  31. make: function() {
  32. this.make_wrapper();
  33. this.wrapper = this.$wrapper.get(0);
  34. this.wrapper.fieldobj = this; // reference for event handlers
  35. },
  36. make_wrapper: function() {
  37. this.$wrapper = $("<div>").appendTo(this.parent);
  38. },
  39. // returns "Read", "Write" or "None"
  40. // as strings based on permissions
  41. get_status: function(explain) {
  42. if(!this.doctype)
  43. return "Write";
  44. return wn.perm.get_field_display_status(this.df,
  45. locals[this.doctype][this.docname], this.perm || this.frm.perm, explain);
  46. },
  47. refresh: function() {
  48. this.disp_status = this.get_status();
  49. this.$wrapper
  50. && this.$wrapper.toggle(this.disp_status!="None")
  51. && this.$wrapper.trigger("refresh");
  52. },
  53. get_doc: function() {
  54. return this.doctype && this.docname
  55. && locals[this.doctype] && locals[this.doctype][this.docname] || {};
  56. },
  57. parse_validate_and_set_in_model: function(value) {
  58. var me = this;
  59. this.inside_change_event = true;
  60. if(this.parse) value = this.parse(value);
  61. var set = function(value) {
  62. me.set_model_value(value);
  63. me.inside_change_event = false;
  64. me.set_mandatory && me.set_mandatory(value);
  65. }
  66. this.validate ? this.validate(value, set) : set(value);
  67. },
  68. get_parsed_value: function() {
  69. var me = this;
  70. return this.get_value ?
  71. (this.parse ? this.parse(this.get_value()) : this.get_value()) :
  72. undefined;
  73. },
  74. set_model_value: function(value) {
  75. if(wn.model.set_value(this.doctype, this.docname, this.df.fieldname,
  76. value, this.df.fieldtype)) {
  77. this.last_value = value;
  78. }
  79. },
  80. });
  81. wn.ui.form.ControlHTML = wn.ui.form.Control.extend({
  82. make: function() {
  83. this._super();
  84. var me = this;
  85. this.disp_area = this.wrapper;
  86. this.$wrapper.on("refresh", function() {
  87. if(me.df.options)
  88. me.$wrapper.html(me.df.options);
  89. })
  90. }
  91. });
  92. wn.ui.form.ControlImage = wn.ui.form.Control.extend({
  93. make: function() {
  94. this._super();
  95. var me = this;
  96. this.$wrapper
  97. .css({"margin-bottom": "10px", "margin-right": "15px", "float": "right", "text-align": "right", "max-width": "100%"})
  98. .on("refresh", function() {
  99. me.$wrapper.empty();
  100. if(me.df.options && me.frm.doc[me.df.options]) {
  101. $("<img src='"+me.frm.doc[me.df.options]+"' style='max-width: 70%;'>")
  102. .appendTo(me.$wrapper);
  103. } else {
  104. $("<div class='missing-image'><i class='icon-camera'></i></div>")
  105. .appendTo(me.$wrapper)
  106. }
  107. return false;
  108. })
  109. }
  110. });
  111. wn.ui.form.ControlReadOnly = wn.ui.form.Control.extend({
  112. make: function() {
  113. this._super();
  114. var me = this;
  115. this.$wrapper.on("refresh", function() {
  116. var value = wn.model.get_value(me.doctype, me.docname, me.fieldname);
  117. me.$wrapper.html(value);
  118. })
  119. },
  120. });
  121. wn.ui.form.ControlInput = wn.ui.form.Control.extend({
  122. horizontal: true,
  123. make: function() {
  124. // parent element
  125. this._super();
  126. this.set_input_areas();
  127. // set description
  128. this.set_max_width();
  129. this.setup_update_on_refresh();
  130. },
  131. make_wrapper: function() {
  132. if(this.only_input) {
  133. this.$wrapper = $('<div class="form-group">').appendTo(this.parent);
  134. } else {
  135. this.$wrapper = $('<div class="form-horizontal">\
  136. <div class="form-group row" style="margin: 0px">\
  137. <label class="control-label small col-xs-'+(this.horizontal?"4":"12")
  138. +'" style="padding-right: 0px; color: #777;"></label>\
  139. <div class="col-xs-'+(this.horizontal?"8":"12")+'">\
  140. <div class="control-input"></div>\
  141. <div class="control-value like-disabled-input" style="display: none;"></div>\
  142. <p class="help-box small text-muted"></p>\
  143. </div>\
  144. </div>\
  145. </div>').appendTo(this.parent);
  146. if(!this.horizontal) {
  147. this.$wrapper.removeClass("form-horizontal");
  148. }
  149. }
  150. },
  151. set_input_areas: function() {
  152. if(this.only_input) {
  153. this.input_area = this.wrapper;
  154. } else {
  155. this.label_area = this.label_span = this.$wrapper.find("label").get(0);
  156. this.input_area = this.$wrapper.find(".control-input").get(0);
  157. this.disp_area = this.$wrapper.find(".control-value").get(0);
  158. }
  159. },
  160. set_max_width: function() {
  161. if(this.horizontal) {
  162. this.$wrapper.css({"max-width": "600px"});
  163. }
  164. },
  165. // update input value, label, description
  166. // display (show/hide/read-only),
  167. // mandatory style on refresh
  168. setup_update_on_refresh: function() {
  169. var me = this;
  170. this.$wrapper.on("refresh", function() {
  171. if(me.disp_status != "None") {
  172. // refresh value
  173. if(me.doctype && me.docname) {
  174. me.value = wn.model.get_value(me.doctype, me.docname, me.df.fieldname);
  175. }
  176. if(me.disp_status=="Write") {
  177. me.disp_area && $(me.disp_area).toggle(false);
  178. $(me.input_area).toggle(true);
  179. !me.has_input && me.make_input();
  180. if(me.doctype && me.docname)
  181. me.set_input(me.value);
  182. } else {
  183. $(me.input_area).toggle(false);
  184. $(me.input_area).find("input").prop("disabled", true);
  185. me.disp_area && $(me.disp_area)
  186. .toggle(true)
  187. .html(
  188. wn.format(me.value, me.df, {no_icon:true}, locals[me.doctype][me.name])
  189. );
  190. }
  191. me.set_description();
  192. me.set_label();
  193. me.set_mandatory(me.value);
  194. }
  195. return false;
  196. });
  197. },
  198. bind_change_event: function() {
  199. var me = this;
  200. this.$input && this.$input.on("change", this.change || function() {
  201. me.doctype && me.docname && me.get_value
  202. && me.parse_validate_and_set_in_model(me.get_value()); } );
  203. },
  204. set_label: function(label) {
  205. if(label) this.df.label = label;
  206. if(this.only_input || this.df.label==this._label)
  207. return;
  208. // var icon = wn.ui.form.fieldtype_icons[this.df.fieldtype];
  209. // if(this.df.fieldtype==="Link") {
  210. // icon = wn.boot.doctype_icons[this.df.options];
  211. // } else if(this.df.link_doctype) {
  212. // icon = wn.boot.doctype_icons[this.df.link_doctype];
  213. // }
  214. var icon = "";
  215. this.label_span.innerHTML = (icon ? '<i class="'+icon+'"></i> ' : "") +
  216. wn._(this.df.label) || "&nbsp;";
  217. this._label = this.df.label;
  218. },
  219. set_description: function() {
  220. if(this.only_input || this.df.description===this._description)
  221. return;
  222. if(this.df.description) {
  223. this.$wrapper.find(".help-box").html(wn._(this.df.description));
  224. } else {
  225. this.set_empty_description();
  226. }
  227. this._description = this.df.description;
  228. },
  229. set_empty_description: function() {
  230. this.$wrapper.find(".help-box").html("");
  231. },
  232. set_mandatory: function(value) {
  233. this.$wrapper.toggleClass("has-error", (this.df.reqd
  234. && (value==null || value==="")) ? true : false);
  235. },
  236. });
  237. wn.ui.form.ControlData = wn.ui.form.ControlInput.extend({
  238. html_element: "input",
  239. input_type: "text",
  240. make_input: function() {
  241. this.$input = $("<"+ this.html_element +">")
  242. .attr("type", this.input_type)
  243. .addClass("col-md-12 input-with-feedback form-control")
  244. .prependTo(this.input_area)
  245. this.set_input_attributes();
  246. this.input = this.$input.get(0);
  247. this.has_input = true;
  248. this.bind_change_event();
  249. },
  250. set_input_attributes: function() {
  251. this.$input
  252. .attr("data-fieldtype", this.df.fieldtype)
  253. .attr("data-fieldname", this.df.fieldname)
  254. .attr("placeholder", this.df.placeholder || "")
  255. if(this.doctype)
  256. this.$input.attr("data-doctype", this.doctype);
  257. if(this.df.input_css)
  258. this.$input.css(this.df.input_css);
  259. },
  260. set_input: function(val) {
  261. this.$input.val(this.format_for_input(val));
  262. this.last_value = val;
  263. this.set_mandatory && this.set_mandatory(val);
  264. },
  265. get_value: function() {
  266. return this.$input ? this.$input.val() : undefined;
  267. },
  268. format_for_input: function(val) {
  269. return val==null ? "" : val;
  270. },
  271. validate: function(v, callback) {
  272. if(this.df.options == 'Phone') {
  273. if(v+''=='')return '';
  274. v1 = ''
  275. // phone may start with + and must only have numbers later, '-' and ' ' are stripped
  276. v = v.replace(/ /g, '').replace(/-/g, '').replace(/\(/g, '').replace(/\)/g, '');
  277. // allow initial +,0,00
  278. if(v && v.substr(0,1)=='+') {
  279. v1 = '+'; v = v.substr(1);
  280. }
  281. if(v && v.substr(0,2)=='00') {
  282. v1 += '00'; v = v.substr(2);
  283. }
  284. if(v && v.substr(0,1)=='0') {
  285. v1 += '0'; v = v.substr(1);
  286. }
  287. v1 += cint(v) + '';
  288. callback(v1);
  289. } else if(this.df.options == 'Email') {
  290. if(v+''=='')return '';
  291. if(!validate_email(v)) {
  292. msgprint(wn._("Invalid Email") + ": " + v);
  293. callback("");
  294. } else
  295. callback(v);
  296. } else {
  297. callback(v);
  298. }
  299. }
  300. });
  301. wn.ui.form.ControlPassword = wn.ui.form.ControlData.extend({
  302. input_type: "password"
  303. });
  304. wn.ui.form.ControlInt = wn.ui.form.ControlData.extend({
  305. make_input: function() {
  306. var me = this;
  307. this._super();
  308. this.$input
  309. .css({"text-align": "right"})
  310. .on("focus", function() {
  311. setTimeout(function() {
  312. if(!document.activeElement) return;
  313. me.validate(document.activeElement.value, function(val) {
  314. document.activeElement.value = val;
  315. });
  316. document.activeElement.select()
  317. }, 100);
  318. return false;
  319. })
  320. },
  321. validate: function(value, callback) {
  322. return callback(cint(value, null));
  323. }
  324. });
  325. wn.ui.form.ControlFloat = wn.ui.form.ControlInt.extend({
  326. validate: function(value, callback) {
  327. return callback(isNaN(parseFloat(value)) ? null : flt(value));
  328. },
  329. format_for_input: function(value) {
  330. var formatted_value = format_number(parseFloat(value),
  331. null, cint(wn.boot.sysdefaults.float_precision, null));
  332. return isNaN(parseFloat(value)) ? "" : formatted_value;
  333. }
  334. });
  335. wn.ui.form.ControlCurrency = wn.ui.form.ControlFloat.extend({
  336. format_for_input: function(value) {
  337. var formatted_value = format_number(parseFloat(value),
  338. get_number_format(this.get_currency()));
  339. return isNaN(parseFloat(value)) ? "" : formatted_value;
  340. },
  341. get_currency: function() {
  342. return wn.meta.get_field_currency(this.df, this.get_doc());
  343. }
  344. });
  345. wn.ui.form.ControlPercent = wn.ui.form.ControlFloat;
  346. wn.ui.form.ControlDate = wn.ui.form.ControlData.extend({
  347. datepicker_options: {
  348. altFormat:'yy-mm-dd',
  349. changeYear: true,
  350. yearRange: "-70Y:+10Y",
  351. },
  352. make_input: function() {
  353. this._super();
  354. this.set_datepicker();
  355. },
  356. set_datepicker: function() {
  357. this.datepicker_options.dateFormat =
  358. (wn.boot.sysdefaults.date_format || 'yyyy-mm-dd').replace("yyyy", "yy")
  359. this.$input.datepicker(this.datepicker_options);
  360. },
  361. parse: function(value) {
  362. return value ? dateutil.user_to_str(value) : value;
  363. },
  364. format_for_input: function(value) {
  365. return value ? dateutil.str_to_user(value) : "";
  366. },
  367. validate: function(value, callback) {
  368. var value = wn.datetime.validate(value);
  369. if(!value) {
  370. msgprint (wn._("Date must be in format") + ": " + (sys_defaults.date_format || "yyyy-mm-dd"));
  371. callback("");
  372. }
  373. return callback(value);
  374. }
  375. })
  376. import_timepicker = function() {
  377. wn.require("lib/js/lib/jquery/jquery.ui.slider.min.js");
  378. wn.require("lib/js/lib/jquery/jquery.ui.sliderAccess.js");
  379. wn.require("lib/js/lib/jquery/jquery.ui.timepicker-addon.css");
  380. wn.require("lib/js/lib/jquery/jquery.ui.timepicker-addon.js");
  381. }
  382. wn.ui.form.ControlTime = wn.ui.form.ControlData.extend({
  383. make_input: function() {
  384. import_timepicker();
  385. this._super();
  386. this.$input.timepicker({
  387. timeFormat: 'hh:mm:ss',
  388. });
  389. }
  390. });
  391. wn.ui.form.ControlDatetime = wn.ui.form.ControlDate.extend({
  392. set_datepicker: function() {
  393. this.datepicker_options.timeFormat = "hh:mm:ss";
  394. this.datepicker_options.dateFormat =
  395. (wn.boot.sysdefaults.date_format || 'yy-mm-dd').replace('yyyy','yy');
  396. this.$input.datetimepicker(this.datepicker_options);
  397. },
  398. make_input: function() {
  399. import_timepicker();
  400. this._super();
  401. },
  402. });
  403. wn.ui.form.ControlText = wn.ui.form.ControlData.extend({
  404. html_element: "textarea",
  405. horizontal: false
  406. });
  407. wn.ui.form.ControlLongText = wn.ui.form.ControlText;
  408. wn.ui.form.ControlSmallText = wn.ui.form.ControlText;
  409. wn.ui.form.ControlCheck = wn.ui.form.ControlData.extend({
  410. input_type: "checkbox",
  411. make_wrapper: function() {
  412. this.$wrapper = $('<div class="form-group row" style="margin: 0px;">\
  413. <div class="col-md-offset-4 col-md-8">\
  414. <div class="checkbox" style="margin: 5px 0px">\
  415. <label>\
  416. <span class="input-area"></span>\
  417. <span class="disp-area" style="display:none;"></span>\
  418. <span class="label-area small text-muted"></span>\
  419. </label>\
  420. <p class="help-box small text-muted"></p>\
  421. </div>\
  422. </div>\
  423. </div>').appendTo(this.parent)
  424. },
  425. set_input_areas: function() {
  426. this.label_area = this.label_span = this.$wrapper.find(".label-area").get(0);
  427. this.input_area = this.$wrapper.find(".input-area").get(0);
  428. this.disp_area = this.$wrapper.find(".disp-area").get(0);
  429. },
  430. make_input: function() {
  431. this._super();
  432. this.$input.removeClass("col-md-12 form-control");
  433. },
  434. parse: function(value) {
  435. return this.input.checked ? 1 : 0;
  436. },
  437. validate: function(value, callback) {
  438. return callback(cint(value));
  439. },
  440. set_input: function(value) {
  441. this.input.checked = value ? 1 : 0;
  442. this.last_value = value;
  443. }
  444. });
  445. wn.ui.form.ControlButton = wn.ui.form.ControlData.extend({
  446. make_input: function() {
  447. var me = this;
  448. this.$input = $('<button class="btn btn-default">')
  449. .prependTo(me.input_area)
  450. .on("click", function() {
  451. me.onclick();
  452. });
  453. this.input = this.$input.get(0);
  454. this.set_input_attributes();
  455. this.has_input = true;
  456. },
  457. onclick: function() {
  458. if(this.frm && this.frm.doc && this.frm.cscript) {
  459. if(this.frm.cscript[this.df.fieldname]) {
  460. this.frm.script_manager.trigger(this.df.fieldname, this.doctype, this.docname);
  461. } else {
  462. this.frm.runscript(this.df.options, this);
  463. }
  464. }
  465. else if(this.df.click) {
  466. this.df.click();
  467. }
  468. },
  469. set_input_areas: function() {
  470. this._super();
  471. $(this.disp_area).removeClass();
  472. },
  473. set_empty_description: function() {
  474. this.$wrapper.find(".help-box").empty().toggle(false);
  475. },
  476. set_label: function() {
  477. $(this.label_span).html("&nbsp;");
  478. this.$input && this.$input.html(this.df.label);
  479. }
  480. });
  481. wn.ui.form.ControlAttach = wn.ui.form.ControlButton.extend({
  482. onclick: function() {
  483. if(!this.dialog) {
  484. this.dialog = new wn.ui.Dialog({
  485. title: wn._(this.df.label || "Upload Attachment"),
  486. });
  487. }
  488. $(this.dialog.body).empty();
  489. this.set_upload_options();
  490. wn.upload.make(this.upload_options);
  491. this.dialog.show();
  492. },
  493. set_upload_options: function() {
  494. var me = this;
  495. this.upload_options = {
  496. parent: this.dialog.body,
  497. args: {},
  498. max_width: this.df.max_width,
  499. max_height: this.df.max_height,
  500. callback: function() {
  501. me.dialog.hide();
  502. me.on_upload_complete(fileid, filename, r);
  503. me.show_ok_on_button();
  504. },
  505. onerror: function() {
  506. me.dialog.hide();
  507. },
  508. }
  509. if(this.frm) {
  510. this.upload_options.args = {
  511. from_form: 1,
  512. doctype: this.frm.doctype,
  513. docname: this.frm.docname,
  514. }
  515. } else {
  516. this.upload_options.on_attach = function(fileobj, dataurl) {
  517. me.dialog.hide();
  518. me.fileobj = fileobj;
  519. me.dataurl = dataurl;
  520. if(me.on_attach) {
  521. me.on_attach()
  522. }
  523. if(me.df.on_attach) {
  524. me.df.on_attach(fileobj, dataurl);
  525. }
  526. me.show_ok_on_button();
  527. }
  528. }
  529. },
  530. get_value: function() {
  531. return this.fileobj ? (this.fileobj.filename + "," + this.dataurl) : null;
  532. },
  533. on_upload_complete: function(fileid, filename, r) {
  534. this.frm && this.frm.attachments.update_attachment(fileid, filename, this.df.fieldname, r);
  535. },
  536. show_ok_on_button: function() {
  537. if(!$(this.input).find(".icon-ok").length) {
  538. $(this.input).html('<i class="icon-ok"></i> ' + this.df.label);
  539. }
  540. }
  541. });
  542. wn.ui.form.ControlAttachImage = wn.ui.form.ControlAttach.extend({
  543. make_input: function() {
  544. this._super();
  545. this.img = $("<img class='img-responsive'>").appendTo($('<div style="margin: 7px 0px;">\
  546. <div class="missing-image"><i class="icon-camera"></i></div></div>').prependTo(this.input_area)).toggle(false);
  547. },
  548. on_attach: function() {
  549. $(this.input_area).find(".missing-image").toggle(false);
  550. this.img.attr("src", this.dataurl).toggle(true);
  551. }
  552. });
  553. wn.ui.form.ControlSelect = wn.ui.form.ControlData.extend({
  554. html_element: "select",
  555. make_input: function() {
  556. var me = this;
  557. this._super();
  558. if(this.df.options=="attach_files:") {
  559. this.setup_attachment();
  560. }
  561. this.set_options();
  562. },
  563. set_input: function(value) {
  564. // refresh options first - (new ones??)
  565. this.set_options();
  566. this._super(value);
  567. // not a possible option, repair
  568. if(this.doctype && this.docname) {
  569. // model value is not an option,
  570. // set the default option (displayed)
  571. var input_value = this.$input.val();
  572. var model_value = wn.model.get_value(this.doctype, this.docname, this.df.fieldname);
  573. if(input_value != (model_value || "")) {
  574. this.set_model_value(input_value);
  575. } else {
  576. this.last_value = value;
  577. }
  578. }
  579. },
  580. setup_attachment: function() {
  581. var me = this;
  582. $(this.input).css({"width": "85%", "display": "inline-block"});
  583. this.$attach = $("<button class='btn btn-default' title='"+ wn._("Add attachment") + "'\
  584. style='padding-left: 6px; padding-right: 6px; margin-right: 6px;'>\
  585. <i class='icon-plus'></i></button>")
  586. .click(function() {
  587. me.frm.attachments.new_attachment(me.df.fieldname);
  588. })
  589. .prependTo(this.input_area);
  590. $(document).on("upload_complete", function(event, filename, file_url) {
  591. if(cur_frm === me.frm) {
  592. me.set_options();
  593. }
  594. })
  595. this.$wrapper.on("refresh", function() {
  596. me.$attach.toggle(!me.frm.doc.__islocal);
  597. });
  598. },
  599. set_options: function() {
  600. var options = this.df.options || [];
  601. if(this.df.options=="attach_files:") {
  602. options = this.get_file_attachment_list();
  603. } else if(typeof this.df.options==="string") {
  604. options = this.df.options.split("\n");
  605. }
  606. if(this.in_filter && options[0] != "") {
  607. options = add_lists([''], options);
  608. }
  609. var selected = this.$input.find(":selected").val();
  610. this.$input.empty().add_options(options || []);
  611. if(selected) this.$input.val(selected);
  612. },
  613. get_file_attachment_list: function() {
  614. if(!this.frm) return;
  615. var fl = wn.model.docinfo[this.frm.doctype][this.frm.docname];
  616. if(fl && fl.attachments) {
  617. fl = fl.attachments;
  618. this.set_description("");
  619. var options = [""];
  620. for(var fname in fl) {
  621. if(fname.indexOf("/")===-1)
  622. fname = "files/" + fname;
  623. options.push(fname);
  624. }
  625. return options;
  626. } else {
  627. this.set_description(wn._("Please attach a file first."))
  628. return [""];
  629. }
  630. }
  631. });
  632. // special features for link
  633. // buttons
  634. // autocomplete
  635. // link validation
  636. // custom queries
  637. // add_fetches
  638. wn.ui.form.ControlLink = wn.ui.form.ControlData.extend({
  639. make_input: function() {
  640. var me = this;
  641. $('<div class="link-field" style="display: table; width: 100%;">\
  642. <input type="text" class="input-with-feedback form-control" \
  643. style="display: table-cell">\
  644. <span class="link-field-btn" style="display: table-cell">\
  645. <a class="btn-search" title="Search Link">\
  646. <i class="icon-search"></i>\
  647. </a><a class="btn-open" title="Open Link">\
  648. <i class="icon-play"></i>\
  649. </a><a class="btn-new" title="Make New">\
  650. <i class="icon-plus"></i>\
  651. </a>\
  652. </span>\
  653. </div>').prependTo(this.input_area);
  654. this.$input_area = $(this.input_area);
  655. this.$input = this.$input_area.find('input');
  656. this.set_input_attributes();
  657. this.$input.on("focus", function() {
  658. setTimeout(function() {
  659. if(!me.$input.val()) {
  660. me.$input.val("%").trigger("keydown");
  661. }
  662. }, 1000)
  663. })
  664. this.input = this.$input.get(0);
  665. this.has_input = true;
  666. //this.bind_change_event();
  667. var me = this;
  668. this.setup_buttons();
  669. //this.setup_typeahead();
  670. this.setup_autocomplete();
  671. },
  672. setup_buttons: function() {
  673. var me = this;
  674. // magnifier - search
  675. this.$input_area.find(".btn-search").on("click", function() {
  676. new wn.ui.form.LinkSelector({
  677. doctype: me.df.options,
  678. target: me,
  679. txt: me.get_value()
  680. });
  681. });
  682. // open
  683. if(wn.model.can_read(me.df.options)) {
  684. this.$input_area.find(".btn-open").on("click", function() {
  685. var value = me.get_value();
  686. if(value && me.df.options) wn.set_route("Form", me.df.options, value);
  687. });
  688. } else {
  689. this.$input_area.find(".btn-open").remove();
  690. }
  691. // new
  692. if(wn.model.can_create(me.df.options)) {
  693. this.$input_area.find(".btn-new").on("click", function() {
  694. wn._from_link = me; wn._from_link_scrollY = scrollY;
  695. new_doc(me.df.options);
  696. });
  697. } else {
  698. this.$input_area.find(".btn-new").remove();
  699. }
  700. if(this.only_input) this.$input_area.find(".btn-open, .btn-new").remove();
  701. },
  702. setup_autocomplete: function() {
  703. var me = this;
  704. this.$input.on("blur", function() {
  705. if(me.selected) {
  706. me.selected = false;
  707. return;
  708. }
  709. if(me.doctype && me.docname) {
  710. var value = me.get_value();
  711. if(value!==me.last_value) {
  712. me.parse_validate_and_set_in_model(value);
  713. }
  714. }});
  715. this.$input.autocomplete({
  716. source: function(request, response) {
  717. var args = {
  718. 'txt': request.term,
  719. 'doctype': me.df.options,
  720. };
  721. me.set_custom_query(args);
  722. return wn.call({
  723. type: "GET",
  724. method:'webnotes.widgets.search.search_link',
  725. no_spinner: true,
  726. args: args,
  727. callback: function(r) {
  728. response(r.results);
  729. },
  730. });
  731. },
  732. open: function(event, ui) {
  733. me.autocomplete_open = true;
  734. },
  735. close: function(event, ui) {
  736. me.autocomplete_open = false;
  737. },
  738. select: function(event, ui) {
  739. me.autocomplete_open = false;
  740. if(me.frm && me.frm.doc) {
  741. me.selected = true;
  742. me.parse_validate_and_set_in_model(ui.item.value);
  743. } else {
  744. me.$input.val(ui.item.value);
  745. me.$input.trigger("change");
  746. }
  747. }
  748. }).data('uiAutocomplete')._renderItem = function(ul, d) {
  749. var html = "";
  750. if(keys(d).length > 1) {
  751. d.info = $.map(d, function(val, key) { return ["value", "label"].indexOf(key)!==-1 ? null : val }).join(", ") || "";
  752. html = repl("<a>%(value)s<br><span class='text-muted'>%(info)s</span></a>", d);
  753. } else {
  754. html = "<a>" + d.value + "</a>";
  755. }
  756. return $('<li></li>')
  757. .data('item.autocomplete', d)
  758. .append(html)
  759. .appendTo(ul);
  760. };
  761. // remove accessibility span (for now)
  762. this.$wrapper.find(".ui-helper-hidden-accessible").remove();
  763. },
  764. set_custom_query: function(args) {
  765. var set_nulls = function(obj) {
  766. $.each(obj, function(key, value) {
  767. if(value!==undefined) {
  768. obj[key] = value || null;
  769. }
  770. });
  771. return obj;
  772. }
  773. if(this.get_query || this.df.get_query) {
  774. var get_query = this.get_query || this.df.get_query;
  775. if($.isPlainObject(get_query)) {
  776. $.extend(args, set_nulls(get_query));
  777. } else if(typeof(get_query)==="string") {
  778. args.query = get_query;
  779. } else {
  780. var q = (get_query)(this.frm && this.frm.doc, this.doctype, this.docname);
  781. if (typeof(q)==="string") {
  782. args.query = q;
  783. } else if($.isPlainObject(q)) {
  784. if(q.filters) {
  785. set_nulls(q.filters);
  786. }
  787. $.extend(args, q);
  788. }
  789. }
  790. }
  791. },
  792. validate: function(value, callback) {
  793. // validate the value just entered
  794. var me = this;
  795. if(this.df.options=="[Select]") {
  796. callback(value);
  797. return;
  798. }
  799. this.frm.script_manager.validate_link_and_fetch(this.df, this.docname, value, callback);
  800. },
  801. });
  802. wn.ui.form.ControlCode = wn.ui.form.ControlText.extend({
  803. make_input: function() {
  804. this._super();
  805. $(this.input_area).find("textarea").css({"height":"400px", "font-family": "Monaco, \"Courier New\", monospace"});
  806. }
  807. });
  808. wn.ui.form.ControlTextEditor = wn.ui.form.ControlCode.extend({
  809. editor_name: "bsEditor",
  810. horizontal: false,
  811. make_input: function() {
  812. $(this.input_area).css({"min-height":"360px"});
  813. var me = this;
  814. this.editor = new (wn.provide(this.editor_name))({
  815. parent: this.input_area,
  816. change: function(value) {
  817. me.parse_validate_and_set_in_model(value);
  818. },
  819. field: this
  820. });
  821. this.has_input = true;
  822. this.editor.editor.keypress("ctrl+s meta+s", function() {
  823. me.frm.save_or_update();
  824. });
  825. },
  826. get_value: function() {
  827. return this.editor.get_value();
  828. },
  829. set_input: function(value) {
  830. this.editor.set_input(value);
  831. this.last_value = value;
  832. }
  833. });
  834. wn.ui.form.ControlTable = wn.ui.form.Control.extend({
  835. make: function() {
  836. this._super();
  837. // add title if prev field is not column / section heading or html
  838. var prev_fieldtype = wn.model.get("DocField",
  839. {parent: this.frm.doctype, idx: this.df.idx-1})[0].fieldtype;
  840. if(["Column Break", "Section Break", "HTML"].indexOf(prev_fieldtype)===-1) {
  841. $("<label>" + this.df.label + "<label>").appendTo(this.wrapper);
  842. }
  843. this.grid = new wn.ui.form.Grid({
  844. frm: this.frm,
  845. df: this.df,
  846. perm: this.perm || this.frm.perm,
  847. parent: this.wrapper
  848. })
  849. if(this.frm)
  850. this.frm.grids[this.frm.grids.length] = this;
  851. // description
  852. if(this.df.description) {
  853. $('<p class="text-muted small">' + wn._(this.df.description) + '</p>')
  854. .appendTo(this.wrapper);
  855. }
  856. var me = this;
  857. this.$wrapper.on("refresh", function() {
  858. me.grid.refresh();
  859. return false;
  860. });
  861. }
  862. })
  863. wn.ui.form.fieldtype_icons = {
  864. "Date": "icon-calendar",
  865. "Time": "icon-time",
  866. "Datetime": "icon-time",
  867. "Code": "icon-code",
  868. "Select": "icon-flag"
  869. };