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.
 
 
 
 
 
 

98 lines
2.5 KiB

  1. wn.ui.form.LinkSelector = Class.extend({
  2. _help: "Dialog box to select a Link Value",
  3. init: function(opts) {
  4. /* help: Options: doctype, get_query, target */
  5. $.extend(this, opts);
  6. var me = this;
  7. if(this.doctype!="[Select]") {
  8. wn.model.with_doctype(this.doctype, function(r) {
  9. me.make();
  10. });
  11. } else {
  12. this.make();
  13. }
  14. },
  15. make: function() {
  16. this.dialog = new wn.ui.Dialog({
  17. "title": "Select " + (this.doctype=='[Select]' ? "Value" : this.doctype),
  18. "fields": [
  19. {
  20. fieldtype: "Data", fieldname: "txt", label: "Beginning with",
  21. description: "You can use wildcard %"
  22. },
  23. {
  24. fieldtype: "Select", fieldname: "search_field", label: "Search With"
  25. },
  26. {
  27. fieldtype: "Button", fieldname: "search", label: "Search",
  28. },
  29. {
  30. fieldtype: "HTML", fieldname: "results"
  31. }
  32. ]
  33. });
  34. var search_fields = wn.model.get_value("DocType", this.doctype, "search_fields"),
  35. me = this;
  36. // add search fields
  37. if(this.doctype!="[Select]" && search_fields) {
  38. var search_fields = search_fields.split(",");
  39. this.dialog.fields_dict.search_field.$input.add_options(
  40. [{value:"name", label:"ID"}].concat(
  41. $.map(search_fields, function(fieldname) {
  42. fieldname = strip(fieldname);
  43. var df = wn.meta.docfield_map[me.doctype][fieldname];
  44. return {
  45. value: fieldname,
  46. label: df ? df.label : fieldname
  47. }
  48. })));
  49. } else {
  50. this.dialog.fields_dict.search_field.$wrapper.toggle(false);
  51. }
  52. this.dialog.fields_dict.search.$input.on("click", function() {
  53. me.search(this);
  54. });
  55. this.dialog.show();
  56. },
  57. search: function(btn) {
  58. var args = {
  59. txt: this.dialog.fields_dict.txt.get_value(),
  60. doctype: this.doctype,
  61. searchfield: this.dialog.fields_dict.search_field.get_value()
  62. },
  63. me = this;
  64. this.target.set_custom_query(args);
  65. wn.call({
  66. method: "webnotes.widgets.search.search_widget",
  67. type: "GET",
  68. args: args,
  69. callback: function(r) {
  70. var parent = me.dialog.fields_dict.results.$wrapper;
  71. parent.empty();
  72. $.each(r.values, function(i, v) {
  73. var row = $(repl('<p><a href="#" data-value="%(name)s">%(name)s</a> \
  74. <span class="text-muted">%(values)s</span></p>', {
  75. name: v[0],
  76. values: v.splice(1).join(", ")
  77. })).appendTo(parent);
  78. row.find("a").click(function() {
  79. var value = $(this).attr("data-value");
  80. if(me.target.doctype)
  81. me.target.set_model_value(value);
  82. else
  83. me.target.set_input(value);
  84. me.dialog.hide();
  85. return false;
  86. })
  87. })
  88. },
  89. btn: btn
  90. });
  91. }
  92. })