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.

13 年之前
13 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. search_fields = {};
  23. // Search Selector 2.0
  24. // -------------------
  25. function setlinkvalue(name) {
  26. //selector.input.set(name);// in local - this will be set onchange
  27. selector.input.set_input_value(name); // on screen
  28. selector.hide();
  29. }
  30. // Link Selector
  31. // -------------
  32. function makeselector() {
  33. var d = new Dialog(540,440, 'Search');
  34. d.make_body([
  35. ['HTML', 'Help'],
  36. ['Data', 'Beginning With', 'Tip: You can use wildcard "%"'],
  37. ['Select', 'Search By'],
  38. ['Button', 'Search'],
  39. ['HTML', 'Result']
  40. ]);
  41. // search with
  42. var inp = d.widgets['Beginning With'];
  43. var field_sel = d.widgets['Search By'];
  44. var btn = d.widgets['Search'];
  45. // result
  46. d.sel_type = '';
  47. d.values_len = 0;
  48. d.set = function(input, type, label) {
  49. d.sel_type = type;
  50. d.input = input;
  51. if(d.style!='Link') {
  52. d.rows['Result'].innerHTML ='';
  53. d.values_len = 0;
  54. }
  55. d.style = 'Link';
  56. d.set_query_description()
  57. if(!d.sel_type)d.sel_type = 'Value';
  58. d.set_title("Select");
  59. d.set_query_description('Select a "'+ d.sel_type +'" for field "'+label+'"');
  60. }
  61. d.set_search = function(dt) {
  62. if(d.style!='Search') {
  63. d.rows['Result'].innerHTML ='';
  64. d.values_len = 0;
  65. }
  66. d.style = 'Search';
  67. if(d.input) { d.input = null; sel_type = null; }
  68. d.sel_type = dt;
  69. d.set_title('Quick Search for ' + dt);
  70. }
  71. $(inp).keydown(function(e) {
  72. if(e.which==13) {
  73. if(!btn.disabled)btn.onclick();
  74. }
  75. })
  76. d.set_query_description = function(txt) {
  77. txt = d.input && d.input.query_description || txt;
  78. if(txt) {
  79. d.rows['Help'].innerHTML ='<div class="alert" style="margin-top:0px">' + txt + '</div>';
  80. } else {
  81. d.rows['Help'].innerHTML =''
  82. }
  83. }
  84. d.onshow = function() {
  85. if(d.set_doctype!=d.sel_type) {
  86. d.rows['Result'].innerHTML ='';
  87. d.values_len = 0;
  88. }
  89. inp.value = '';
  90. if(d.input && d.input.txt.value) {
  91. inp.value = d.input.txt.value;
  92. }
  93. try{inp.focus();} catch(e){}
  94. if(d.input) d.input.set_get_query();
  95. // temp function to strip labels from search fields
  96. var get_sf_list = function(dt) {
  97. var l = []; var lf = search_fields[dt];
  98. for(var i=0; i<lf.length; i++) l.push(lf[i][1]);
  99. return l;
  100. }
  101. // set fields
  102. $ds(d.rows['Search By']);
  103. if(search_fields[d.sel_type]) {
  104. empty_select(field_sel);
  105. add_sel_options(field_sel, get_sf_list(d.sel_type), 'ID');
  106. } else {
  107. // set default select by
  108. empty_select(field_sel);
  109. add_sel_options(field_sel, ['ID'], 'ID');
  110. $c('webnotes.widgets.search.getsearchfields', {'doctype':d.sel_type}, function(r,rt) {
  111. search_fields[d.sel_type] = r.searchfields;
  112. empty_select(field_sel);
  113. add_sel_options(field_sel, get_sf_list(d.sel_type));
  114. field_sel.selectedIndex = 0;
  115. } );
  116. }
  117. }
  118. d.onhide = function() {
  119. //if(d.input && d.input.txt) // link, call onchange
  120. // d.input.txt.set_input_value()
  121. }
  122. btn.onclick = function() {
  123. if(this.disabled) return;
  124. this.args.is_ajax = true;
  125. d.set_doctype = d.sel_type;
  126. var q = '';
  127. args = {};
  128. if(d.input && d.input.get_query) {
  129. var doc = {};
  130. args.is_simple = 1;
  131. if(cur_frm) doc = locals[cur_frm.doctype][cur_frm.docname];
  132. var q = d.input.get_query(doc, d.input.doctype, d.input.docname);
  133. if(!q) { return ''; }
  134. }
  135. // for field type, return field name
  136. var get_sf_fieldname = function(v) {
  137. var lf = search_fields[d.sel_type];
  138. // still loading options
  139. if(!lf)
  140. return 'name'
  141. for(var i=0; i<lf.length; i++) if(lf[i][1]==v) return lf[i][0];
  142. }
  143. // build args
  144. $.extend(args, {
  145. 'txt':strip(inp.value)
  146. ,'doctype':d.sel_type
  147. ,'query':q
  148. ,'searchfield':get_sf_fieldname(sel_val(field_sel))
  149. });
  150. // run the query
  151. this.set_working();
  152. $c('webnotes.widgets.search.search_widget',
  153. args,
  154. function(r, rtxt) {
  155. btn.done_working();
  156. if(r.coltypes)r.coltypes[0]='Link'; // first column must always be selectable even if it is not a link
  157. d.values_len = r.values.length;
  158. d.set_result(r);
  159. }, function() { btn.done_working(); });
  160. }
  161. d.set_result = function(r) {
  162. d.rows['Result'].innerHTML = '';
  163. var c = $a(d.rows['Result'],'div','comment',{paddingBottom:'4px',marginBottom:'4px',borderBottom:'1px solid #CCC', marginLeft:'4px'});
  164. if(r.values.length==50)
  165. c.innerHTML = 'Showing max 50 results. Use filters to narrow down your search';
  166. else
  167. c.innerHTML = 'Showing '+r.values.length+' resuts.';
  168. var w = $a(d.rows['Result'],'div','',{height:'240px',overflow:'auto',margin:'4px'});
  169. for(var i=0; i<r.values.length; i++) {
  170. var div = $a(w,'div','',
  171. {marginBottom:'4px',paddingBottom:'4px',borderBottom:'1px dashed #CCC'});
  172. // link
  173. var l = $a($a(div,'div'),'span','link_type');
  174. l.innerHTML = r.values[i][0];
  175. l.link_name = r.values[i][0];
  176. l.dt = r.coloptions[0];
  177. if(d.input)
  178. l.onclick = function() { setlinkvalue(this.link_name); }
  179. else
  180. l.onclick = function() { loaddoc(this.dt, this.link_name); d.hide(); }
  181. // support
  182. var cl = []
  183. for(var j=1; j<r.values[i].length; j++) cl.push(r.values[i][j]);
  184. var c = $a(div,'div','comment',{marginTop:'2px'}); c.innerHTML = cl.join(', ');
  185. }
  186. }
  187. selector = d;
  188. }