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.

listing.js 10 KiB

13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
12 vuotta sitten
13 vuotta sitten
12 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
12 vuotta sitten
12 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
13 vuotta sitten
12 vuotta sitten
13 vuotta sitten
13 vuotta sitten
12 vuotta sitten
13 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. // new re-factored Listing object
  23. // removed rarely used functionality
  24. //
  25. // opts:
  26. // parent
  27. // method (method to call on server)
  28. // args (additional args to method)
  29. // get_args (method to return args as dict)
  30. // show_filters [false]
  31. // doctype
  32. // filter_fields (if given, this list is rendered, else built from doctype)
  33. // query or get_query (will be deprecated)
  34. // query_max
  35. // buttons_in_frame
  36. // no_result_message ("No result")
  37. // page_length (20)
  38. // hide_refresh (False)
  39. // no_toolbar
  40. // new_doctype
  41. // [function] render_row(parent, data)
  42. // [function] onrun
  43. // no_loading (no ajax indicator)
  44. wn.provide('wn.ui');
  45. wn.ui.Listing = Class.extend({
  46. init: function(opts) {
  47. this.opts = opts || {};
  48. this.page_length = 20;
  49. this.start = 0;
  50. this.data = [];
  51. if(opts) {
  52. this.make();
  53. }
  54. },
  55. prepare_opts: function() {
  56. if(this.opts.new_doctype) {
  57. if(wn.boot.profile.can_create.indexOf(this.opts.new_doctype)==-1) {
  58. this.opts.new_doctype = null;
  59. } else {
  60. this.opts.new_doctype = this.opts.new_doctype;
  61. }
  62. }
  63. if(!this.opts.no_result_message) {
  64. this.opts.no_result_message = wn._('Nothing to show');
  65. }
  66. this.opts._more = wn._("More");
  67. },
  68. make: function(opts) {
  69. if(opts) {
  70. this.opts = opts;
  71. }
  72. this.prepare_opts();
  73. $.extend(this, this.opts);
  74. $(this.parent).html(repl('\
  75. <div class="wnlist">\
  76. <h3 class="title hide">%(title)s</h3>\
  77. \
  78. <div class="list-filters" style="display: none;">\
  79. <div class="show_filters well" style="display: none;">\
  80. <div class="filter_area"></div>\
  81. <div>\
  82. <button class="btn btn-default btn-info search-btn">\
  83. <i class="icon-refresh icon-white"></i> \
  84. <span class="hidden-phone">Search</span></button>\
  85. <button class="btn btn-default add-filter-btn">\
  86. <i class="icon-plus"></i> \
  87. <span class="hidden-phone">Add Filter</span></button>\
  88. </div>\
  89. </div>\
  90. </div>\
  91. \
  92. <div style="margin-bottom:9px" class="list-toolbar-wrapper">\
  93. <div class="list-toolbar btn-group" style="display:inline-block; margin-right: 10px;">\
  94. </div>\
  95. <div style="display: none; width: 24px; margin-left: 4px">\
  96. <img src="lib/images/ui/button-load.gif" \
  97. class="img-load"/></div>\
  98. </div><div style="clear:both"></div>\
  99. \
  100. <div class="no-result" style="display: none;">\
  101. %(no_result_message)s\
  102. </div>\
  103. \
  104. <div class="result">\
  105. <div class="result-list"></div>\
  106. </div>\
  107. \
  108. <p class="paging-button" style="text-align: center;">\
  109. <button class="btn btn-default btn-more" style="display: none; margin: 15px 0px;">%(_more)s...</div>\
  110. </p>\
  111. </div>\
  112. ', this.opts));
  113. this.$w = $(this.parent).find('.wnlist');
  114. this.set_events();
  115. if(this.appframe) {
  116. this.$w.find('.list-toolbar-wrapper').toggle(false);
  117. }
  118. if(this.show_filters) {
  119. this.make_filters();
  120. }
  121. },
  122. add_button: function(label, click, icon) {
  123. if(this.appframe) {
  124. return this.appframe.add_button(label, click, icon)
  125. } else {
  126. $button = $('<button class="btn btn-default"></button>')
  127. .appendTo(this.$w.find('.list-toolbar'))
  128. .html((icon ? ("<i class='"+icon+"'></i> ") : "") + label)
  129. .click(click);
  130. return $button
  131. }
  132. },
  133. show_view: function($btn, $div, $btn_unsel, $div_unsel) {
  134. $btn_unsel.removeClass('btn-info');
  135. $btn_unsel.find('i').removeClass('icon-white');
  136. $div_unsel.toggle(false);
  137. $btn.addClass('btn-info');
  138. $btn.find('i').addClass('icon-white');
  139. $div.toggle(true);
  140. },
  141. set_events: function() {
  142. var me = this;
  143. // next page
  144. this.$w.find('.btn-more').click(function() {
  145. me.run({append: true });
  146. });
  147. // title
  148. if(this.title) {
  149. this.$w.find('h3').html(this.title).toggle(true);
  150. }
  151. // hide-refresh
  152. if(!(this.hide_refresh || this.no_refresh)) {
  153. this.add_button('', function() {
  154. me.run();
  155. }, 'icon-refresh');
  156. }
  157. // new
  158. if(this.new_doctype) {
  159. this.add_button(wn._('New') + ' ' + wn._(this.new_doctype), function() {
  160. (me.custom_new_doc || me.make_new_doc).apply(me, [me.new_doctype]);
  161. }, 'icon-plus');
  162. }
  163. // hide-filter
  164. if(me.show_filters) {
  165. this.add_button(wn._('Filter'), function() {
  166. me.filter_list.show_filters();
  167. }, 'icon-search').addClass('btn-filter');
  168. }
  169. if(me.no_toolbar || me.hide_toolbar) {
  170. me.$w.find('.list-toolbar-wrapper').toggle(false);
  171. }
  172. },
  173. make_new_doc: function(doctype) {
  174. var me = this;
  175. wn.model.with_doctype(doctype, function() {
  176. var doc = wn.model.get_new_doc(doctype);
  177. if(me.filter_list) {
  178. $.each(me.filter_list.get_filters(), function(i, f) {
  179. if(f[0]===doctype && f[2]==="=")
  180. doc[f[1]]=f[3];
  181. })
  182. }
  183. wn.set_route("Form", doctype, doc.name);
  184. });
  185. },
  186. make_filters: function() {
  187. this.filter_list = new wn.ui.FilterList({
  188. listobj: this,
  189. $parent: this.$w.find('.list-filters').toggle(true),
  190. doctype: this.doctype,
  191. filter_fields: this.filter_fields
  192. });
  193. },
  194. clear: function() {
  195. this.data = [];
  196. this.$w.find('.result-list').empty();
  197. this.$w.find('.result').toggle(true);
  198. this.$w.find('.no-result').toggle(false);
  199. this.start = 0;
  200. },
  201. run: function(more) {
  202. var me = this;
  203. if(!more) {
  204. this.start = 0;
  205. if(this.onreset) this.onreset();
  206. }
  207. if(!me.opts.no_loading)
  208. me.set_working(true);
  209. wn.call({
  210. method: this.opts.method || 'webnotes.widgets.query_builder.runquery',
  211. type: "GET",
  212. args: this.get_call_args(),
  213. callback: function(r) {
  214. if(!me.opts.no_loading)
  215. me.set_working(false);
  216. me.dirty = false;
  217. me.render_results(r);
  218. },
  219. no_spinner: this.opts.no_loading
  220. });
  221. },
  222. set_working: function(flag) {
  223. this.$w.find('.img-load').toggle(flag);
  224. },
  225. get_call_args: function() {
  226. // load query
  227. if(!this.method) {
  228. var query = this.get_query ? this.get_query() : this.query;
  229. query = this.add_limits(query);
  230. var args={
  231. query_max: this.query_max,
  232. as_dict: 1
  233. }
  234. args.simple_query = query;
  235. } else {
  236. var args = {
  237. limit_start: this.start,
  238. limit_page_length: this.page_length
  239. }
  240. }
  241. // append user-defined arguments
  242. if(this.args)
  243. $.extend(args, this.args)
  244. if(this.get_args) {
  245. $.extend(args, this.get_args());
  246. }
  247. return args;
  248. },
  249. render_results: function(r) {
  250. if(this.start===0) this.clear();
  251. this.$w.find('.btn-more').toggle(false);
  252. if(r.message) {
  253. r.values = this.get_values_from_response(r.message);
  254. }
  255. if(r.values && r.values.length) {
  256. this.data = this.data.concat(r.values);
  257. this.render_list(r.values);
  258. this.update_paging(r.values);
  259. } else {
  260. if(this.start===0) {
  261. this.$w.find('.result').toggle(false);
  262. var msg = this.get_no_result_message
  263. ? this.get_no_result_message()
  264. : (this.no_result_message
  265. ? this.no_result_message
  266. : wn._("Nothing to show"));
  267. this.$w.find('.no-result')
  268. .html(msg)
  269. .toggle(true);
  270. }
  271. }
  272. // callbacks
  273. if(this.onrun) this.onrun();
  274. if(this.callback) this.callback(r);
  275. },
  276. get_values_from_response: function(data) {
  277. // make dictionaries from keys and values
  278. if(data.keys) {
  279. var values = [];
  280. $.each(data.values, function(row_idx, row) {
  281. var new_row = {};
  282. $.each(data.keys, function(key_idx, key) {
  283. new_row[key] = row[key_idx];
  284. })
  285. values.push(new_row);
  286. });
  287. return values;
  288. } else {
  289. return data;
  290. }
  291. },
  292. render_list: function(values) {
  293. var m = Math.min(values.length, this.page_length);
  294. this.data = values;
  295. if(this.filter_list)
  296. this.filter_values = this.filter_list.get_filters();
  297. // render the rows
  298. for(var i=0; i < m; i++) {
  299. this.render_row(this.add_row(), values[i], this, i);
  300. }
  301. },
  302. update_paging: function(values) {
  303. if(values.length >= this.page_length) {
  304. this.$w.find('.btn-more').toggle(true);
  305. this.start += this.page_length;
  306. }
  307. },
  308. add_row: function() {
  309. return $('<div class="list-row">').appendTo(this.$w.find('.result-list')).get(0);
  310. },
  311. refresh: function() {
  312. this.run();
  313. },
  314. add_limits: function(query) {
  315. query += ' LIMIT ' + this.start + ',' + (this.page_length+1);
  316. return query
  317. },
  318. set_filter: function(fieldname, label, doctype) {
  319. if(!doctype) doctype = this.doctype;
  320. var filter = this.filter_list.get_filter(fieldname);
  321. if(filter) {
  322. var v = filter.field.get_parsed_value();
  323. if(v.indexOf(label)!=-1) {
  324. // already set
  325. return false;
  326. } else {
  327. // second filter set for this field
  328. if(fieldname=='_user_tags') {
  329. // and for tags
  330. this.filter_list.add_filter(doctype, fieldname,
  331. 'like', '%' + label);
  332. } else {
  333. // or for rest using "in"
  334. filter.set_values(doctype, fieldname, 'in', v + ', ' + label);
  335. }
  336. }
  337. } else {
  338. // no filter for this item,
  339. // setup one
  340. if(fieldname==='_user_tags') {
  341. this.filter_list.add_filter(doctype, fieldname,
  342. 'like', '%' + label);
  343. } else {
  344. this.filter_list.add_filter(doctype, fieldname, '=', label);
  345. }
  346. }
  347. }
  348. });