/*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), templater = gadget_klass.__template_element, llogin_tooltip_template = Handlebars.compile( templater.getElementById("login-toltip-template").innerHTML ), hashCode = new Rusha().digestFromString; gadget_klass .ready(function (g) { g.props = {}; return g.getElement() .push(function (element) { g.props.element = element; g.props.deferred = RSVP.defer(); g.props.from = {page: 'main'}; g.props.login = 'login'; }); }) .declareAcquiredMethod("redirect", "redirect") .declareAcquiredMethod('getUrlFor', 'getUrlFor') .declareAcquiredMethod("getSetting", "getSetting") .declareAcquiredMethod("setSetting", "setSetting") .declareMethod("render", function (options) { var gadget = this, tmp; if (!options.url) { tmp = document.URL.split('#'); if (tmp.length > 1) { gadget.props.url = tmp[0]; } else { gadget.props.url = ""; } } else { gadget.props.url = options.url; } gadget.props.title = options.title || 'Please sign in to Monitoring'; gadget.props.root = options.root || 'UNKNOW'; return gadget.getSetting('redirect_after_login') .push(function (redirect_after_login) { if (redirect_after_login) { gadget.props.from = redirect_after_login; } gadget.props.element.querySelector('h3') .innerHTML = ' ' + gadget.props.title; return gadget.props.deferred.resolve(); }); }) ///////////////////////////////////////// // Form submit ///////////////////////////////////////// .declareService(function () { var gadget = this; return new RSVP.Queue() .push(function () { return gadget.props.deferred.promise; }) .push(function (redirect_after_login) { var text = 'Open Link', content = llogin_tooltip_template({ title: gadget.props.title, root_title: gadget.props.root, href: gadget.props.url, link_title: text }); return $('.tooltip').tooltipster({ animation: 'fade', delay: 200, theme: 'tooltipster-shadow', touchDevices: true, content: $(content), interactive: true, trigger: 'hover' }); }) .push(function () { return loopEventListener( gadget.props.element.querySelector('form'), 'submit', true, function () { var username = gadget.props.element.querySelector("input[name='username']").value, password = gadget.props.element.querySelector("input[name='password']").value; return gadget.getDeclaredGadget('login_gadget') .push(function (login_gadget) { var hash = btoa(username + ':' + password); return login_gadget.login(gadget.props.url, hash) .then(function(result) { if (result.status === 'OK') { return gadget.setSetting('redirect_after_login', '') .push(function () { /*var credential = { hash: hash, url: gadget.props.url }, hash_url = hashCode(gadget.props.url) + ''; return login_gadget.setUrlInfo(hash_url, credential);*/ return login_gadget.updateUrlValue( gadget.props.url, username, password); }) .push(function () { return gadget.redirect(gadget.props.from); }); } else { gadget.props.element.querySelector(".msg") .innerHtml = 'Invalid username or password.'; } }, function(error) { gadget.props.element.querySelector(".msg") .innerHTML ='Invalid username or password.'; }); }); } ); }); }); }(window, document, rJS, Rusha, btoa));