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.
 
 
 
 
 
 

534 lines
15 KiB

  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. wn.provide('wn.views.doclistview');
  23. wn.provide('wn.doclistviews');
  24. wn.views.doclistview.show = function(doctype) {
  25. var page_name = wn.get_route_str();
  26. if(wn.pages[page_name]) {
  27. wn.container.change_to(wn.pages[page_name]);
  28. } else {
  29. var route = wn.get_route();
  30. if(route[1]) {
  31. wn.model.with_doctype(route[1], function(r) {
  32. if(r && r['403']) {
  33. return;
  34. }
  35. new wn.views.DocListView(route[1]);
  36. });
  37. }
  38. }
  39. }
  40. wn.views.DocListView = wn.ui.Listing.extend({
  41. init: function(doctype) {
  42. this.doctype = doctype;
  43. this.label = get_doctype_label(doctype);
  44. this.label = (this.label.toLowerCase().substr(-4) == 'list') ?
  45. this.label : (this.label + ' List');
  46. this.make_page();
  47. this.setup();
  48. },
  49. make_page: function() {
  50. var me = this;
  51. var page_name = wn.get_route_str();
  52. var page = wn.container.add_page(page_name);
  53. wn.container.change_to(page_name);
  54. this.$page = $(page);
  55. this.$page.html('<div class="layout-wrapper layout-wrapper-background">\
  56. <div class="appframe-area"></div>\
  57. <div class="layout-main-section">\
  58. <div class="wnlist-area"><div class="help">Loading...</div></div>\
  59. </div>\
  60. <div class="layout-side-section">\
  61. <div class="show-docstatus hide" style="margin-bottom: 19px">\
  62. <h4>Show</h4>\
  63. <div><input data-docstatus="0" type="checkbox" checked="checked" /> Drafts</div>\
  64. <div><input data-docstatus="1" type="checkbox" checked="checked" /> Submitted</div>\
  65. <div><input data-docstatus="2" type="checkbox" /> Cancelled</div>\
  66. </div>\
  67. </div>\
  68. <div style="clear: both"></div>\
  69. </div>');
  70. this.appframe = new wn.ui.AppFrame(this.$page.find('.appframe-area'));
  71. wn.views.breadcrumbs(this.appframe, locals.DocType[this.doctype].module, this.doctype);
  72. },
  73. setup: function() {
  74. var me = this;
  75. me.can_delete = wn.model.can_delete(me.doctype);
  76. me.meta = locals.DocType[me.doctype];
  77. me.$page.find('.wnlist-area').empty(),
  78. me.setup_docstatus_filter();
  79. me.setup_listview();
  80. me.init_list();
  81. me.init_stats();
  82. me.make_report_button();
  83. me.add_delete_option();
  84. },
  85. make_report_button: function() {
  86. var me = this;
  87. if(wn.boot.profile.can_get_report.indexOf(this.doctype)!=-1) {
  88. this.appframe.add_button('Build Report', function() {
  89. wn.set_route('Report2', me.doctype);
  90. }, 'icon-th')
  91. }
  92. },
  93. setup_docstatus_filter: function() {
  94. var me = this;
  95. this.can_submit = $.map(locals.DocPerm, function(d) {
  96. if(d.parent==me.meta.name && d.submit) return 1
  97. else return null;
  98. }).length;
  99. if(this.can_submit) {
  100. this.$page.find('.show-docstatus').removeClass('hide');
  101. this.$page.find('.show-docstatus input').click(function() {
  102. me.run();
  103. })
  104. }
  105. },
  106. setup_listview: function() {
  107. if(this.meta.__listjs) {
  108. eval(this.meta.__listjs);
  109. this.listview = new wn.doclistviews[this.doctype](this);
  110. } else {
  111. this.listview = new wn.views.ListView(this);
  112. }
  113. this.listview.parent = this;
  114. this.wrapper = this.$page.find('.wnlist-area');
  115. this.page_length = 20;
  116. this.allow_delete = true;
  117. },
  118. init_list: function(auto_run) {
  119. var me = this;
  120. // init list
  121. this.make({
  122. method: 'webnotes.widgets.doclistview.get',
  123. get_args: this.get_args,
  124. parent: this.wrapper,
  125. start: 0,
  126. page_length: this.page_length,
  127. show_filters: true,
  128. show_grid: true,
  129. new_doctype: this.doctype,
  130. allow_delete: this.allow_delete,
  131. no_result_message: this.make_no_result(),
  132. columns: this.listview.fields
  133. });
  134. // make_new_doc can be overridden so that default values can be prefilled
  135. // for example - communication list in customer
  136. $(this.wrapper).find('button[list_view_doc="'+me.doctype+'"]').click(function(){
  137. me.make_new_doc(me.doctype);
  138. });
  139. if((auto_run !== false) && (auto_run !== 0)) this.run();
  140. },
  141. make_no_result: function() {
  142. var no_result_message = repl('<div class="well">\
  143. <p>No %(doctype_label)s found</p>\
  144. %(description)s\
  145. <hr>\
  146. <p><button class="btn btn-info btn-small" list_view_doc="%(doctype)s">\
  147. Make a new %(doctype_label)s</button>\
  148. </p></div>', {
  149. doctype_label: get_doctype_label(this.doctype),
  150. doctype: this.doctype,
  151. description: wn.markdown(locals.DocType[this.doctype].description || ''),
  152. });
  153. return no_result_message;
  154. },
  155. render_row: function(row, data) {
  156. data.doctype = this.doctype;
  157. this.listview.render(row, data, this);
  158. },
  159. get_query_fields: function() {
  160. return this.listview.fields;
  161. },
  162. get_args: function() {
  163. return {
  164. doctype: this.doctype,
  165. fields: this.get_query_fields(),
  166. filters: this.filter_list.get_filters(),
  167. docstatus: this.can_submit ? $.map(this.$page.find('.show-docstatus :checked'),
  168. function(inp) { return $(inp).attr('data-docstatus') }) : [],
  169. order_by: this.listview.order_by || undefined,
  170. }
  171. },
  172. add_delete_option: function() {
  173. var me = this;
  174. if(this.can_delete) {
  175. this.add_button('Delete', function() { me.delete_items(); }, 'icon-remove')
  176. }
  177. },
  178. delete_items: function() {
  179. var me = this;
  180. var dl = $.map(me.$page.find('.list-delete:checked'), function(e) {
  181. return $(e).data('name');
  182. });
  183. if(!dl.length)
  184. return;
  185. if(!confirm('This is PERMANENT action and you cannot undo. Continue?')) {
  186. return;
  187. }
  188. me.set_working(true);
  189. wn.call({
  190. method: 'webnotes.widgets.doclistview.delete_items',
  191. args: {
  192. items: dl,
  193. doctype: me.doctype
  194. },
  195. callback: function() {
  196. me.set_working(false);
  197. me.refresh();
  198. }
  199. })
  200. },
  201. init_stats: function() {
  202. var me = this
  203. wn.call({
  204. method: 'webnotes.widgets.doclistview.get_stats',
  205. args: {
  206. stats: me.listview.stats,
  207. doctype: me.doctype
  208. },
  209. callback: function(r) {
  210. // This gives a predictable stats order
  211. $.each(me.listview.stats, function(i, v) {
  212. me.render_stat(v, r.message[v]);
  213. });
  214. // reload button at the end
  215. if(me.listview.stats.length) {
  216. $('<button class="btn btn-small"><i class="refresh"></i> Refresh</button>')
  217. .click(function() {
  218. me.reload_stats();
  219. }).appendTo($('<div class="stat-wrapper">')
  220. .appendTo(me.$page.find('.layout-side-section')))
  221. }
  222. }
  223. });
  224. },
  225. render_stat: function(field, stat) {
  226. var me = this;
  227. if(!stat || !stat.length) {
  228. if(field=='_user_tags') {
  229. this.$page.find('.layout-side-section')
  230. .append('<div class="stat-wrapper"><h4>Tags</h4>\
  231. <div class="help small"><i>No records tagged.</i><br><br> \
  232. To add a tag, open the document and click on \
  233. "Add Tag" on the sidebar</div></div>');
  234. }
  235. return;
  236. }
  237. var label = wn.meta.docfield_map[this.doctype][field] ?
  238. wn.meta.docfield_map[this.doctype][field].label : field;
  239. if(label=='_user_tags') label = 'Tags';
  240. // grid
  241. var $w = $('<div class="stat-wrapper">\
  242. <h4>'+ label +'</h4>\
  243. <div class="stat-grid">\
  244. </div>\
  245. </div>');
  246. // sort items
  247. stat = stat.sort(function(a, b) { return b[1] - a[1] });
  248. var sum = 0;
  249. $.each(stat, function(i,v) { sum = sum + v[1]; })
  250. // render items
  251. $.each(stat, function(i, v) {
  252. me.render_stat_item(i, v, sum, field).appendTo($w.find('.stat-grid'));
  253. });
  254. $w.appendTo(this.$page.find('.layout-side-section'));
  255. },
  256. render_stat_item: function(i, v, max, field) {
  257. var me = this;
  258. var args = {}
  259. args.label = v[0];
  260. args.width = flt(v[1]) / max * 100;
  261. args.count = v[1];
  262. args.field = field;
  263. $item = $(repl('<div class="stat-item">\
  264. <div class="stat-bar" style="width: %(width)s%"></div>\
  265. <div class="stat-label">\
  266. <a href="#" data-label="%(label)s" data-field="%(field)s">\
  267. %(label)s</a> \
  268. (%(count)s)</div>\
  269. </div>', args));
  270. this.setup_stat_item_click($item);
  271. return $item;
  272. },
  273. reload_stats: function() {
  274. this.$page.find('.layout-side-section .stat-wrapper').remove();
  275. this.init_stats();
  276. },
  277. setup_stat_item_click: function($item) {
  278. var me = this;
  279. $item.find('a').click(function() {
  280. var fieldname = $(this).attr('data-field');
  281. var label = $(this).attr('data-label');
  282. me.set_filter(fieldname, label);
  283. return false;
  284. });
  285. },
  286. set_filter: function(fieldname, label) {
  287. var filter = this.filter_list.get_filter(fieldname);
  288. if(filter) {
  289. var v = filter.field.get_value();
  290. if(v.indexOf(label)!=-1) {
  291. // already set
  292. return false;
  293. } else {
  294. // second filter set for this field
  295. if(fieldname=='_user_tags') {
  296. // and for tags
  297. this.filter_list.add_filter(fieldname, 'like', '%' + label);
  298. } else {
  299. // or for rest using "in"
  300. filter.set_values(fieldname, 'in', v + ', ' + label);
  301. }
  302. }
  303. } else {
  304. // no filter for this item,
  305. // setup one
  306. if(fieldname=='_user_tags') {
  307. this.filter_list.add_filter(fieldname, 'like', '%' + label);
  308. } else {
  309. this.filter_list.add_filter(fieldname, '=', label);
  310. }
  311. }
  312. this.run();
  313. }
  314. });
  315. wn.views.ListView = Class.extend({
  316. init: function(doclistview) {
  317. this.doclistview = doclistview;
  318. this.doctype = doclistview.doctype;
  319. var t = "`tab"+this.doctype+"`.";
  320. this.fields = [t + 'name', t + 'owner', t + 'docstatus',
  321. t + '_user_tags', t + 'modified'];
  322. this.stats = ['_user_tags'];
  323. this.show_hide_check_column();
  324. },
  325. columns: [
  326. {width: '3%', content:'check'},
  327. {width: '4%', content:'avatar'},
  328. {width: '3%', content:'docstatus', css: {"text-align": "center"}},
  329. {width: '35%', content:'name'},
  330. {width: '40%', content:'tags', css: {'color':'#aaa'}},
  331. {width: '15%', content:'modified', css: {'text-align': 'right', 'color':'#222'}}
  332. ],
  333. render_column: function(data, parent, opts) {
  334. var me = this;
  335. // style
  336. if(opts.css) {
  337. $.each(opts.css, function(k, v) { $(parent).css(k, v)});
  338. }
  339. // multiple content
  340. if(opts.content.indexOf && opts.content.indexOf('+')!=-1) {
  341. $.map(opts.content.split('+'), function(v) {
  342. me.render_column(data, parent, {content:v});
  343. });
  344. return;
  345. }
  346. // content
  347. if(typeof opts.content=='function') {
  348. opts.content(parent, data);
  349. }
  350. else if(opts.content=='name') {
  351. $(parent).append(repl('<a href="#!Form/%(doctype)s/%(name)s">%(name)s</a>', data));
  352. }
  353. else if(opts.content=='avatar') {
  354. $(parent).append(repl('<span class="avatar-small"><img src="%(avatar)s" \
  355. title="%(fullname)s"/></span>',
  356. data));
  357. }
  358. else if(opts.content=='check') {
  359. $(parent).append('<input class="list-delete" type="checkbox">');
  360. $(parent).find('input').data('name', data.name);
  361. }
  362. else if(opts.content=='docstatus') {
  363. $(parent).append(repl('<span class="docstatus"><i class="%(docstatus_icon)s" \
  364. title="%(docstatus_title)s"></i></span>', data));
  365. }
  366. else if(opts.content=='tags') {
  367. this.add_user_tags(parent, data);
  368. }
  369. else if(opts.content=='modified') {
  370. $(parent).append(data.when);
  371. }
  372. else if(opts.type=='bar-graph') {
  373. args = {
  374. percent: data[opts.content],
  375. fully_delivered: (data[opts.content] > 99 ? 'bar-complete' : ''),
  376. label: opts.label
  377. }
  378. $(parent).append(repl('<span class="bar-outer" style="width: 30px; float: right" \
  379. title="%(percent)s% %(label)s">\
  380. <span class="bar-inner %(fully_delivered)s" \
  381. style="width: %(percent)s%;"></span>\
  382. </span>', args));
  383. }
  384. else if(opts.type=='link' && opts.doctype) {
  385. $(parent).append(repl('<a href="#!Form/'+opts.doctype+'/'
  386. +data[opts.content]+'">'+data[opts.content]+'</a>', data));
  387. }
  388. else if(opts.template) {
  389. $(parent).append(repl(opts.template, data));
  390. }
  391. else if(data[opts.content]) {
  392. $(parent).append(' ' + data[opts.content]);
  393. }
  394. },
  395. render: function(row, data) {
  396. var me = this;
  397. this.prepare_data(data);
  398. rowhtml = '';
  399. // make table
  400. $.each(this.columns, function(i, v) {
  401. rowhtml += repl('<td style="width: %(width)s"></td>', v);
  402. });
  403. var tr = $(row).html('<table><tbody><tr>' + rowhtml + '</tr></tbody></table>').find('tr').get(0);
  404. // render cells
  405. $.each(this.columns, function(i, v) {
  406. me.render_column(data, tr.cells[i], v);
  407. });
  408. },
  409. prepare_data: function(data) {
  410. data.fullname = wn.user_info(data.owner).fullname;
  411. data.avatar = wn.user_info(data.owner).image;
  412. this.prepare_when(data, data.modified);
  413. // docstatus
  414. if(data.docstatus==0 || data.docstatus==null) {
  415. data.docstatus_icon = 'icon-pencil';
  416. data.docstatus_title = 'Editable';
  417. } else if(data.docstatus==1) {
  418. data.docstatus_icon = 'icon-lock';
  419. data.docstatus_title = 'Submitted';
  420. } else if(data.docstatus==2) {
  421. data.docstatus_icon = 'icon-remove';
  422. data.docstatus_title = 'Cancelled';
  423. }
  424. // nulls as strings
  425. for(key in data) {
  426. if(data[key]==null) {
  427. data[key]='';
  428. }
  429. }
  430. },
  431. prepare_when: function(data, date_str) {
  432. if (!date_str) date_str = data.modified;
  433. // when
  434. data.when = dateutil.str_to_user(date_str).split(' ')[0];
  435. var diff = dateutil.get_diff(dateutil.get_today(), date_str.split(' ')[0]);
  436. if(diff==0) {
  437. data.when = dateutil.comment_when(date_str);
  438. }
  439. if(diff == 1) {
  440. data.when = 'Yesterday'
  441. }
  442. if(diff == 2) {
  443. data.when = '2 days ago'
  444. }
  445. },
  446. add_user_tags: function(parent, data) {
  447. var me = this;
  448. if(data._user_tags) {
  449. if($(parent).html().length > 0) {
  450. $(parent).append('<br />');
  451. }
  452. $.each(data._user_tags.split(','), function(i, t) {
  453. if(t) {
  454. $('<span class="label label-info" style="cursor: pointer; line-height: 200%">'
  455. + strip(t) + '</span>')
  456. .click(function() {
  457. me.doclistview.set_filter('_user_tags', $(this).text())
  458. })
  459. .appendTo(parent);
  460. }
  461. });
  462. }
  463. },
  464. show_hide_check_column: function() {
  465. if(!this.doclistview.can_delete) {
  466. this.columns = $.map(this.columns, function(v, i) { if(v.content!='check') return v });
  467. }
  468. }
  469. });
  470. wn.provide('wn.views.RecordListView');
  471. wn.views.RecordListView = wn.views.DocListView.extend({
  472. init: function(doctype, wrapper, ListView) {
  473. this.doctype = doctype;
  474. this.wrapper = wrapper;
  475. this.listview = new ListView(this);
  476. this.listview.parent = this;
  477. this.setup();
  478. },
  479. setup: function() {
  480. var me = this;
  481. me.page_length = 10;
  482. $(me.wrapper).empty();
  483. me.init_list();
  484. },
  485. get_args: function() {
  486. var args = this._super();
  487. $.each((this.default_filters || []), function(i, f) {
  488. args.filters.push(f);
  489. });
  490. args.docstatus = args.docstatus.concat((this.default_docstatus || []));
  491. return args;
  492. },
  493. });