blob: 429920fd630b31831bc69de8f316057c6d3c6969 [file] [log] [blame]
Rushabh Mehtad0251332012-02-21 17:26:50 +05301wn.provide('erpnext.module_page');
2
3erpnext.module_page.setup_page = function(module, wrapper) {
4 erpnext.module_page.hide_links(wrapper);
5 erpnext.module_page.make_list(module, wrapper);
Rushabh Mehta35fabf72012-02-21 19:03:50 +05306 $(wrapper).find("a[title]").tooltip({
Rushabh Mehtad0251332012-02-21 17:26:50 +05307 delay: { show: 500, hide: 100 }
8 });
9}
10
11// hide list links where the user does
12// not have read permissions
13
14erpnext.module_page.hide_links = function(wrapper) {
15 // lists
16 $(wrapper).find('[href*="List/"]').each(function() {
17 var href = $(this).attr('href');
18 var dt = href.split('/')[1];
19 if(wn.boot.profile.can_read.indexOf(get_label_doctype(dt))==-1) {
20 var txt = $(this).text();
21 $(this).parent().css('color', '#999').html(txt);
22 }
23 });
24
25 // reports
26 $(wrapper).find('[data-doctype]').each(function() {
27 var dt = $(this).attr('data-doctype');
28 if(wn.boot.profile.can_read.indexOf(dt)==-1) {
29 var txt = $(this).text();
30 $(this).parent().css('color', '#999').html(txt);
31 }
32 });
33
34 // single (forms)
35 $(wrapper).find('[href*="Form/"]').each(function() {
36 var href = $(this).attr('href');
37 var dt = href.split('/')[1];
38 if(wn.boot.profile.can_read.indexOf(get_label_doctype(dt))==-1) {
39 var txt = $(this).text();
40 $(this).parent().css('color', '#999').html(txt);
41 }
42 });}
43
44// make list of reports
45
46erpnext.module_page.make_list = function(module, wrapper) {
47 // make project listing
48 wrapper.list = new wn.widgets.Listing({
49 parent: $(wrapper).find('.reports-list').get(0),
50 method: 'utilities.get_report_list',
51 render_row: function(row, data) {
52 if(!data.parent_doc_type) data.parent_doc_type = data.doc_type;
53 $(row).html(repl('<a href="#!Report/%(doc_type)s/%(criteria_name)s" \
54 data-doctype="%(parent_doc_type)s">\
55 %(criteria_name)s</a>', data))
56 },
57 args: {
58 module: module
59 },
60 no_refresh: true
61 });
62 wrapper.list.run();
63}
64
65
66
Rushabh Mehta865c00a2012-01-24 14:33:21 +053067// ====================================================================
68
69pscript.startup_make_sidebar = function() {
70 $y(page_body.left_sidebar, {width:(100/6)+'%', paddingTop:'8px'});
71
72 var callback = function(r,rt) {
73 // menu
74 var ml = r.message;
75
76 // clear
77 page_body.left_sidebar.innerHTML = '';
78
79 for(var m=0; m<ml.length; m++){
80 if(ml[m]) {
81 new SidebarItem(ml[m]);
82 }
83 }
Rushabh Mehta865c00a2012-01-24 14:33:21 +053084 nav_obj.observers.push({notify:function(t,dt,dn) { pscript.select_sidebar_menu(t, dt, dn); }});
85
86 // select current
87 var no = nav_obj.ol[nav_obj.ol.length-1];
88 if(no && menu_item_map[decodeURIComponent(no[0])][decodeURIComponent(no[1])])
89 pscript.select_sidebar_menu(decodeURIComponent(no[0]), decodeURIComponent(no[1]));
90 }
91 $c_obj('Home Control', 'get_modules', '', callback);
92}
93
94// ====================================================================
95// Menu observer
96// ====================================================================
97
98cur_menu_pointer = null;
99var menu_item_map = {'Form':{}, 'Page':{}, 'Report':{}, 'List':{}}
100
101pscript.select_sidebar_menu = function(t, dt, dn) {
102 // get menu item
103 if(menu_item_map[t][dt]) {
104 // select
105 menu_item_map[t][dt].select();
106 } else {
107 // none found :-( Unselect
108 if(cur_menu_pointer)
109 cur_menu_pointer.deselect();
110 }
111}
112
113// ====================================================================
114// Menu pointer
115// ====================================================================
116
117var body_background = '#e2e2e2';
118
119MenuPointer = function(parent, label) {
Rushabh Mehta7018b192012-02-02 13:42:28 +0530120 var me = this;
Rushabh Mehta865c00a2012-01-24 14:33:21 +0530121 this.wrapper = $a(parent, 'div', '', {padding:'0px', cursor:'pointer', margin:'2px 0px'});
122 $br(this.wrapper, '3px');
123
124 this.tab = make_table($a(this.wrapper, 'div'), 1, 2, '100%', ['', '11px'], {height:'22px',
125 verticalAlign:'middle', padding:'0px'}, {borderCollapse:'collapse', tableLayout:'fixed'});
126
127 $y($td(this.tab, 0, 0), {padding:'0px 4px', color:'#444', whiteSpace:'nowrap'});
128
129 // triangle border (?)
130 this.tab.triangle_div = $a($td(this.tab, 0, 1), 'div','', {
131 borderColor: body_background + ' ' + body_background + ' ' + body_background + ' ' + 'transparent',
132 borderWidth:'11px', borderStyle:'solid', height:'0px', width:'0px', marginRight:'-11px'});
133
134 this.label_area = $a($td(this.tab, 0, 0), 'span', '', '', label);
135
136 $(this.wrapper)
137 .hover(
Rushabh Mehta7018b192012-02-02 13:42:28 +0530138 function() { if(!me.selected)
139 $bg(this, '#eee'); } ,
140 function() { if(!me.selected)
141 $bg(this, body_background); }
Rushabh Mehta865c00a2012-01-24 14:33:21 +0530142 )
143
144 $y($td(this.tab, 0, 0), {borderBottom:'1px solid #ddd'});
145
146}
147
148// ====================================================================
149
150MenuPointer.prototype.select = function(grey) {
Rushabh Mehta7018b192012-02-02 13:42:28 +0530151 $y($td(this.tab, 0, 0), {
152 color:'#fff', borderBottom:'0px solid #000'
153 });
154 $(this.wrapper).css('background-color', '#999');
Rushabh Mehta865c00a2012-01-24 14:33:21 +0530155 this.selected = 1;
156
157 if(cur_menu_pointer && cur_menu_pointer != this)
158 cur_menu_pointer.deselect();
159
160 cur_menu_pointer = this;
161}
162
163// ====================================================================
164
165MenuPointer.prototype.deselect = function() {
166 $y($td(this.tab, 0, 0), {color:'#444', borderBottom:'1px solid #ddd'});
Rushabh Mehta7018b192012-02-02 13:42:28 +0530167 $(this.wrapper).css('background-color', body_background);
Rushabh Mehta865c00a2012-01-24 14:33:21 +0530168 this.selected = 0;
169}
170
171
172// ====================================================================
173// Sidebar Item
174// ====================================================================
175
176var cur_sidebar_item = null;
177
178SidebarItem = function(det) {
179 var me = this;
180 this.det = det;
181 this.wrapper = $a(page_body.left_sidebar, 'div', '', {marginRight:'12px'});
182
183 this.body = $a(this.wrapper, 'div');
184 this.tab = make_table(this.body, 1, 2, '100%', ['24px', null], {verticalAlign:'middle'}, {tableLayout:'fixed'});
185
186 // icon
187 var ic = $a($td(this.tab, 0, 0), 'div', 'module-icons module-icons-' + det.module_label.toLowerCase(), {marginLeft:'3px', marginBottom:'-2px'});
188
189 // pointer table
190 this.pointer = new MenuPointer($td(this.tab, 0, 1), det.module_label);
191 $y($td(this.pointer.tab, 0, 0), {fontWeight:'bold'});
192
193 // for page type
194 if(det.module_page) {
195 menu_item_map.Page[det.module_page] = this.pointer;
196 }
197
198 // items area
199 this.items_area = $a(this.wrapper, 'div');
200
201 this.body.onclick = function() { me.onclick(); }
202}
203
204// ====================================================================
205
206SidebarItem.prototype.onclick = function() {
207 var me = this;
208
209 if(this.det.module_page) {
210 // page type
211 this.pointer.select();
212
Rushabh Mehta949496c2012-01-25 18:48:46 +0530213 $(me.pointer.label_area).set_working();
214 loadpage(this.det.module_page, function() {
215 $(me.pointer.label_area).done_working();
216 });
Rushabh Mehta865c00a2012-01-24 14:33:21 +0530217
218 } else {
219 // show sub items
220 this.toggle();
221 }
222}
223
224// ====================================================================
225
226SidebarItem.prototype.collapse = function() {
227 $(this.items_area).slideUp();
228 this.is_open = 0;
229 $fg(this.pointer.label_area, '#444')
230}
231
232// ====================================================================
233
234SidebarItem.prototype.toggle = function() {
235 if(this.loading) return;
236
237 if(this.is_open) {
238 this.collapse();
239 } else {
240 if(this.loaded) $(this.items_area).slideDown();
241 else this.show_items();
242 this.is_open = 1;
243 $fg(this.pointer.label_area, '#000')
244 //this.pointer.select(1);
245
246 // close existing open
247 if(cur_sidebar_item && cur_sidebar_item != this) {
248 cur_sidebar_item.collapse();
249 }
250 cur_sidebar_item = this;
251 }
252}
253
254// ====================================================================
255
256SidebarItem.prototype.show_items = function() {
257 this.loading = 1;
258 var me = this;
259
Rushabh Mehta949496c2012-01-25 18:48:46 +0530260 $(this.pointer.label_area).set_working();
Rushabh Mehta865c00a2012-01-24 14:33:21 +0530261 var callback = function(r,rt){
262 me.loaded = 1;
263 me.loading = 0;
264 var smi = null;
265 var has_reports = 0;
266 var has_tools = 0;
267
268 // widget code
Rushabh Mehta949496c2012-01-25 18:48:46 +0530269 $(me.pointer.label_area).done_working();
Rushabh Mehta865c00a2012-01-24 14:33:21 +0530270
271 if(r.message.il) {
272 me.il = r.message.il;
273
274 // forms
275 for(var i=0; i<me.il.length;i++){
276 if(me.il[i].doc_type == 'Forms') {
277 if(in_list(profile.can_read, me.il[i].doc_name)) {
278 var smi = new SidebarModuleItem(me, me.il[i]);
279
280 menu_item_map['Form'][me.il[i].doc_name] = smi.pointer;
281 menu_item_map['List'][me.il[i].doc_name] = smi.pointer;
282 }
283 }
284 if(me.il[i].doc_type=='Reports') has_reports = 1;
Rushabh Mehtaa8ad3902012-01-25 10:37:35 +0530285 if(in_list(['Single DocType', 'Pages', 'Setup Forms'], me.il[i].doc_type))
286 has_tools = 1;
Rushabh Mehta865c00a2012-01-24 14:33:21 +0530287 }
288 // reports
289 if(has_reports) {
290 var smi = new SidebarModuleItem(me, {doc_name:'Reports', doc_type:'Reports'});
291
292 // add to menu-item mapper
293 menu_item_map['Page'][me.det.module_label + ' Reports'] = smi.pointer;
294 }
295
296 // tools
297 if(has_tools) {
298 var smi = new SidebarModuleItem(me, {doc_name:'Tools', doc_type:'Tools'});
299
300 // add to menu-item mapper
301 menu_item_map['Page'][me.det.module_label + ' Tools'] = smi.pointer;
302 }
303
304 // custom reports
305 if(r.message.custom_reports.length) {
306 me.il = add_lists(r.message.il, r.message.custom_reports);
307 var smi = new SidebarModuleItem(me, {doc_name:'Custom Reports', doc_type:'Custom Reports'});
308
309 // add to menu-item mapper
310 menu_item_map['Page'][me.det.module_label + ' Custom Reports'] = smi.pointer;
311 }
312 }
313
314
315 $(me.items_area).slideDown();
316
317 // high light
318 var no = nav_obj.ol[nav_obj.ol.length-1];
319 if(no && menu_item_map[decodeURIComponent(no[0])][decodeURIComponent(no[1])])
320 pscript.select_sidebar_menu(decodeURIComponent(no[0]), decodeURIComponent(no[1]));
321
322 }
323
324 $c_obj('Home Control', 'get_module_details', me.det.name, callback);
325}
326
327// ====================================================================
328// Show Reports
329// ====================================================================
330
331SidebarItem.prototype.show_section = function(sec_type) {
332 var me = this;
333 var label = this.det.module_label + ' ' + sec_type;
Rushabh Mehtaab1148c2012-01-31 18:01:16 +0530334 var type_map = {'Reports':'Reports', 'Custom Reports':'Custom Reports',
335 'Pages':'Tools', 'Single DocType':'Tools', 'Setup Forms':'Tools'}
336
Rushabh Mehta865c00a2012-01-24 14:33:21 +0530337
338 if(page_body.pages[label]) {
339 loadpage(label, null, 1);
340 } else {
Rushabh Mehtaab1148c2012-01-31 18:01:16 +0530341
Rushabh Mehta865c00a2012-01-24 14:33:21 +0530342 // make the reports page
343 var page = page_body.add_page(label);
344 this.wrapper = $a(page,'div','layout_wrapper');
345
346
347 // head
348 this.head = new PageHeader(this.wrapper, label);
349
350 // body
351 this.body1 = $a(this.wrapper, 'div', '', {marginTop:'16px'});
352
353 // add a report link
354 var add_link = function(det) {
355 var div = $a(me.body1, 'div', '', {marginBottom:'6px'});
356 var span = $a(div, 'span', 'link_type');
357
358 // tag the span
359 span.innerHTML = det.display_name; span.det = det;
360 if(sec_type=='Reports' || sec_type=='Custom Reports') {
361 // Reports
362 // -------
363 span.onclick = function() { loadreport(this.det.doc_name, this.det.display_name); }
364
365 } else {
366 // Tools
367 // -----
368
369 if(det.doc_type=='Pages') {
370 // Page
371 if(det.click_function) {
372 span.onclick = function() { eval(this.det.click_function) }
373 span.click_function = det.click_function;
374 } else {
375 span.onclick = function() { loadpage(this.det.doc_name); }
376 }
377 } else if(det.doc_type=='Setup Forms') {
378 // Doc Browser
379 span.onclick = function() { loaddocbrowser(this.det.doc_name); }
380 } else {
381 // Single
382 span.onclick = function() { loaddoc(this.det.doc_name, this.det.doc_name); }
383 }
384 }
385 }
386
387 // item list
388 for(var i=0; i<me.il.length;i++){
389 if(type_map[me.il[i].doc_type] == sec_type) {
390 add_link(me.il[i]);
391 }
392 }
393 loadpage(label, null, 1);
394 }
395}
396
397
398// ====================================================================
399// Sidebar module item
400// ====================================================================
401
402SidebarModuleItem = function(si, det) {
403 this.det = det;
404 var me= this;
405
406 this.pointer = new MenuPointer(si.items_area, get_doctype_label(det.doc_name));
407 $y(si.items_area, {marginLeft:'32px'})
408 $y($td(this.pointer.tab, 0, 0), {fontSize:'11px'});
409
410 this.pointer.wrapper.onclick = function() {
411 if(me.det.doc_type=='Forms')
412 loaddocbrowser(det.doc_name);
413 else
414 si.show_section(me.det.doc_type);
415 }
416}