diff options
Diffstat (limited to 'js/controllers/redirectorpage.js')
-rw-r--r-- | js/controllers/redirectorpage.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/js/controllers/redirectorpage.js b/js/controllers/redirectorpage.js new file mode 100644 index 0000000..05fbe9f --- /dev/null +++ b/js/controllers/redirectorpage.js @@ -0,0 +1,50 @@ +// This is the main controller of the page. It is responsible for showing messages, +// modal windows and loading and saving the list of redirects, that all of the +// controllers work with. +redirectorApp.controller('RedirectorPageCtrl', ['$scope', '$timeout', function($s, $timeout) { + + $s.deleting = null; //Variable for redirect being edited, of the form { index:<nr>, redirect:<redirect>}; + $s.showEditForm = $s.showDeleteForm = false; // Variables, child controllers can set them to show their forms + + var storage = chrome.storage.local; //TODO: Change to sync when Firefox supports it... + + function normalize(r) { + return new Redirect(r).toObject(); //Cleans out any extra props, and adds default values for missing ones. + } + + // Saves the entire list of redirects to storage. + $s.saveChanges = function() { + + // Clean them up so angular $$hash things and stuff don't get serialized. + var arr = $s.redirects.map(normalize); + + storage.set({redirects:arr}, function() { + console.log('Saved redirects at ' + new Date()); + }); + } + + $s.redirects = []; + storage.get('redirects', function(results) { + if (!results || !results.redirects) { + return; + } + + for (var i=0; i < results.redirects.length; i++) { + $s.redirects.push(normalize(results.redirects[i])); + } + $s.$apply(); + }); + + // Shows a message bar above the list of redirects. + $s.showMessage = function(message, success) { + $s.message = message; + $s.messageType = success ? 'success' : 'error'; + + //Remove the message in 20 seconds if it hasn't been changed... + $timeout(function() { + if ($s.message == message) { + $s.message = null; + } + }, 20 * 1000); + } +}]); |