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.
 
 
 
 
 
 

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