Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 18th, 2010 | Syntax: JavaScript | Size: 12.36 KB | Hits: 47 | Expires: Never
Copy text to clipboard
  1. /**
  2.  * User Interface Engine for IPMS. Used for UI objects definition purpose.
  3.  *
  4.  * @author Aprian Diaz Novandi (aprian.diaz.novandi@gmail.com)
  5.  * @version 1.0
  6.  * @since March 14th, 2010
  7.  *
  8.  */
  9.  
  10. //global variables
  11. var baseUrl = '';
  12.  
  13. IpmsUIEngine = function() {
  14.     /*
  15.      * -----------------
  16.      * Basic properties
  17.      * -----------------
  18.      */
  19.     //this.baseUrl = '';
  20.  
  21.     /*
  22.      * ---------------------
  23.      * Property definitions
  24.      * ---------------------
  25.      */
  26.     this.loginForm = new Ext.FormPanel({
  27.         id: 'formpanel',
  28.         labelWidth: 75,
  29.         url: this.baseUrl,
  30.         frame: true,
  31.         title: 'IPMS » Login Form',
  32.         bodyStyle:'padding:5px 5px 0',
  33.         width: '100%',
  34.         method: 'POST',
  35.         defaults: {
  36.             width: '95%'
  37.         },
  38.         defaultType: 'textfield',
  39.         items: [{
  40.             fieldLabel: 'Username',
  41.             name: 'username',
  42.             id: 'username',
  43.             allowBlank: false
  44.             //enableKeyEvents: true
  45.         },{
  46.             fieldLabel: 'Password',
  47.             name: 'password',
  48.             id: 'password',
  49.             xtype: 'textfield',
  50.             inputType: 'password',
  51.             allowBlank: false,
  52.             enableKeyEvents: true,
  53.             //event listeners
  54.             listeners: {
  55.                 specialkey: function(field, el) {
  56.                     /*
  57.                      * This is to add enter key press event handler for users
  58.                      * after typing their username and password.
  59.                      * ExtJS doesn't provide this behavior by default.
  60.                      */
  61.                     if(el.getKey() == Ext.EventObject.ENTER) {
  62.                         Ext.getCmp('submitButton').fireEvent('click');
  63.                     }
  64.                 }
  65.             }
  66.         }],
  67.         buttons: [{
  68.             text: '<strong>Login</strong>',
  69.             id: 'submitButton',
  70.             formBind: true,
  71.             listeners: {
  72.                 click: function() {
  73.                     var loginUrl = baseUrl + '/users/login';
  74.                     Ext.getCmp('formpanel').getForm().submit({
  75.                         waitMsg: 'Processing...',
  76.                         url: loginUrl,
  77.                         success: function(form, action) {
  78.                             if(action.result.success == "true") {
  79.                                 window.location = action.result.url;
  80.                             } else {
  81.                                 Ext.Msg.alert('Failure', action.result.msg);
  82.                             }
  83.                         },
  84.                         failure: function(form, action) {
  85.                             switch (action.failureType) {
  86.                                 case Ext.form.Action.CLIENT_INVALID:
  87.                                     Ext.Msg.alert('Failure',
  88.                                         'Form fields may not be submitted with invalid values');
  89.                                     break;
  90.                                 case Ext.form.Action.CONNECT_FAILURE:
  91.                                     Ext.Msg.alert('Failure', 'Ajax communication failed ' + loginUrl);
  92.                                     break;
  93.                                 case Ext.form.Action.SERVER_INVALID:
  94.                                     Ext.Msg.alert('Failure', action.result.msg);
  95.                                     break;
  96.                                 default:
  97.                                     Ext.Msg.alert('Failure', action.result.msg);
  98.                                     break;
  99.                            }
  100.                         }
  101.                     });
  102.                 }
  103.             }
  104.         }, {
  105.             text: 'Clear',
  106.             id: 'resetButton',
  107.             handler: function() {
  108.                 Ext.getCmp('formpanel').getForm().reset();
  109.             }
  110.         }]
  111.     });
  112.  
  113.     this.logoutButton = new Ext.Button({
  114.         text: 'Logout',
  115.         listeners: {
  116.             click: function() {
  117.                 Ext.Msg.show({
  118.                     title: 'Logout confirmation',
  119.                     msg: 'Are you sure to log out from application?',
  120.                     buttons: Ext.Msg.YESNO,
  121.                     fn: function(buttonId) {
  122.                         if(buttonId == 'yes') {
  123.                             Ext.Ajax.request({
  124.                                 url: baseUrl + '/users/logout',
  125.                                 success: function(response) {
  126.                                     var data = Ext.util.JSON.decode(response.responseText);
  127.                                     var success = data.success;
  128.                                     var url = data.url;
  129.                                     if(success == 'true') {
  130.                                         window.location = url;
  131.                                     } else {
  132.                                         Ext.Msg.alert('Failure', 'Failed to log out');
  133.                                     }
  134.                                 },
  135.                                 failure: function() {
  136.                                     Ext.Msg.alert('Failure', 'Ajax communication failed');
  137.                                 }
  138.                             });
  139.                         }
  140.                     }
  141.                 });
  142.             }
  143.         }
  144.     });
  145.  
  146.         this.tespanel = new Ext.FormPanel({
  147.         id: 'formpanel2',
  148.         labelWidth: 75,
  149.         url: this.baseUrl,
  150.         frame: true,
  151.         title: 'tes panel',
  152.         bodyStyle:'padding:5px 5px 0',
  153.         width: '100%',
  154.         });
  155.        
  156.         ///////////GRID ALARM///////////////
  157.         var doWinPopUp = function(rec){
  158.                 var nid = rec.get('node_id');
  159.                 var nname = rec.get('node_name');
  160.                 var dvc = rec.get('device_name');
  161.                 var almName = rec.get('alarm_name');
  162.                 var almParam = rec.get('alarm_parameter');
  163.                 var almVal = rec.get('value');
  164.                 var almValIdeal = rec.get('ideal_value');
  165.                 var almUnit = rec.get('unit');
  166.                 var almPrio = rec.get('priority');
  167.                 var numTicket = rec.get('num_tickets');
  168.  
  169.                 var fieldSet = new Ext.form.FieldSet({
  170.                         id                              : "fldSet"
  171.                         ,title                  : "Info"
  172.                         ,defaultType    : "textfield"
  173.                         ,defaults               : {
  174.                                 readOnly        : true
  175.                         }
  176.                         ,items                  : [{
  177.                                         fieldLabel      : "Node ID"
  178.                                         ,name           : "fldNodeId"
  179.                                         ,width          : 200
  180.                                         ,value          : nid
  181.                                 },{
  182.                                         fieldLabel      : "Node"
  183.                                         ,name           : "fldNode"
  184.                                         ,width          : 200
  185.                                         ,value          : nname
  186.                                 },{
  187.                                         fieldLabel      : "Alarm Name"
  188.                                         ,name           : "fldAlarmName"
  189.                                         ,width          : 200                                  
  190.                                         ,value          : almName
  191.                                 },{
  192.                                         fieldLabel      : "Alarm Parameter"
  193.                                         ,name           : "fldAlarmParameter"
  194.                                         ,width          : 200                                  
  195.                                         ,value          : almParam
  196.                                 },{
  197.                                         fieldLabel      : "Device"
  198.                                         ,name           : "fldDevice"
  199.                                         ,width          : 200                                  
  200.                                         ,value          : dvc
  201.                                 },{
  202.                                         fieldLabel      : "Value"
  203.                                         ,name           : "fldValue"
  204.                                         ,width          : 50                           
  205.                                         ,value          : almVal
  206.                                 },{
  207.                                         fieldLabel      : "Priority"
  208.                                         ,name           : "fldPriority"
  209.                                         ,width          : 50
  210.                                         ,value          : almPrio
  211.                                 },{
  212.                                         fieldLabel      : "# tickets"
  213.                                         ,name           : "fieldNumTickets"
  214.                                         ,width          : 50
  215.                                         ,value          : numTicket
  216.                         }]
  217.                 });            
  218.  
  219.                 var formPanel = new Ext.form.FormPanel({
  220.                         id                                      : "frmPanel"
  221.                         ,title                          : "Selected Event"
  222.                         ,frame                          : true
  223.                         ,labelAlign                     : 'right'
  224.                         ,waitMsgTarget          : true
  225.                         ,items                          : fieldSet
  226.                         ,buttonAlign            : "center"
  227.                 });
  228.                
  229.                 var winPop = new Ext.Window({
  230.                         id                              : "winpop"
  231.                         ,height                 : 400
  232.                         ,width                  : 400
  233.                         ,layout                 : "fit"
  234.                         ,plain                  : true
  235.                         ,border                 : false
  236.                         ,resizable              : false
  237.                         ,draggable              : true
  238.                         ,closable               : true
  239.                         ,closeAction    : "close"
  240.                         ,modal                  : true
  241.                         ,items                  : formPanel
  242.                 });
  243.        
  244.                 winPop.show();
  245.         }
  246.        
  247.         this.dataRec = Ext.data.Record.create([
  248.                 {name: 'id'}               
  249.            ,{name: 'tstamp', type: 'date'}
  250.            ,{name: 'node_id'}
  251.            ,{name: 'node_name'}
  252.            ,{name: 'alarm_name'}
  253.            ,{name: 'alarm_parameter'}
  254.            ,{name: 'device_name'}
  255.            ,{name: 'value', type: 'float'}
  256.            ,{name: 'ideal_value', type: 'float'}
  257.            ,{name: 'unit'}
  258.            ,{name: 'priority'}
  259.            ,{name: 'hidden'}
  260.            ,{name: 'num_tickets'}      
  261.         ]);
  262.        
  263.         this.dataReader = new Ext.data.XmlReader({
  264.                 record          : "event"
  265.                 ,idProperty     : "id"
  266.                 }
  267.                 ,this.dataRec
  268.         );
  269.        
  270.         this.dataStore = new Ext.data.Store({
  271.         url             : baseUrl + '/data/events'
  272.         ,reader : this.dataReader
  273.     });
  274.        
  275.         this.columnModel = new Ext.grid.ColumnModel(
  276.                 [{
  277.                         header          : "Id"
  278.                         ,dataIndex      : "id"
  279.                         ,width          : 60
  280.                         ,hidden         : true
  281.                
  282.                  },{
  283.                         header          : "Time"
  284.                         ,dataIndex      : "tstamp"
  285.                         ,width          : 60
  286.                         ,sortable       : true
  287.                  
  288.                  },{
  289.                
  290.                         header          : "Node Id"
  291.                         ,dataIndex      : "node_id"
  292.                         ,width          : 60
  293.                         ,hidden         : true
  294.                
  295.                  },{
  296.                  
  297.                         header          : "Node"
  298.                         ,dataIndex      : "node_name"
  299.                         ,width          : 70
  300.                
  301.                  },{
  302.                  
  303.                         header          : "Alarm Name"
  304.                         ,dataIndex      : "alarm_name"
  305.                         ,width          : 180
  306.                         ,sortable       : true
  307.                  
  308.                  },{
  309.                  
  310.                         header          : "Alarm Parameter"
  311.                         ,dataIndex      : "alarm_parameter"
  312.                         ,width          : 180
  313.                         ,sortable       : true
  314.                         ,hidden         : true
  315.                  
  316.                  },{
  317.  
  318.                         header          : "Device"
  319.                         ,dataIndex      : "device_name"
  320.                         ,width          : 100
  321.                         ,sortable       : true           
  322.                  
  323.                  },{
  324.                        
  325.                         header          : "Value"
  326.                         ,dataIndex      : "value"
  327.                         ,width          : 50
  328.                         ,readonly       : true
  329.                
  330.                  },{
  331.                  
  332.                         header          : "Ideal Value"
  333.                         ,dataIndex      : "ideal_value"
  334.                         ,width          : 50
  335.                         ,readOnly       : true
  336.                
  337.                  },{
  338.                  
  339.                         header          : "Unit"
  340.                         ,dataIndex      : "unit"
  341.                         ,width          : 50
  342.                         ,readOnly       : true
  343.                
  344.                  },{
  345.                  
  346.                         header          : "Priority"
  347.                         ,dataIndex      : "priority"
  348.                         ,width          : 30
  349.                         ,sortable       : true
  350.                         ,hidden         : true
  351.                
  352.                  },{
  353.                  
  354.                         header          : "Hidden"
  355.                         ,dataIndex      : "hidden"
  356.                         ,width          : 30
  357.                         ,hidden         : true
  358.                        
  359.                  },{
  360.                        
  361.                         header          : "Num tickets"
  362.                         ,dataIndex      : "num_tickets"
  363.                         ,width          : 30
  364.                         ,hidden         : true
  365.                        
  366.                 }]
  367.         );
  368.        
  369.         this.selectionModel = new Ext.grid.RowSelectionModel({
  370.                 singleSelect    : true
  371.     });
  372.  
  373.         this.pagingToolbar = new Ext.PagingToolbar({
  374.                 pageSize                : 25
  375.                 ,store                  : this.dataStore
  376.                 ,displayInfo    : true
  377.                 ,displayMsg             : 'Displaying events {0} - {1} of {2}'
  378.                 ,emptyMsg               : "No Alarms to display"
  379.     });
  380.  
  381.         var doRowClick = function(thisGrid, rowIndex){
  382.                 var rec = thisGrid.getStore().getAt(rowIndex);
  383.  
  384.                 doWinPopUp(rec);               
  385.         }
  386.  
  387.         var gridView = new Ext.grid.GridView({
  388.            //forceFit: true,
  389.                 getRowClass : function (row, index) {
  390.                   var cls = '';
  391.                   var data = row.data;
  392.                   var d = data.priority.toLowerCase();
  393.                   switch (data.priority) {
  394.                          case 'minor' :
  395.                                 cls = 'yellow-row'
  396.                                 break;
  397.                                
  398.                          case 'release' :
  399.                                 cls = 'green-row'
  400.                                 break;
  401.                                
  402.                          case 'major' :
  403.                                 cls = 'blue-row'
  404.                                 break;
  405.                                
  406.                          case 'critical' :
  407.                                 cls = 'red-row'
  408.                                 break;  
  409.                   }
  410.                   return cls;
  411.            }
  412.         });
  413.        
  414.         this.gridAlarm = new Ext.grid.GridPanel({
  415.         store           : this.dataStore
  416.                 ,cm                     : this.columnModel
  417.                 ,sm                     : this.selectionModel
  418.                 ,title          : "Event - Alarm"
  419.         ,width          : 600
  420.         ,height         : 400
  421.                 ,stripeRows     : true
  422.                 ,loadMask       : true
  423.         ,bbar           : this.pagingToolbar
  424.                 ,view           : gridView
  425.                 ,listeners      : {
  426.                         rowclick                : doRowClick
  427.                         ,load                   : this.dataStore.load()
  428.                 }
  429.     });
  430.        
  431.         ///////////GRID ALARM END///////////
  432.        
  433.         //////////////TAB ALARM ///////////
  434.        
  435.         this.tabAlarm = new Ext.TabPanel({
  436.                 id                                      : "tabAlarm"
  437.                 ,activeTab                      : 0
  438.                 ,width                          : 700
  439.                 ,height                         : 400
  440.                 ,plain                          : false
  441.                 ,defaults                       : {
  442.                         autoScroll : true
  443.                 }
  444.                 ,layoutOnTabChange      : true
  445.                 ,defferedRender         : true 
  446.                 ,items                          : this.gridAlarm
  447.         });
  448.        
  449.         ///////////TAB ALARM END///////////
  450.     /*
  451.      * -------------------------
  452.      * Method implementations
  453.      * -------------------------
  454.      */
  455.     this.getBaseUrl = function() {
  456.         return baseUrl;
  457.     }
  458.  
  459.     this.setBaseUrl = function(_base_url) {
  460.         baseUrl = _base_url;
  461.     }
  462.  
  463.     this.getLoginForm = function() {
  464.         return this.loginForm;
  465.     }
  466.  
  467.     this.getLogoutButton = function() {
  468.         return this.logoutButton;
  469.     }
  470.  
  471.         this.objAlarm = function(){
  472.                 return this.tabAlarm;
  473.         }
  474.  
  475.         this.loadDataGridAlarm = function(){
  476.                 this.dataStore.load();
  477.         }      
  478.        
  479.     //this must be applied to a renderable object
  480.     this.renderObject = function(object, id) {
  481.         object.render(id);
  482.     }
  483.  
  484.     this.showErrorMessageBox = function(_message) {
  485.         Ext.MessageBox.show({
  486.            title: '',
  487.            msg: _message,
  488.            buttons: Ext.MessageBox.OK,
  489.            icon: Ext.MessageBox.ERROR
  490.        });
  491.     }
  492. };