PartKeeprMobile.js (7982B)
1 Ext.application({ 2 name: 'PartKeepr', 3 4 launch: function () { 5 Ext.get("loader-wrapper").hide(); 6 7 PartKeepr.application = this; 8 9 var authenticationProvider = Ext.create(window.parameters.authentication_provider); 10 PartKeepr.Auth.AuthenticationProvider.setAuthenticationProvider(authenticationProvider); 11 12 var config = {}; 13 14 if (window.parameters.autoLoginUsername) { 15 config.autoLogin = true; 16 config.autoLoginUsername = window.parameters.autoLoginUsername; 17 config.autoLoginPassword = window.parameters.autoLoginPassword; 18 } 19 20 this.loginManager = Ext.create("PartKeepr.Auth.LoginManager", config); 21 this.loginManager.on("login", this.onLogin, this); 22 this.loginManager.on("logout", this.onLogout, this); 23 24 this.loginManager.login(); 25 }, 26 27 test: function () { 28 29 var panel = Ext.create({xtype: "PartPanel"}); 30 var partDetails = Ext.create({xtype: "PartDetails", hidden: true}); 31 32 Ext.Viewport.add(panel); 33 Ext.Viewport.add(partDetails); 34 35 36 }, 37 getLoginManager: function () 38 { 39 return this.loginManager; 40 }, 41 /** 42 * Queries for a specific user preference. Returns either the value or a default value if 43 * the preference was not found. 44 * @param key The key to query 45 * @param defaultValue A default value to return (optional) 46 * @returns the key value, or defaultValue if preference key was not found 47 */ 48 getUserPreference: function (key, defaultValue) 49 { 50 var record = this.userPreferenceStore.findRecord("preferenceKey", key); 51 52 if (record) { 53 var value = record.get("preferenceValue"); 54 var decodedValue = Ext.decode(value, true); 55 56 if (decodedValue === null) { 57 return value; 58 } else { 59 return decodedValue; 60 } 61 } else { 62 return (typeof defaultValue == "undefined") ? null : defaultValue; 63 } 64 }, 65 /** 66 * Sets a specific user preference. Directly commits the change to the server. 67 * 68 * @param key The key to set 69 * @param value The value to set 70 */ 71 setUserPreference: function (key, value) 72 { 73 var record = this.userPreferenceStore.findRecord("preferenceKey", key); 74 value = Ext.encode(value); 75 76 if (record) { 77 if (record.get("preferenceValue") != value) { 78 record.set("preferenceValue", value); 79 record.save(); 80 } 81 } else { 82 var j = new PartKeepr.AuthBundle.Entity.UserPreference(); 83 j.set("preferenceKey", key); 84 j.set("preferenceValue", value); 85 j.save(); 86 this.userPreferenceStore.add(j); 87 } 88 }, 89 getUserPreferenceStore: function () 90 { 91 return this.userPreferenceStore; 92 }, 93 94 /** 95 * Handles the login function. Initializes the part manager window, 96 * enables the menu bar and creates the stores+loads them. 97 */ 98 onLogin: function () { 99 this.createGlobalStores(); 100 101 var initialUserPreferences = Ext.decode(this.getLoginManager().getUser().get("initialUserPreferences")); 102 103 var records = this.getUserPreferenceStore().getProxy().getReader().read(initialUserPreferences); 104 105 this.getUserPreferenceStore().loadRecords(records.records); 106 107 this.test(); 108 }, 109 110 onLogout: function () 111 { 112 this.menuBar.disable(); 113 this.centerPanel.removeAll(true); 114 this.getStatusbar().setDisconnected(); 115 116 Ext.TaskManager.stop(this.unacknowledgedNoticesTask); 117 }, 118 119 createGlobalStores: function () 120 { 121 this.footprintStore = Ext.create("Ext.data.Store", 122 { 123 model: 'PartKeepr.FootprintBundle.Entity.Footprint', 124 pageSize: 99999999, 125 autoLoad: true 126 }); 127 128 this.siPrefixStore = Ext.create("Ext.data.Store", 129 { 130 model: 'PartKeepr.SiPrefixBundle.Entity.SiPrefix', 131 pageSize: 99999999, 132 autoLoad: true 133 }); 134 135 this.currencyStore = Ext.create("PartKeepr.Data.Store.CurrencyStore", { 136 autoLoad: true 137 }); 138 139 this.distributorStore = Ext.create("Ext.data.Store", 140 { 141 model: 'PartKeepr.DistributorBundle.Entity.Distributor', 142 pageSize: 99999999, 143 autoLoad: true 144 }); 145 146 this.manufacturerStore = Ext.create("Ext.data.Store", 147 { 148 model: 'PartKeepr.ManufacturerBundle.Entity.Manufacturer', 149 pageSize: 99999999, 150 autoLoad: true 151 }); 152 153 this.partUnitStore = Ext.create("Ext.data.Store", 154 { 155 model: 'PartKeepr.PartBundle.Entity.PartMeasurementUnit', 156 pageSize: 99999999, 157 autoLoad: true 158 }); 159 160 this.unitStore = Ext.create("Ext.data.Store", 161 { 162 model: 'PartKeepr.UnitBundle.Entity.Unit', 163 pageSize: 99999999, 164 autoLoad: true 165 }); 166 167 this.userStore = Ext.create("Ext.data.Store", 168 { 169 model: 'PartKeepr.AuthBundle.Entity.User', 170 pageSize: 99999999, 171 autoLoad: true 172 }); 173 174 this.userPreferenceStore = Ext.create("PartKeepr.data.store.UserPreferenceStore", 175 { 176 model: 'PartKeepr.AuthBundle.Entity.UserPreference', 177 autoLoad: false 178 }); 179 180 181 }, 182 getSystemPreferenceStore: function () 183 { 184 return this.systemPreferenceStore; 185 }, 186 /** 187 * Queries for a specific system preference. Returns either the value or a default value if 188 * the preference was not found. 189 * @param key The key to query 190 * @param defaultValue A default value to return (optional) 191 * @returns the key value, or defaultValue if preference key was not found 192 */ 193 getSystemPreference: function (key, defaultValue) 194 { 195 if (this.systemPreferenceStore === undefined) { 196 return defaultValue; 197 } 198 var record = this.systemPreferenceStore.findRecord("preferenceKey", key); 199 200 if (record) { 201 var value = record.get("preferenceValue"); 202 var decodedValue = Ext.decode(value, true); 203 204 if (decodedValue === null) { 205 return value; 206 } else { 207 return decodedValue; 208 } 209 } else { 210 return (typeof defaultValue == "undefined") ? null : defaultValue; 211 } 212 }, 213 /** 214 * Sets a specific system preference. Directly commits the change to the server. 215 * 216 * @param key The key to set 217 * @param value The value to set 218 */ 219 setSystemPreference: function (key, value) 220 { 221 var record = this.systemPreferenceStore.findRecord("preferenceKey", key); 222 value = Ext.encode(value); 223 224 if (record) { 225 if (record.get("preferenceValue") != value) { 226 record.set("preferenceValue", value); 227 record.save(); 228 } 229 } else { 230 var j = new PartKeepr.SystemPreferenceBundle.Entity.SystemPreference(); 231 j.set("preferenceKey", key); 232 j.set("preferenceValue", value); 233 j.save(); 234 this.systemPreferenceStore.add(j); 235 } 236 }, 237 }); 238 239 PartKeepr.getBasePath = function () 240 { 241 var href = document.getElementsByTagName('base')[0].href; 242 243 244 if (href.substr(-2) === '//') { 245 return href.substr(0, href.length - 2); 246 } 247 248 if (href.substr(-1) === '/') { 249 return href.substr(0, href.length - 1); 250 } 251 252 return href; 253 }; 254 255 /** 256 * <p>This static method returns the instance of the application.</p> 257 * @return {PartKeepr} The application 258 */ 259 PartKeepr.getApplication = function () 260 { 261 return PartKeepr.application; 262 };