Activating bcmagic model & initial data.
[htsworkflow.git] / htsworkflow / frontend / static / js / htsw.js
1 Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
2
3 Ext.override(Ext.Panel,{
4   getState: function() {
5     return { collapsed: this.collapsed };
6     }
7 });
8
9 var quick_msg = function(msg)
10 {
11     Ext.MessageBox.show({
12         title: 'Info',
13         msg: msg,
14         buttons: Ext.MessageBox.OK,
15         icon: Ext.MessageBox.INFO
16     });
17 }
18
19 var goto_url = function(www_url)
20 {
21     window.location = www_url; 
22 }
23
24 $(document).ready(function(){
25     //----------------------------------------
26     // Django Library Page CSS Fix
27     var fix_library_css = function() {
28       var tofix = Ext.fly('library-index-div');
29       if (tofix != null)
30       {
31         tofix.select('*').addClass('djangocss');
32       }
33     }
34     fix_library_css();
35     
36     
37     //----------------------------------------
38     // Dynamically Generate Panels from HTML!
39     var create_dynamic_panels = function(){
40         var wp_items = Ext.fly('west_panel_items');
41         var ul_items = wp_items.query('ul');
42         
43         var dynamic_panels = new Array();
44         Ext.each(ul_items, function(ul) {
45             var panel_obj = new Ext.Panel({
46                 frame: true,
47                 title: ul.id,
48                 //collapsible: true,
49                 //titleCollapse: true,
50                 //collapsed: true,
51                 //stateful: true,
52                 //stateId: 'freezer_panel_state',
53                 margins: '0 0 0 0',
54                 //width: 200,
55                 contentEl: ul
56                 //stateEvents: ['collapse', 'expand']
57             });
58             dynamic_panels.push(panel_obj);
59         });
60         return dynamic_panels;
61     }
62     
63     var panel_bcmagic = new Ext.Panel({
64         //title: 'BC Magic',
65         unstyled: true,
66         contentEl: 'bcmagic_div',
67         height: 180
68     });
69     
70     var menuPanel = new Ext.Panel({
71         id: 'menu_panel',
72         region: 'west',
73         hidden: true,
74         collapsible: true,
75         split: true,
76         collapseMode: 'mini',
77         margins: '4 0 0 0',
78         //cmargins: '2 2 0 2',
79         width: 200,
80         minWidth: 150,
81         border: false,
82         //baseCls: 'x-plain',
83         unstyled: true,
84         layout: 'vbox',
85         layoutConfig: {
86             align: 'stretch',
87             pack: 'start'
88         },
89         // Add dynamically generated panels from html and include barcode magic
90        items: create_dynamic_panels().concat([panel_bcmagic])
91     });
92     
93     
94     
95     //-------------------------------
96     // East Region Setup
97     //-------------------------------
98     var get_east_panel_content = function(){
99       // East panel contentEl id is supplied in html div id of east_region_config.
100       var east_id = Ext.fly('east_region_config').dom.textContent;
101       
102       // Length of zero is a valid response... also happens to return null in next
103       //   if statement if not handled before hand.
104       if (east_id.length == 0){
105         return east_id;
106       }
107       
108       // If no element exists with the supplied content id, report and error.
109       if (Ext.fly(east_id) == null){
110         return 'east_region_config_error';
111       }
112       
113       return east_id;
114     }
115     
116     var east_panel_content_id = get_east_panel_content();
117     if (east_panel_content_id.length > 0){
118       var eastPanel = new Ext.Panel({
119           region: 'east',
120           layout: 'auto',
121           //margins: '0 2 0 2',
122           width: 180,
123           collapsible: true,
124           split: true,
125           collapseMode: 'mini',
126           autoScroll: true,
127           contentEl: east_panel_content_id
128       });
129     } else {
130       var eastPanel = new Ext.Panel({
131           region: 'east',
132           layout: 'auto',
133           //margins: '0 2 0 2',
134           width: 180,
135           collapsible: true,
136           split: true,
137           hidden: true,
138           collapseMode: 'mini',
139           autoScroll: true
140           //contentEl: ''
141       });
142     }
143     
144
145     //-------------------------------
146     // Main Viewport Setup
147     //-------------------------------
148     var mainBorderPanel = new Ext.Viewport({
149        layout: 'border',
150        items: [{
151             region: 'north',
152             layout: 'vBox',
153             layoutConfig: {
154                 align: 'stretch',
155                 pack: 'start'
156             },
157             items: [{
158                     xtype: 'box',
159                     applyTo: 'header',
160                     id: 'header-panel',
161                     height: 30
162                 },{
163                     id: 'main_toolbar',
164                     xtype: 'toolbar',
165                     height: 30,
166                     //height: 100,
167                     /*items: [{
168                         text: "Demo Button",
169                         handler: function() { quick_msg('Messages can be fun!'); }
170                     }],*/
171                     margins: '2 0 0 0'
172                 },{
173                     id: 'app_toolbar',
174                     xtype: 'toolbar',
175                     height: 30
176                     //margins: '2 0 0 0'
177                 }],
178             height: 90
179        },menuPanel,{
180             //title: 'Body',
181             region: 'center',
182             xtype: 'panel',
183             //autoScroll: true,
184             layout: 'fit',
185             margins: '2 0 2 0',
186             items: [{
187                 //title: 'Inner Panel',
188                 contentEl: 'body_content',
189                 border: true,
190                 autoScroll: true
191             }]
192             },eastPanel]
193     });
194     
195     //-------------------------------
196     // Menu Bar Setup
197     //-------------------------------
198     var main_tb = Ext.getCmp('main_toolbar');
199     
200     var add_buttons_from_html = function(tb, bar_id){
201         var tbar_data = Ext.fly(bar_id);
202         var div_array = tbar_data.query('div');
203         var div_id = null;
204         // Loop through each div since it defines a button and link or a spacer and add it to the right side of the toolbar
205         Ext.each(div_array, function(divobj) {
206             div_id = divobj.id;
207             if (div_id == 'spacer'){
208                 tb.add('-');
209             } else {
210                 tb.add({
211                     text: div_id,
212                     handler: function() { goto_url(divobj.getAttribute('href')); }
213                 });
214             }
215         });
216     }
217     
218     // Fill in left side of main toolbar
219     add_buttons_from_html(main_tb, 'left_tbar_data');
220     
221     // Shifts the remaining toolbar options to the right side.
222     main_tb.add({ xtype: 'tbfill' });
223     
224     //----------------------------------------
225     // ExtJS Barcode Magic Implementation
226     var bcmagic_ext_keyhandler = function(sObj, e){
227       //Process upon enter key as input.
228       if (e.getKey() == e.ENTER)
229         bcmagic_process();
230     }
231     
232     var bcmagic_input = new Ext.form.TextField({
233       id: 'bcmagic_input_field',
234       emptyText: 'barcode magic'
235     });
236     bcmagic_input.on('specialkey', bcmagic_ext_keyhandler);
237     
238     main_tb.add(bcmagic_input);
239     //--------------------------------------
240     
241     var user_info = Ext.fly('login_info');
242     var logout_url = user_info.getAttribute('logouturl');
243     var login_url = user_info.getAttribute('loginurl');
244     
245     if (user_info.getAttribute('authenticated') == 'true') {
246         main_tb.add({
247                         xtype: 'tbtext',
248                         text: 'User: ' + user_info.getAttribute('user')
249                     });
250         main_tb.add('-');
251         add_buttons_from_html(main_tb, 'right_tbar_data');
252         main_tb.add('-');
253         main_tb.add({
254                         text: 'Logout',
255                         handler: function() { goto_url(logout_url); }
256                     });
257         
258     } else {
259         main_tb.add({
260                         text: 'Login',
261                         handler: function() { goto_url(login_url) }
262                     });
263     }
264     
265     main_tb.doLayout();
266     
267     //-------------------------------
268     // App Toolbar Setup
269     //-------------------------------
270     var app_tb = Ext.getCmp('app_toolbar');
271     add_buttons_from_html(app_tb, 'app_toolbar_west');
272     app_tb.add({ xtype: 'tbfill' });
273     add_buttons_from_html(app_tb, 'app_toolbar_east');
274     app_tb.doLayout();
275     
276     // Focus on barcode magic, because it's awesome and needs attention! ;-)
277     bcmagic_input.focus();
278 });