|
| 1 | +// CardboardCtrl = main controller with settings & header |
| 2 | +cardboard.controller('CardboardCtrl', ['$scope','$location','SettingsFactory','SearchFactory','AppFactory','PermissionFactory','ToolFactory', function($scope, $location, sf, schf, af, pf, tf){ |
| 3 | + |
| 4 | + // Get SyncArea settings |
| 5 | + sf.update().then(function(update){ |
| 6 | + $scope.settings = update.content; |
| 7 | + $scope.checkPermissions(); |
| 8 | + $scope.update = update.state; |
| 9 | + if($scope.update == 1) |
| 10 | + $location.path("welcome"); |
| 11 | + }); |
| 12 | + |
| 13 | + // Get bgDataUrl from LocalArea storage |
| 14 | + sf.getLocal("bgDataUrl").then(function(value){ |
| 15 | + // Avoid non-blocking/minors errors at load (because settings are not yet defined) |
| 16 | + if($scope.settings){ |
| 17 | + $scope.settings.bgDataUrl = value; |
| 18 | + |
| 19 | + // if no local bg, delete the matching record and put back to default bg |
| 20 | + if(!value && ($scope.settings.backgrounds[$scope.settings.backgroundId].type == "Custom")){ |
| 21 | + $scope.settings.backgrounds.pop(); |
| 22 | + $scope.settings.backgroundId = 4; |
| 23 | + $scope.background = $scope.settings.backgrounds[4]; |
| 24 | + sf.set({backgroundId : 4}); |
| 25 | + sf.set({backgrounds : $scope.settings.backgrounds}); |
| 26 | + alert("No custom background found here. Back to default"); |
| 27 | + } |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + |
| 32 | + // Why the hell do I need this function ? ==> fix this |
| 33 | + $scope.dismiss = function(){ |
| 34 | + $scope.update = 0; |
| 35 | + }; |
| 36 | + |
| 37 | + $scope.checkPermissions = function(){ |
| 38 | + // Allows cards based on settings and permissions |
| 39 | + for(var i in $scope.settings.cards){ |
| 40 | + // Closure: Yo dawg we heard yo' like functions... So we put a function in yo' function: http://www.apaxsoftware.com/2012/05/common-javascript-mistakes-loops-and-callbacks/ |
| 41 | + (function(i_copy){ |
| 42 | + pf.verify($scope.settings.cards[i_copy].permissions).then(function(granted){ |
| 43 | + if(granted) |
| 44 | + $scope.settings.cards[i_copy].allowed = true; |
| 45 | + else |
| 46 | + $scope.settings.cards[i_copy].allowed = false; |
| 47 | + }); |
| 48 | + }(i)); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + $scope.goTo = function(url){ |
| 53 | + chrome.tabs.update({url:url}); |
| 54 | + }; |
| 55 | + $scope.getHeaderStyle = function(noLogo){ |
| 56 | + var style = {}; |
| 57 | + var bgUrl; |
| 58 | + var bgImg = true; |
| 59 | + // Avoid non-blocking/minors errors at load (because settings are not yet defined) |
| 60 | + if($scope.settings){ |
| 61 | + // if noLogo is true don't display google logo |
| 62 | + var logo = (!noLogo ? $scope.settings.backgroundLogo : false); |
| 63 | + var customBg = ($scope.settings.backgrounds[$scope.settings.backgroundId].type == "Custom"); |
| 64 | + if(customBg) |
| 65 | + if($scope.settings.bgDataUrl) |
| 66 | + bgUrl = $scope.settings.bgDataUrl; |
| 67 | + else // if undefined (due to speed of exec) background-image = none |
| 68 | + bgImg = false; |
| 69 | + else |
| 70 | + bgUrl = $scope.settings.backgrounds[$scope.settings.backgroundId].url+"_"+tf.getTime()+".webp"; |
| 71 | + |
| 72 | + if(bgImg) |
| 73 | + style.backgroundImage = "url("+(logo ? "../img/google.webp), url(":"")+bgUrl+")"; |
| 74 | + else |
| 75 | + style.backgroundImage = "none"; |
| 76 | + style.backgroundPosition = (logo ? "center 25px,":"")+$scope.settings.backgroundPosition; |
| 77 | + style.backgroundSize = (logo ? "280px,":"")+"cover"; |
| 78 | + } |
| 79 | + return style; |
| 80 | + }; |
| 81 | + $scope.suggestions = function(input){ |
| 82 | + // suggestions for typeahead |
| 83 | + return schf.getSuggestions(input); |
| 84 | + }; |
| 85 | + $scope.onSelect = function (item) { |
| 86 | + switch(item.type){ |
| 87 | + case "app": |
| 88 | + af.launchApp(item.data.id); |
| 89 | + break; |
| 90 | + case "history": |
| 91 | + case "bookmark": |
| 92 | + this.goTo(item.data.url); |
| 93 | + break; |
| 94 | + case "web": |
| 95 | + default: |
| 96 | + this.search(); |
| 97 | + }; |
| 98 | + }; |
| 99 | + $scope.search = function(){ |
| 100 | + // Process the google search |
| 101 | + if(this.query && this.query.length>0){ |
| 102 | + this.isLoading = true; |
| 103 | + this.goTo(this.settings.searchEngine + this.query); |
| 104 | + } |
| 105 | + }; |
| 106 | +}]); |
| 107 | + |
| 108 | +// AppCtrl |
| 109 | +cardboard.controller('AppCtrl', ['$scope','AppFactory', function($scope, af){ |
| 110 | + af.getApps().then(function(apps){ |
| 111 | + $scope.apps = apps; |
| 112 | + }); |
| 113 | + $scope.launch = function(){ |
| 114 | + af.launchApp(this.app); |
| 115 | + }; |
| 116 | + $scope.viewPermissions = function(){ |
| 117 | + this.permissions = af.getPermissions(this.app.id); |
| 118 | + }; |
| 119 | + $scope.uninstall = function(){ |
| 120 | + if(confirm("Uninstall "+this.app.name+" ?")){ |
| 121 | + af.uninstall(this.app.id); |
| 122 | + $scope.apps.splice(this.key,1); |
| 123 | + } |
| 124 | + }; |
| 125 | + $scope.getIcon = function(){ |
| 126 | + var icon_url; |
| 127 | + if(this.app.icons) |
| 128 | + icon_url = this.app.icons[this.app.icons.length-1].url; |
| 129 | + else |
| 130 | + icon_url = "chrome://extension-icon/khopmbdjffemhegeeobelklnbglcdgfh/256/1"; |
| 131 | + if(!this.app.enabled) |
| 132 | + icon_url+="?grayscale=true"; |
| 133 | + return icon_url; |
| 134 | + }; |
| 135 | +}]); |
| 136 | + |
| 137 | +// BookmarkCtrl |
| 138 | +cardboard.controller('BookmarkCtrl', ['$scope','BookmarkFactory', function($scope, bf){ |
| 139 | + bf.getRecent(5).then(function(bookmarks){ |
| 140 | + $scope.bookmarks = bookmarks; |
| 141 | + }); |
| 142 | +}]); |
| 143 | + |
| 144 | +// DownloadCtrl |
| 145 | +cardboard.controller('DownloadCtrl', ['$scope','DownloadFactory', function($scope, df){ |
| 146 | + df.getRecent(5).then(function(downloads){ |
| 147 | + $scope.downloads = downloads; |
| 148 | + }); |
| 149 | + // Watch changes in download states and apply them to the model |
| 150 | + chrome.downloads.onChanged.addListener(function(downloadDelta){ |
| 151 | + for (i in $scope.downloads){ |
| 152 | + if($scope.downloads[i].id == downloadDelta.id){ |
| 153 | + for (j in downloadDelta) |
| 154 | + if(j != "id") |
| 155 | + $scope.downloads[i][j] = downloadDelta[j].current; |
| 156 | + $scope.$apply(); |
| 157 | + break; |
| 158 | + } |
| 159 | + } |
| 160 | + }); |
| 161 | + // whatch if a download is created and add it to the card |
| 162 | + chrome.downloads.onCreated.addListener(function(downloadCreated){ |
| 163 | + $scope.$apply(function(){ |
| 164 | + $scope.downloads.pop(); // remove the last element |
| 165 | + $scope.downloads.unshift(downloadCreated); // add the newly created at the beggining |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + $scope.show = function(){ |
| 170 | + df.show(this.download.id); |
| 171 | + }; |
| 172 | + $scope.open = function(){ |
| 173 | + df.open(this.download.id); |
| 174 | + }; |
| 175 | + $scope.pause = function(){ |
| 176 | + df.pause(this.download.id); |
| 177 | + }; |
| 178 | + $scope.resume = function(){ |
| 179 | + df.resume(this.download.id); |
| 180 | + }; |
| 181 | + $scope.cancel = function(){ |
| 182 | + df.cancel(this.download.id); |
| 183 | + }; |
| 184 | + $scope.retry = function(){ |
| 185 | + df.download(this.download.url); |
| 186 | + }; |
| 187 | +}]); |
| 188 | + |
| 189 | + |
| 190 | +// QuickSettingsCtrl |
| 191 | +cardboard.controller('QuickSettingsCtrl', ['$scope','QuickSettingsFactory', function($scope, qsf){ |
| 192 | + $scope.isWorking = false; |
| 193 | + |
| 194 | + $scope.clearCache = function(){ |
| 195 | + $scope.isWorking = true; |
| 196 | + qsf.clearCache().then(function(){ $scope.isWorking = false;}); |
| 197 | + }; |
| 198 | + $scope.clearCookies = function(){ |
| 199 | + if(confirm("Clear Cookies?")){ |
| 200 | + $scope.isWorking = true; |
| 201 | + qsf.clearCookies().then(function(){$scope.isWorking = false;}); |
| 202 | + } |
| 203 | + }; |
| 204 | + $scope.clearHistory = function(){ |
| 205 | + if(confirm("Clear Browser History?")){ |
| 206 | + $scope.isWorking = true; |
| 207 | + qsf.clearHistory().then(function(){$scope.isWorking = false;}); |
| 208 | + } |
| 209 | + }; |
| 210 | + $scope.clearLocalStorage = function(){ |
| 211 | + if(confirm("Clear Local Storage?")){ |
| 212 | + $scope.isWorking = true; |
| 213 | + qsf.clearLocalStorage().then(function(){$scope.isWorking = false;}); |
| 214 | + } |
| 215 | + }; |
| 216 | +}]); |
| 217 | + |
| 218 | +// TopSitesCtrl |
| 219 | +cardboard.controller('TopSitesCtrl', ['$scope','TopSitesFactory', function($scope, tsf){ |
| 220 | + tsf.get(5).then(function(topSites){ |
| 221 | + $scope.topSites = topSites; |
| 222 | + }); |
| 223 | +}]); |
| 224 | + |
| 225 | +// StorageCtrl |
| 226 | +cardboard.controller('StorageCtrl', ['$scope','StorageFactory', function($scope, sf){ |
| 227 | + sf.getInfo().then(function(storageUnits){ |
| 228 | + $scope.storageUnits = storageUnits; |
| 229 | + }); |
| 230 | +}]); |
| 231 | + |
| 232 | +/******************************************************************/ |
| 233 | +/* SETTINGS */ |
| 234 | +/******************************************************************/ |
| 235 | + |
| 236 | + |
| 237 | +cardboard.controller('AppearanceCtrl', ['$scope','SettingsFactory', function($scope, sf){ |
| 238 | + $scope.background; |
| 239 | + $scope.updateBg = function(){ |
| 240 | + $scope.settings.backgroundId = this.background.id; |
| 241 | + sf.set({backgroundId : this.background.id}); |
| 242 | + }; |
| 243 | + $scope.updateBgPos = function(){ |
| 244 | + sf.set({backgroundPosition : this.settings.backgroundPosition}); |
| 245 | + }; |
| 246 | + $scope.updateBgLogo = function(){ |
| 247 | + sf.set({backgroundLogo : this.settings.backgroundLogo}); |
| 248 | + }; |
| 249 | +}]); |
| 250 | + |
| 251 | +cardboard.controller('CardsCtrl', ['$scope','SettingsFactory','PermissionFactory', function($scope, sf, pf){ |
| 252 | + $scope.updateCards = function(){ |
| 253 | + // delete the allow property for security measure (even if permissions are verified at load) |
| 254 | + // Plus it saves unnecessary bits from sync storage |
| 255 | + delete this.settings.cards.allowed; |
| 256 | + sf.set({cards:this.settings.cards}); |
| 257 | + }; |
| 258 | + $scope.request = function(){ |
| 259 | + var cardId = this.key; |
| 260 | + if(!this.card.allowed) |
| 261 | + pf.request(this.card.permissions).then(function(granted){ |
| 262 | + if(granted) |
| 263 | + $scope.settings.cards[cardId].allowed = true; |
| 264 | + }); |
| 265 | + else |
| 266 | + console.log("already granted"); |
| 267 | + }; |
| 268 | + $scope.revokeAll = function(){ |
| 269 | + // revoke the cards permissions |
| 270 | + var warning = "Be careful, this will revoke all permissions for this extension."; |
| 271 | + warning += "\nThis will disable cards that needs authorization until you grant corresponding permission again."; |
| 272 | + warning += "\nIMPORTANT : Due to a odd chrome behavior you won't see any confirm popup next time you grant permissions."; |
| 273 | + warning += "\nMore details here: https://plus.google.com/115967816314012668475/posts/VepEbyyw7yf"; |
| 274 | + |
| 275 | + if(confirm(warning)) |
| 276 | + for(i in $scope.settings.cards){ |
| 277 | + (function(i_copy){ |
| 278 | + pf.revoke($scope.settings.cards[i_copy].permissions).then(function(revoked){ |
| 279 | + if(revoked) |
| 280 | + $scope.settings.cards[i_copy].allowed = false; |
| 281 | + else |
| 282 | + console.log("Error while revoking permission"); |
| 283 | + }); |
| 284 | + }(i)); |
| 285 | + } |
| 286 | + }; |
| 287 | +}]); |
| 288 | + |
| 289 | +cardboard.controller('WelcomeCtrl', ['$scope', function($scope){ |
| 290 | + $scope.nextCard = 0; |
| 291 | + $scope.next = function(){ |
| 292 | + $scope.nextCard++; |
| 293 | + }; |
| 294 | +}]); |
0 commit comments