/*global document, window, rJS, Rusha, btoa */ /*jslint nomen: true, indent: 2, maxerr: 3*/ (function (window, document, rJS, Rusha, btoa) { "use strict"; var gadget_klass = rJS(window), hashCode = new Rusha().digestFromString; function getUrlInfo(gadget, key, default_value) { return gadget.getDeclaredGadget("jio_gadget") .push(function (jio_gadget) { return jio_gadget.get("urls"); }) .push(function (doc) { return doc[key] || default_value; }, function (error) { if (error.status_code === 404) { return default_value; } throw error; }); } function setUrlInfo(gadget, key, value) { var jio_gadget; return gadget.getDeclaredGadget("jio_gadget") .push(function (result) { jio_gadget = result; return jio_gadget.get("urls"); }) .push(undefined, function (error) { if (error.status_code === 404) { return {}; } throw error; }) .push(function (doc) { doc[key] = value; return jio_gadget.put('urls', doc); }); } function updateUrlValue(gadget, url, login, password) { var jio_gadget; return gadget.getDeclaredGadget("jio_gadget") .push(function (result) { jio_gadget = result; return jio_gadget.get("urls"); }) .push(undefined, function (error) { if (error.status_code === 404) { return {}; } throw error; }) .push(function (doc) { var key = hashCode(url) + ''; if (doc.hasOwnProperty(key)) { doc[key].hash = btoa(login + ':' + password); doc[key].url = url; } else { doc[key] = { hash: btoa(login + ':' + password), url: url, parent_url: ''}; } return jio_gadget.put('urls', doc); }); } function setUrlDict(gadget, url_dict) { var jio_gadget; return gadget.getDeclaredGadget("jio_gadget") .push(function (result) { jio_gadget = result; return jio_gadget.get("urls"); }) .push(undefined, function (error) { if (error.status_code === 404) { return {}; } throw error; }) .push(function (doc) { var key; for (key in url_dict) { doc[key] = url_dict[key]; } return jio_gadget.put('urls', doc); }); } function clearSettingFromParentUrl (gadget, parent_url) { var jio_gadget; return gadget.getDeclaredGadget("jio_gadget") .push(function (result) { jio_gadget = result; return jio_gadget.get("urls"); }) .push(undefined, function (error) { if (error.status_code === 404) { return {}; } throw error; }) .push(function (doc) { var hash; for (hash in doc) { if (doc[hash].parent_url === parent_url ) { delete doc[hash]; } } return jio_gadget.put('urls', doc); }); } gadget_klass .ready(function (g) { g.props = {}; g.props.from = {page: 'main'}; g.props.login = 'login'; }) .ready(function (g) { return g.getDeclaredGadget("jio_gadget") .push(function (jio_gadget) { return jio_gadget.createJio({ type: "indexeddb", database: "setting" }); }); }) .declareAcquiredMethod("redirect", "redirect") .declareAcquiredMethod('getUrlFor', 'getUrlFor') .declareAcquiredMethod("getSetting", "getSetting") .declareAcquiredMethod("setSetting", "setSetting") .declareMethod('login', function (url, credential_hash) { return new RSVP.Promise(function(resolve, reject){ var xhr = new XMLHttpRequest(); xhr.onload = function (event) { var response = event.target; if (response.status === 200) { resolve({status: 'OK'}); } else { reject({status: 'ERROR', msg: new Error("XHR: " + response.status + ": " + response.statusText)}); } }; xhr.onerror = function (e) { reject({status: 'ERROR', msg: e.target.status + ": " + e.target.statusText}); }; xhr.open("GET", url, true); //xhr.withCredentials = true; xhr.setRequestHeader('Authorization', 'Basic ' + credential_hash); //try { xhr.send(""); //} catch (e) { // reject({status: 'ERROR', msg: e}); //} }); }) .declareMethod('loginRedirect', function (url, options_dict, title, root_title) { var gadget = this, redirectWith, hash_url = hashCode(url) + ''; redirectWith = function (option_dict) { return gadget.setSetting('redirect_after_login', options_dict) .push(function () { return gadget.redirect({ page: gadget.props.login, url: url, title: title || '', root: root_title || '' }); }); }; return getUrlInfo(gadget, hash_url) .push(function (credential_dict) { if (!credential_dict) { credential_dict = {hash: btoa('no:password'), url: url}; } return credential_dict; }) .push(function (credential) { return gadget.login(credential.url, credential.hash) .then(function(result) { if (result.status === 'OK') { return credential; } else { return redirectWith(options_dict); } }, function(error) { return redirectWith(options_dict); }); }); }) .declareMethod('setUrlDict', function (url_dict) { var gadget = this; return setUrlDict(gadget, url_dict); }) .declareMethod('clearSettingFromParentUrl', function (parent_url) { var gadget = this; return clearSettingFromParentUrl(gadget, parent_url); }) .declareMethod('setUrlInfo', function (key, value) { var gadget = this; return setUrlInfo(gadget, key, value); }) .declareMethod('getUrlInfo', function (key, default_value) { var gadget = this; return getUrlInfo(gadget, key, default_value); }) .declareMethod('updateUrlValue', function (url, login, password) { var gadget = this; return updateUrlValue(gadget, url, login, password); }); }(window, document, rJS, Rusha, btoa));