|
| 1 | +/* 2012-05-04: merged this from chilicat to retain it for future use, but not yet activated in index.html. */ |
| 2 | +Ext.require(['*']); |
| 3 | + |
| 4 | +Ext.onReady(function () { |
| 5 | + |
| 6 | + var lastUser; |
| 7 | + |
| 8 | + function login() { |
| 9 | + var username = lastUser; |
| 10 | + var password = Ext.getCmp("password").getValue(); |
| 11 | + loginFor(username, password); |
| 12 | + } |
| 13 | + |
| 14 | + function loginFor(username, password, errorCallback) { |
| 15 | + lastUser = username; |
| 16 | + Ext.Ajax.request({ |
| 17 | + url:"/api/users/" + username + "/session", |
| 18 | + method:"POST", |
| 19 | + headers:{ |
| 20 | + "Accept":'application/json', |
| 21 | + "Content-Type":'application/json' |
| 22 | + }, |
| 23 | + jsonData:{ |
| 24 | + password:password |
| 25 | + }, |
| 26 | + success:function () { |
| 27 | + if (username == "Administrator") { |
| 28 | + window.location = "/admin/list"; |
| 29 | + } else { |
| 30 | + window.location = "/user/" + username + "/edit"; |
| 31 | + } |
| 32 | + }, |
| 33 | + failure:function (response) { |
| 34 | + if (errorCallback(response)) { |
| 35 | + Ext.MessageBox.alert('Login failed', response.statusText); |
| 36 | + } |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + function enumUsers() { |
| 42 | + Ext.Ajax.request({ |
| 43 | + scope:this, |
| 44 | + url:"/api/users/", |
| 45 | + method:'GET', |
| 46 | + headers:{ |
| 47 | + "Accept":'application/json', |
| 48 | + "Content-Type":'application/json' |
| 49 | + }, |
| 50 | + success:function (r) { |
| 51 | + var result = JSON.parse(r.responseText); |
| 52 | + var combo = Ext.getCmp("userName"); |
| 53 | + combo.getStore().loadRawData(result.users); |
| 54 | + combo.setDisabled(false); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + function createStore() { |
| 60 | + return Ext.create('Ext.data.Store', { |
| 61 | + fields:['name'] |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + function createUserGrid() { |
| 66 | + return { |
| 67 | + id:"userName", |
| 68 | + fieldLabel:'User Name', |
| 69 | + xtype:"grid", |
| 70 | + store:createStore(), |
| 71 | + margin:0, |
| 72 | + hideHeaders:true, |
| 73 | + border:0, |
| 74 | + height:100, |
| 75 | + columns:[ |
| 76 | + { header:'Name', dataIndex:'name', flex:1} |
| 77 | + ], |
| 78 | + name:'user', |
| 79 | + displayField:'name', |
| 80 | + valueField:'name', |
| 81 | + queryMode:"local", |
| 82 | + |
| 83 | + editable:false, |
| 84 | + autoSelect:false, |
| 85 | + forceSelection:false, |
| 86 | + |
| 87 | + |
| 88 | + allowBlank:false, |
| 89 | + listeners:{ |
| 90 | + afterRender:function (c) { |
| 91 | + enumUsers(); |
| 92 | + }, |
| 93 | + itemclick:function (view, record) { |
| 94 | + var username = record.data.name; |
| 95 | + loginFor(username, "", function (response) { |
| 96 | + if (response.status == 401) { |
| 97 | + // user has password. Show password field. |
| 98 | + Ext.getCmp("box").getLayout().setActiveItem(1); |
| 99 | + Ext.getCmp("password").focus(); |
| 100 | + // prevent default handling. |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + // allow default handling. |
| 105 | + return true; |
| 106 | + }); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + function createPasswordPanel() { |
| 114 | + return Ext.create("Ext.form.Panel", { |
| 115 | + |
| 116 | + layout: 'anchor', |
| 117 | + border: 0, |
| 118 | + |
| 119 | + defaults: { |
| 120 | + anchor: '100%' |
| 121 | + }, |
| 122 | + height:100, |
| 123 | + defaultType:'textfield', |
| 124 | + padding: "0 0 4 0", |
| 125 | + |
| 126 | + items:[ |
| 127 | + { |
| 128 | + inputType:"password", |
| 129 | + id:"password", |
| 130 | + fieldLabel:'Password', |
| 131 | + name:'password', |
| 132 | + margin: 6, |
| 133 | + |
| 134 | + listeners:{ |
| 135 | + scope:this, |
| 136 | + specialkey:function (f, e) { |
| 137 | + if (e.getKey() == e.ENTER) { |
| 138 | + login(); |
| 139 | + } else if (e.getKey() == e.ESC) { |
| 140 | + Ext.getCmp("box").getLayout().setActiveItem(0); |
| 141 | + } |
| 142 | + } |
| 143 | + }, |
| 144 | + |
| 145 | + allowBlank:false |
| 146 | + } |
| 147 | + ] , |
| 148 | + |
| 149 | + |
| 150 | + buttons:[ |
| 151 | + { |
| 152 | + text:'Back', |
| 153 | + id:"backButton", |
| 154 | + handler:function () { |
| 155 | + Ext.getCmp("box").getLayout().setActiveItem(0); |
| 156 | + } |
| 157 | + }, |
| 158 | + { |
| 159 | + text:'Login', |
| 160 | + id:"loginButton", |
| 161 | + disabled:false, |
| 162 | + handler:function () { |
| 163 | + login(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + ] |
| 168 | + }); |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | + Ext.create('Ext.container.Viewport', { |
| 173 | + layout:{ |
| 174 | + type:'vbox', |
| 175 | + align:'center' |
| 176 | + }, |
| 177 | + id:'root', |
| 178 | + |
| 179 | + items:[ |
| 180 | + { |
| 181 | + xtype:"panel", |
| 182 | + margin:10, |
| 183 | + border:0, |
| 184 | + layout:{ |
| 185 | + type:'vbox', |
| 186 | + align:'center' |
| 187 | + }, |
| 188 | + items:[ |
| 189 | + Ext.create('Ext.Img', { |
| 190 | + border:0, |
| 191 | + src:'/header.gif' |
| 192 | + }) |
| 193 | + ], |
| 194 | + flex:1, |
| 195 | + width:700 |
| 196 | + }, |
| 197 | + |
| 198 | + { |
| 199 | + xtype:"panel", |
| 200 | + layout:"card", |
| 201 | + id:"box", |
| 202 | + |
| 203 | + bodyPadding:0, |
| 204 | + width:300, |
| 205 | + title:"Welcome to Scalatron", |
| 206 | + |
| 207 | + items:[ |
| 208 | + createUserGrid(), |
| 209 | + createPasswordPanel() |
| 210 | + ] |
| 211 | + }, |
| 212 | + { |
| 213 | + flex:1, |
| 214 | + border:0 |
| 215 | + } |
| 216 | + ] |
| 217 | + }); |
| 218 | +}); |
| 219 | + |
0 commit comments