Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

946 řádky
25 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  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", 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"})
  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 col-xs-'+(this.horizontal?"3":"12")+'" style="padding-right: 0px"></label>\
  138. <div class="col-xs-'+(this.horizontal?"9":"12")+'">\
  139. <div class="control-input"></div>\
  140. <div class="control-value like-disabled-input" style="display: none;"></div>\
  141. <p class="help-box small text-muted"></p>\
  142. </div>\
  143. </div>\
  144. </div>').appendTo(this.parent);
  145. if(!this.horizontal) {
  146. this.$wrapper.removeClass("form-horizontal");
  147. }
  148. }
  149. },
  150. set_input_areas: function() {
  151. if(this.only_input) {
  152. this.input_area = this.wrapper;
  153. } else {
  154. this.label_area = this.label_span = this.$wrapper.find("label").get(0);
  155. this.input_area = this.$wrapper.find(".control-input").get(0);
  156. this.disp_area = this.$wrapper.find(".control-value").get(0);
  157. }
  158. },
  159. set_max_width: function() {
  160. if(this.horizontal) {
  161. this.$wrapper.css({"max-width": "600px"});
  162. }
  163. },
  164. // update input value, label, description
  165. // display (show/hide/read-only),
  166. // mandatory style on refresh
  167. setup_update_on_refresh: function() {
  168. var me = this;
  169. this.$wrapper.on("refresh", function() {
  170. if(me.disp_status != "None") {
  171. // refresh value
  172. if(me.doctype && me.docname) {
  173. me.value = wn.model.get_value(me.doctype, me.docname, me.df.fieldname);
  174. }
  175. if(me.disp_status=="Write") {
  176. me.disp_area && $(me.disp_area).toggle(false);
  177. $(me.input_area).toggle(true);
  178. !me.has_input && me.make_input();
  179. if(me.doctype && me.docname)
  180. me.set_input(me.value);
  181. } else {
  182. $(me.input_area).toggle(false);
  183. me.disp_area && $(me.disp_area)
  184. .toggle(true)
  185. .html(
  186. wn.format(me.value, me.df, {no_icon:true}, locals[me.doctype][me.name])
  187. );
  188. }
  189. me.set_description();
  190. me.set_label();
  191. me.set_mandatory(me.value);
  192. }
  193. return false;
  194. });
  195. },
  196. bind_change_event: function() {
  197. var me = this;
  198. this.$input && this.$input.on("change", this.change || function() {
  199. me.doctype && me.docname && me.get_value
  200. && me.parse_validate_and_set_in_model(me.get_value()); } );
  201. },
  202. set_label: function(label) {
  203. if(label) this.df.label = label;
  204. if(this.only_input || this.df.label==this._label)
  205. return;
  206. // var icon = wn.ui.form.fieldtype_icons[this.df.fieldtype];
  207. // if(this.df.fieldtype==="Link") {
  208. // icon = wn.boot.doctype_icons[this.df.options];
  209. // } else if(this.df.link_doctype) {
  210. // icon = wn.boot.doctype_icons[this.df.link_doctype];
  211. // }
  212. var icon = "";
  213. this.label_span.innerHTML = (icon ? '<i class="'+icon+'"></i> ' : "") +
  214. wn._(this.df.label) || "&nbsp;";
  215. this._label = this.df.label;
  216. },
  217. set_description: function() {
  218. if(this.only_input || this.df.description===this._description)
  219. return;
  220. if(this.df.description) {
  221. this.$wrapper.find(".help-box").html(wn._(this.df.description));
  222. } else {
  223. this.set_empty_description();
  224. }
  225. this._description = this.df.description;
  226. },
  227. set_empty_description: function() {
  228. this.$wrapper.find(".help-box").html("");
  229. },
  230. set_mandatory: function(value) {
  231. this.$wrapper.toggleClass("has-error", (this.df.reqd
  232. && (value==null || value==="")) ? true : false);
  233. },
  234. });
  235. wn.ui.form.ControlData = wn.ui.form.ControlInput.extend({
  236. html_element: "input",
  237. input_type: "text",
  238. make_input: function() {
  239. this.$input = $("<"+ this.html_element +">")
  240. .attr("type", this.input_type)
  241. .addClass("col-md-12 input-with-feedback form-control")
  242. .prependTo(this.input_area)
  243. this.set_input_attributes();
  244. this.input = this.$input.get(0);
  245. this.has_input = true;
  246. this.bind_change_event();
  247. },
  248. set_input_attributes: function() {
  249. this.$input
  250. .attr("data-fieldtype", this.df.fieldtype)
  251. .attr("data-fieldname", this.df.fieldname)
  252. .attr("placeholder", this.df.placeholder || "")
  253. if(this.doctype)
  254. this.$input.attr("data-doctype", this.doctype);
  255. if(this.df.input_css)
  256. this.$input.css(this.df.input_css);
  257. },
  258. set_input: function(val) {
  259. this.$input.val(this.format_for_input(val));
  260. this.last_value = val;
  261. this.set_mandatory && this.set_mandatory(val);
  262. },
  263. get_value: function() {
  264. return this.$input ? this.$input.val() : undefined;
  265. },
  266. format_for_input: function(val) {
  267. return val==null ? "" : val;
  268. },
  269. validate: function(v, callback) {
  270. if(this.df.options == 'Phone') {
  271. if(v+''=='')return '';
  272. v1 = ''
  273. // phone may start with + and must only have numbers later, '-' and ' ' are stripped
  274. v = v.replace(/ /g, '').replace(/-/g, '').replace(/\(/g, '').replace(/\)/g, '');
  275. // allow initial +,0,00
  276. if(v && v.substr(0,1)=='+') {
  277. v1 = '+'; v = v.substr(1);
  278. }
  279. if(v && v.substr(0,2)=='00') {
  280. v1 += '00'; v = v.substr(2);
  281. }
  282. if(v && v.substr(0,1)=='0') {
  283. v1 += '0'; v = v.substr(1);
  284. }
  285. v1 += cint(v) + '';
  286. callback(v1);
  287. } else if(this.df.options == 'Email') {
  288. if(v+''=='')return '';
  289. if(!validate_email(v)) {
  290. msgprint(wn._("Invalid Email") + ": " + v);
  291. callback("");
  292. } else
  293. callback(v);
  294. } else {
  295. callback(v);
  296. }
  297. }
  298. });
  299. wn.ui.form.ControlPassword = wn.ui.form.ControlData.extend({
  300. input_type: "password"
  301. });
  302. wn.ui.form.ControlInt = wn.ui.form.ControlData.extend({
  303. make_input: function() {
  304. var me = this;
  305. this._super();
  306. this.$input
  307. .css({"text-align": "right"})
  308. .on("focus", function() {
  309. setTimeout(function() {
  310. if(!document.activeElement) return;
  311. me.validate(document.activeElement.value, function(val) {
  312. document.activeElement.value = val;
  313. });
  314. document.activeElement.select()
  315. }, 100);
  316. return false;
  317. })
  318. },
  319. validate: function(value, callback) {
  320. return callback(cint(value, null));
  321. }
  322. });
  323. wn.ui.form.ControlFloat = wn.ui.form.ControlInt.extend({
  324. validate: function(value, callback) {
  325. return callback(isNaN(parseFloat(value)) ? null : flt(value));
  326. },
  327. format_for_input: function(value) {
  328. var formatted_value = format_number(parseFloat(value),
  329. null, cint(wn.boot.sysdefaults.float_precision, null));
  330. return isNaN(parseFloat(value)) ? "" : formatted_value;
  331. }
  332. });
  333. wn.ui.form.ControlCurrency = wn.ui.form.ControlFloat.extend({
  334. format_for_input: function(value) {
  335. var formatted_value = format_number(parseFloat(value),
  336. get_number_format(this.get_currency()));
  337. return isNaN(parseFloat(value)) ? "" : formatted_value;
  338. },
  339. get_currency: function() {
  340. return wn.meta.get_field_currency(this.df, this.get_doc());
  341. }
  342. });
  343. wn.ui.form.ControlPercent = wn.ui.form.ControlFloat;
  344. wn.ui.form.ControlDate = wn.ui.form.ControlData.extend({
  345. datepicker_options: {
  346. altFormat:'yy-mm-dd',
  347. changeYear: true,
  348. yearRange: "-70Y:+10Y",
  349. },
  350. make_input: function() {
  351. this._super();
  352. this.set_datepicker();
  353. },
  354. set_datepicker: function() {
  355. this.datepicker_options.dateFormat =
  356. (wn.boot.sysdefaults.date_format || 'yyyy-mm-dd').replace("yyyy", "yy")
  357. if(this.not_in_form && this.dialog_wrapper) {
  358. this.$input.css("z-index", cint($(this.dialog_wrapper).zIndex()));
  359. }
  360. this.$input.datepicker(this.datepicker_options);
  361. },
  362. parse: function(value) {
  363. return value ? dateutil.user_to_str(value) : value;
  364. },
  365. format_for_input: function(value) {
  366. return value ? dateutil.str_to_user(value) : "";
  367. },
  368. validate: function(value, callback) {
  369. var value = wn.datetime.validate(value);
  370. if(!value) {
  371. msgprint (wn._("Date must be in format") + ": " + (sys_defaults.date_format || "yyyy-mm-dd"));
  372. callback("");
  373. }
  374. return callback(value);
  375. }
  376. })
  377. import_timepicker = function() {
  378. wn.require("lib/js/lib/jquery/jquery.ui.slider.min.js");
  379. wn.require("lib/js/lib/jquery/jquery.ui.sliderAccess.js");
  380. wn.require("lib/js/lib/jquery/jquery.ui.timepicker-addon.css");
  381. wn.require("lib/js/lib/jquery/jquery.ui.timepicker-addon.js");
  382. }
  383. wn.ui.form.ControlTime = wn.ui.form.ControlData.extend({
  384. make_input: function() {
  385. import_timepicker();
  386. this._super();
  387. this.$input.timepicker({
  388. timeFormat: 'hh:mm:ss',
  389. });
  390. }
  391. });
  392. wn.ui.form.ControlDatetime = wn.ui.form.ControlDate.extend({
  393. set_datepicker: function() {
  394. this.datepicker_options.dateFormat =
  395. (wn.boot.sysdefaults.date_format || 'yy-mm-dd').replace('yyyy','yy');
  396. this.datepicker_options.timeFormat = "hh:mm:ss";
  397. if(this.not_in_form && this.dialog_wrapper) {
  398. this.$input.css("z-index", cint($(this.dialog_wrapper).zIndex()));
  399. }
  400. this.$input.datetimepicker(this.datepicker_options);
  401. },
  402. make_input: function() {
  403. import_timepicker();
  404. this._super();
  405. },
  406. });
  407. wn.ui.form.ControlText = wn.ui.form.ControlData.extend({
  408. html_element: "textarea",
  409. horizontal: false
  410. });
  411. wn.ui.form.ControlLongText = wn.ui.form.ControlText;
  412. wn.ui.form.ControlSmallText = wn.ui.form.ControlText;
  413. wn.ui.form.ControlCheck = wn.ui.form.ControlData.extend({
  414. input_type: "checkbox",
  415. make_wrapper: function() {
  416. this.$wrapper = $('<div class="form-group row">\
  417. <div class="col-md-offset-3 col-md-9">\
  418. <div class="checkbox">\
  419. <label class="input-area">\
  420. <span class="disp-area" style="display:none;"></span>\
  421. <span class="label-area"></span>\
  422. </label>\
  423. <p class="help-box small text-muted">&nbsp;</p>\
  424. </div>\
  425. </div>\
  426. </div>').appendTo(this.parent)
  427. },
  428. set_input_areas: function() {
  429. this.label_area = this.label_span = this.$wrapper.find(".label-area").get(0);
  430. this.input_area = this.$wrapper.find(".input-area").get(0);
  431. this.disp_area = this.$wrapper.find(".disp-area").get(0);
  432. },
  433. make_input: function() {
  434. this._super();
  435. this.$input.removeClass("col-md-12 form-control");
  436. },
  437. parse: function(value) {
  438. return this.input.checked ? 1 : 0;
  439. },
  440. validate: function(value, callback) {
  441. return callback(cint(value));
  442. },
  443. set_input: function(value) {
  444. this.input.checked = value ? 1 : 0;
  445. this.last_value = value;
  446. }
  447. });
  448. wn.ui.form.ControlButton = wn.ui.form.ControlData.extend({
  449. make_input: function() {
  450. var me = this;
  451. this.$input = $('<button class="btn btn-default">')
  452. .prependTo(me.input_area)
  453. .on("click", function() {
  454. me.onclick();
  455. });
  456. this.input = this.$input.get(0);
  457. this.has_input = true;
  458. },
  459. onclick: function() {
  460. if(this.frm && this.frm.doc && this.frm.cscript) {
  461. if(this.frm.cscript[this.df.fieldname]) {
  462. this.frm.script_manager.trigger(this.df.fieldname, this.doctype, this.docname);
  463. } else {
  464. this.frm.runscript(this.df.options, me);
  465. }
  466. }
  467. else if(this.df.click) {
  468. this.df.click();
  469. }
  470. },
  471. set_input_areas: function() {
  472. this._super();
  473. $(this.disp_area).removeClass();
  474. },
  475. set_empty_description: function() {
  476. this.$wrapper.find(".help-box").empty().toggle(false);
  477. },
  478. set_label: function() {
  479. $(this.label_span).html("&nbsp;");
  480. this.$input && this.$input.html(this.df.label);
  481. }
  482. });
  483. wn.ui.form.ControlAttach = wn.ui.form.ControlButton.extend({
  484. onclick: function() {
  485. if(!this.dialog) {
  486. this.dialog = new wn.ui.Dialog({
  487. title: wn._(this.df.label || "Upload Attachment"),
  488. });
  489. }
  490. $(this.dialog.body).empty();
  491. this.set_upload_options();
  492. wn.upload.make(this.upload_options);
  493. this.dialog.show();
  494. },
  495. set_upload_options: function() {
  496. var me = this;
  497. this.upload_options = {
  498. parent: this.dialog.body,
  499. args: {},
  500. max_width: this.df.max_width,
  501. max_height: this.df.max_height,
  502. callback: function() {
  503. me.dialog.hide();
  504. me.on_upload_complete(fileid, filename, r);
  505. me.show_ok_on_button();
  506. },
  507. onerror: function() {
  508. me.dialog.hide();
  509. },
  510. }
  511. if(this.frm) {
  512. this.upload_options.args = {
  513. from_form: 1,
  514. doctype: this.frm.doctype,
  515. docname: this.frm.docname,
  516. }
  517. } else {
  518. this.upload_options.on_attach = function(fileobj, dataurl) {
  519. me.dialog.hide();
  520. me.fileobj = fileobj;
  521. me.dataurl = dataurl;
  522. if(me.on_attach) {
  523. me.on_attach()
  524. }
  525. if(me.df.on_attach) {
  526. me.df.on_attach(fileobj, dataurl);
  527. }
  528. me.show_ok_on_button();
  529. }
  530. }
  531. },
  532. get_value: function() {
  533. return this.fileobj ? (this.fileobj.filename + "," + this.dataurl) : null;
  534. },
  535. on_upload_complete: function(fileid, filename, r) {
  536. this.frm && this.frm.attachments.update_attachment(fileid, filename, this.df.fieldname, r);
  537. },
  538. show_ok_on_button: function() {
  539. if(!$(this.input).find(".icon-ok").length) {
  540. $(this.input).html('<i class="icon-ok"></i> ' + this.df.label);
  541. }
  542. }
  543. });
  544. wn.ui.form.ControlAttachImage = wn.ui.form.ControlAttach.extend({
  545. make_input: function() {
  546. this._super();
  547. this.img = $("<img class='img-responsive'>").appendTo($('<div style="margin: 7px 0px;">\
  548. <div class="missing-image"><i class="icon-camera"></i></div></div>').prependTo(this.input_area)).toggle(false);
  549. },
  550. on_attach: function() {
  551. $(this.input_area).find(".missing-image").toggle(false);
  552. this.img.attr("src", this.dataurl).toggle(true);
  553. }
  554. });
  555. wn.ui.form.ControlSelect = wn.ui.form.ControlData.extend({
  556. html_element: "select",
  557. make_input: function() {
  558. var me = this;
  559. this._super();
  560. if(this.df.options=="attach_files:") {
  561. this.setup_attachment();
  562. }
  563. this.set_options();
  564. },
  565. set_input: function(value) {
  566. // refresh options first - (new ones??)
  567. this.set_options();
  568. this._super(value);
  569. // not a possible option, repair
  570. if(this.doctype && this.docname) {
  571. // model value is not an option,
  572. // set the default option (displayed)
  573. var input_value = this.$input.val();
  574. var model_value = wn.model.get_value(this.doctype, this.docname, this.df.fieldname);
  575. if(input_value != (model_value || "")) {
  576. this.set_model_value(input_value);
  577. } else {
  578. this.last_value = value;
  579. }
  580. }
  581. },
  582. setup_attachment: function() {
  583. var me = this;
  584. $(this.input).css({"width": "85%", "display": "inline-block"});
  585. this.$attach = $("<button class='btn btn-default' title='"+ wn._("Add attachment") + "'\
  586. style='padding-left: 6px; padding-right: 6px; margin-right: 6px;'>\
  587. <i class='icon-plus'></i></button>")
  588. .click(function() {
  589. me.frm.attachments.new_attachment(me.df.fieldname);
  590. })
  591. .prependTo(this.input_area);
  592. $(document).on("upload_complete", function(event, filename, file_url) {
  593. if(cur_frm === me.frm) {
  594. me.set_options();
  595. }
  596. })
  597. this.$wrapper.on("refresh", function() {
  598. me.$attach.toggle(!me.frm.doc.__islocal);
  599. });
  600. },
  601. set_options: function() {
  602. var options = this.df.options || [];
  603. if(this.df.options=="attach_files:") {
  604. options = this.get_file_attachment_list();
  605. } else if(typeof this.df.options==="string") {
  606. options = this.df.options.split("\n");
  607. }
  608. if(this.in_filter && options[0] != "") {
  609. options = add_lists([''], options);
  610. }
  611. var selected = this.$input.find(":selected").val();
  612. this.$input.empty().add_options(options || []);
  613. if(selected) this.$input.val(selected);
  614. },
  615. get_file_attachment_list: function() {
  616. if(!this.frm) return;
  617. var fl = wn.model.docinfo[this.frm.doctype][this.frm.docname];
  618. if(fl && fl.attachments) {
  619. fl = fl.attachments;
  620. this.set_description("");
  621. var options = [""];
  622. for(var fname in fl) {
  623. if(fname.indexOf("/")===-1)
  624. fname = "files/" + fname;
  625. options.push(fname);
  626. }
  627. return options;
  628. } else {
  629. this.set_description(wn._("Please attach a file first."))
  630. return [""];
  631. }
  632. }
  633. });
  634. // special features for link
  635. // buttons
  636. // autocomplete
  637. // link validation
  638. // custom queries
  639. // add_fetches
  640. wn.ui.form.ControlLink = wn.ui.form.ControlData.extend({
  641. make_input: function() {
  642. var me = this;
  643. $('<div class="link-field">\
  644. <input type="text" class="input-with-feedback form-control" style="width: 80%; display: inline-block">\
  645. <span class="link-field-btn">\
  646. <a class="btn-search" title="Search Link">\
  647. <i class="icon-search"></i>\
  648. </a><a class="btn-open" title="Open Link">\
  649. <i class="icon-play"></i>\
  650. </a><a class="btn-new" title="Make New">\
  651. <i class="icon-plus"></i>\
  652. </a>\
  653. </span>\
  654. </div>').prependTo(this.input_area);
  655. this.$input_area = $(this.input_area);
  656. this.$input = this.$input_area.find('input');
  657. this.set_input_attributes();
  658. this.$input.on("focus", function() {
  659. setTimeout(function() {
  660. if(!me.$input.val()) {
  661. me.$input.val("%").trigger("keydown");
  662. }
  663. }, 1000)
  664. })
  665. this.input = this.$input.get(0);
  666. this.has_input = true;
  667. //this.bind_change_event();
  668. var me = this;
  669. this.setup_buttons();
  670. //this.setup_typeahead();
  671. this.setup_autocomplete();
  672. },
  673. setup_buttons: function() {
  674. var me = this;
  675. // magnifier - search
  676. this.$input_area.find(".btn-search").on("click", function() {
  677. new wn.ui.form.LinkSelector({
  678. doctype: me.df.options,
  679. target: me,
  680. txt: me.get_value()
  681. });
  682. });
  683. // open
  684. if(wn.model.can_read(me.df.options)) {
  685. this.$input_area.find(".btn-open").on("click", function() {
  686. var value = me.get_value();
  687. if(value && me.df.options) wn.set_route("Form", me.df.options, value);
  688. });
  689. } else {
  690. this.$input_area.find(".btn-open").remove();
  691. }
  692. // new
  693. if(wn.model.can_create(me.df.options)) {
  694. this.$input_area.find(".btn-new").on("click", function() {
  695. wn._from_link = me; wn._from_link_scrollY = scrollY;
  696. new_doc(me.df.options);
  697. });
  698. } else {
  699. this.$input_area.find(".btn-new").remove();
  700. }
  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.ControlInput.extend({
  803. editor_name: "wn.editors.ACE",
  804. horizontal: false,
  805. make_input: function() {
  806. $(this.input_area).css({"min-height":"360px"});
  807. var me = this;
  808. this.editor = new (wn.provide(this.editor_name))({
  809. parent: this.input_area,
  810. change: function(value) {
  811. me.parse_validate_and_set_in_model(value);
  812. },
  813. field: this
  814. });
  815. this.has_input = true;
  816. },
  817. get_value: function() {
  818. return this.editor.get_value();
  819. },
  820. set_input: function(value) {
  821. this.editor.set_input(value);
  822. this.last_value = value;
  823. }
  824. });
  825. wn.ui.form.ControlTextEditor = wn.ui.form.ControlCode.extend({
  826. editor_name: "bsEditor",
  827. make_input: function() {
  828. this._super();
  829. this.editor.editor.keypress("ctrl+s meta+s", function() {
  830. me.frm.save_or_update();
  831. });
  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">' + 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. };