Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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