/*global window, rJS, btoa, RSVP, $, Rusha */ /*jslint nomen: true, indent: 2, maxerr: 3*/ (function (window, rJS, RSVP, $, btoa, Rusha) { "use strict"; var gadget_klass = rJS(window), templater = gadget_klass.__template_element, opml_url_template = Handlebars.compile( templater.getElementById("template-opmlurl-list").innerHTML ), hashCode = new Rusha().digestFromString; function validateHttpUrl(value) { if (/\(?(?:(http|https):\/\/)(?:((?:[^\W\s]|\.|-|[:]{1})+)@{1})?((?:www.)?(?:[^\W\s]|\.|-)+[\.][^\W\s]{2,4}|localhost(?=\/)|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\[[\dabcedf:]+\])(?::(\d*))?([\/]?[^\s\?]*[\/]{1})*(?:\/?([^\s\n\?\[\]\{\}\#]*(?:(?=\.)){1}|[^\s\n\?\[\]\{\}\.\#]*)?([\.]{1}[^\s\?\#]*)?)?(?:\?{1}([^\s\n\#\[\]]*))?([\#][^\s\n]*)?\)?/i.test(value)) { return true; } return false; } function formatDate(d){ function addZero(n){ return n < 10 ? '0' + n : '' + n; } return d.getFullYear() + "-" + addZero(d.getMonth()+1) + "-" + addZero(d.getDate()) + " " + addZero(d.getHours()) + ":" + addZero(d.getMinutes()) + ":" + addZero(d.getMinutes()); } function setUrlConfiguration(gadget, url, title) { return gadget.getSetting('monitor_url_description') .push(function (url_description_dict) { var i, tmp, tmp_dict; if (url_description_dict === undefined) { url_description_dict = {}; } tmp = hashCode(url); url_description_dict[tmp] = {href: url, title: title}; return gadget.setSetting('monitor_url_description', url_description_dict); }); } function loadUrlFromOPML(gadget, url) { var text_content = gadget.props.element.querySelector('form.opml button.show') .textContent; return new RSVP.Queue() .push(function () { return $("article.configure-b").addClass("ui-content-hidden"); }) .push(function () { gadget.props.element.querySelector('form.opml button.show') .textContent = "Loading..."; }) .push(function () { var listbox_configuration = { enable_search: false, disable_href: true, column_list: [{ title: 'Software Instance', select: 'title' }, { title: 'Hosting Subscription', select: 'opml_title' }, { select: 'htmlurl', title: 'URL' }, { select: 'type', title: 'URL Type' }], query: { select_list: ['title', 'opml_title', 'htmlurl', 'type', 'url'], sort_on: [["opml_title", "ascending"]] }, replicate: false }, jio_options = { type: "query", sub_storage: { type: "feed", feed_type: 'opml', url: url } }; listbox_configuration.storage_list = [jio_options]; return gadget.props.listbox.render(listbox_configuration); }) .push(function () { gadget.props.element.querySelector('form.opml button.show') .textContent = text_content; if (gadget.props.listbox.property_dict.data_result.length > 0) { return $("article.configure-b").removeClass("ui-content-hidden"); } else { $(gadget.props.element.querySelector('.opml .ui-text-error')).fadeIn(400); gadget.props.element.querySelector('.opml .ui-text-error') .innerHTML = "ERROR: Failed to load URL!"; } //return $(gadget.props.element.querySelector(".configure-b")).show(); }, function(error) { gadget.props.element.querySelector('form.opml button.show') .textContent = text_content; $(gadget.props.element.querySelector('.opml .ui-text-error')).fadeIn(400); gadget.props.element.querySelector('.opml .ui-text-error') .innerHTML = "ERROR " + (error.target.status || '') + "Failed to load URL!"; console.log(error); }); } function loginFromLocalCredential(gadget, url) { /* Try to login from registered credential */ return gadget.props.login_gadget.getUrlInfo( hashCode(url) ) .push(function (credential_dict) { if (!credential_dict) { return {}; } return credential_dict; }) .push(function (credential) { if (credential) { return gadget.props.login_gadget.login(credential.url, credential.hash) .then(function(result) { if (result.status === 'OK') { return credential; } else { return undefined; } }, function(error) { return undefined; }); } else { return undefined; } }); } function checkCredential(gadget, url, hash) { // Verify if login and password are correct for this URL return gadget.props.login_gadget.login(url, hash) .then(function(result) { if (result.status === 'OK') { return {status: 'OK'}; } else { return loginFromLocalCredential(gadget, url) .push(function (result) { if (result) { return { status: 'OK', hash: result.hash, login: result.login, url: url}; } return {status: 'KO', msg: error.msg + ' (' + url + ')'}; }); } }, function(error) { return loginFromLocalCredential(gadget, url) .push(function (result) { if (result) { return { status: 'OK', hash: result.hash, login: result.login, url: url}; } return {status: 'KO', msg: error.msg + ' (' + url + ')'}; }); }); } function changeMonitorPassword(gadget, credential_dict, url_key, password) { var url, jio_gadget, jio_options; url = credential_dict.url; url += (url.endsWith('/') ? '':'/') + 'config/'; jio_options = { type: "query", sub_storage: { type: "drivetojiomapping", sub_storage: { type: "dav", url: url, basic_login: credential_dict.hash } } }; gadget.props.gindex += 1; return gadget.declareGadget("gadget_monitoring_jio.html", { //element: gadget.props.element, scope: 'jio_' + gadget.props.gindex + "_gadget", sandbox: "public" } ).push(function(new_gadget) { jio_gadget = new_gadget; jio_gadget.createJio(jio_options, false); return jio_gadget.get('config'); }) .push(function (doc) { var i; if (doc) { for (i = 0; i < doc.length; i += 1) { if (doc[i].key === 'monitor-password') { doc[i].value = password; return jio_gadget.put('config.tmp', doc); } } } return new Error("Cannot get document at : " % url); }) .push(function (result) { return {status: 'OK', key: url_key}; }, function (error) { console.log(error); return {status: 'ERROR', code: error.target.status, key: url_key}; }); } function testOPmlUrl(gadget, url, title) { var jio_options = { type: "query", sub_storage: { type: "feed", feed_type: 'opml', url: url } }; gadget.props.jio_gadget.createJio(jio_options, false); return gadget.props.jio_gadget.allDocs({}) .push(function (doc) { if (!doc) { gadget.props.element.querySelector('.msgtext-box') .innerHTML += "" + url + " [ " + title + " ] is not reachable!
"; return false; } else { return true; } }); } gadget_klass .ready(function (g) { g.props = {}; return g.getElement() .push(function (element) { g.props.element = element; g.props.deferred = RSVP.defer(); g.props.urls = []; g.props.gindex = 0; }); }) .ready(function (gadget) { return gadget.getDeclaredGadget("login_gadget") .push(function (login_gadget) { gadget.props.login_gadget = login_gadget; }); }) .ready(function (gadget) { return gadget.getDeclaredGadget("listbox") .push(function (listbox_gadget) { gadget.props.listbox = listbox_gadget; }); }) .ready(function (gadget) { return gadget.getDeclaredGadget("jio_gadget") .push(function (jio_gadget) { gadget.props.jio_gadget = jio_gadget; }); }) .ready(function (gadget) { return gadget.getDeclaredGadget("sync_gadget") .push(function (sync_gadget) { gadget.props.sync_gadget = sync_gadget; }); }) .declareAcquiredMethod("updateHeader", "updateHeader") .declareAcquiredMethod("redirect", "redirect") .declareAcquiredMethod("reload", "reload") .declareAcquiredMethod("getSetting", "getSetting") .declareAcquiredMethod("setSetting", "setSetting") .declareMethod("render", function (options) { var gadget = this; return gadget.updateHeader({ title: "Configure monitoring Instances"/*, back_url: "#page=main", panel_action: false*/ }) .push(function () { if (options.url !== undefined && options.url !== '') { gadget.props.element.querySelector("input[name='url']").value = options.url; } if (options.username !== undefined && options.username !== '' && options.password !== undefined && options.password !== '') { gadget.props.username = options.username; gadget.props.password = options.password; gadget.props.element.querySelector("input[name='username']").value = options.username; gadget.props.element.querySelector("input[name='password']").value = options.password; } return gadget.getSetting('monitor_url_description'); }) .push(function (url_description_dict) { var opml_list = [], content, key; if (url_description_dict !== undefined) { for (key in url_description_dict) { opml_list.push({ key: key, href: "#page=settings_configurator&url=" + url_description_dict[key].href, link: url_description_dict[key].href, title: url_description_dict[key].title || '' }); } } content = opml_url_template({opml_list: opml_list}); gadget.props.element.querySelector("#config-manage .opml-tablelinks > tbody") .innerHTML = content; return gadget.getSetting('latest_sync_time'); }) .push(function (latest_sync_time) { if (latest_sync_time !== undefined) { gadget.props.element.querySelector(".last-sync") .innerHTML = formatDate(new Date(latest_sync_time)); } else { gadget.props.element.querySelector(".last-sync") .innerHTML = '--'; } }) .push(function () { if (!options.tab || options.tab !== 'manage') { options.tab = 'add'; } gadget.props.selected = options.tab; return gadget.props.deferred.resolve(); }); }) ///////////////////////////////////////// // Form submit ///////////////////////////////////////// .declareService(function () { var gadget = this, sync_checkbox_list, i; function setSyncTimerInterval(element) { var timer; if ($(element).prop('checked')) { timer = parseInt($(element).val(), 10); console.log(timer); if (timer && !isNaN(timer)) { return new RSVP.Queue() .push(function () { return gadget.setSetting('sync_data_interval', timer); }); } } } function setSelectSyncTime(time_interval) { var element_id = "#sync-data-"; if (time_interval === 300000) { element_id += "5m"; } else if (time_interval === 600000) { element_id += "10m"; } else if (time_interval === 1200000) { element_id += "20m"; } else if (time_interval === 1800000) { element_id += "30m"; } else if (time_interval === 3600000) { element_id += "1h"; } $(element_id).prop('checked', true); return $(gadget.props.element.querySelector(".sync-interval-controlgroup")) .controlgroup().controlgroup("refresh"); } function loadOPMLContent () { var url = gadget.props.element.querySelector("input[name='url']").value; gadget.props.element.querySelector('.opml .ui-text-error') .innerHTML = ""; $(gadget.props.element.querySelector('.opml .ui-text-error')).fadeOut(400); if (url && validateHttpUrl(url)) { gadget.props.opml_url = url; return loadUrlFromOPML(gadget, url); } else { $(gadget.props.element.querySelector('.opml .ui-text-error')).fadeIn(400); gadget.props.element.querySelector('.opml .ui-text-error') .innerHTML = "'" + url + "' is not a valid URL"; } } return new RSVP.Queue() .push(function () { return gadget.props.deferred.promise; }) .push(function () { if (gadget.props.username && gadget.props.password) { return loadOPMLContent(); } }) .push(function () { return $(gadget.props.element.querySelector("a[href='#config-" + gadget.props.selected + "']")).trigger('click'); }) .push(function () { return gadget.getSetting('sync_data_interval'); }) .push(function (time_interval) { return setSelectSyncTime(time_interval); }) .push(function () { var text = "Give username and password that you have.
For each Monitor, " + "credentials will be saved only if Login/password are valid.
" + "For simple use, configure instances with the " + "same password."; return $('.tooltip').tooltipster({ animation: 'fade', delay: 200, theme: 'tooltipster-shadow', touchDevices: true, content: $("

## Monitors Private Access

" + text + "

"), interactive: true, trigger: 'hover' }); }) .push(function () { var promise_list = []; promise_list.push(loopEventListener( gadget.props.element.querySelector('form.opml button.show'), 'click', true, function () { return loadOPMLContent(); } )); promise_list.push(loopEventListener( gadget.props.element.querySelector('.sync-all'), 'click', true, function () { var title = gadget.props.element.querySelector('.sync-all').innerHTML; return new RSVP.Queue() .push(function () { gadget.props.element.querySelector('.sync-all') .innerHTML = ' Please wait...'; gadget.props.element.querySelector('.sync-all') .disabled = true; return gadget.props.sync_gadget.startSync({now: true}); }) .push(function () { gadget.props.element.querySelector('.sync-all') .innerHTML = title; gadget.props.element.querySelector('.sync-all') .disabled = false; return gadget.getSetting('latest_sync_time'); }) .push(function (latest_sync_time) { if (latest_sync_time !== undefined) { gadget.props.element.querySelector(".last-sync") .innerHTML = formatDate(new Date(latest_sync_time)); } else { gadget.props.element.querySelector(".last-sync") .innerHTML = '--'; } }); } )); promise_list.push(loopEventListener( gadget.props.element.querySelector("table th input[name='opml-all']"), 'change', false, function (element) { if ($(element.target).prop('checked')) { return $(".opml-tablelinks tr td input[type='checkbox']").prop('checked', true); } else { return $(".opml-tablelinks tr td input[type='checkbox']").prop('checked', false); } } )); /*promise_list.push(loopEventListener( gadget.props.element.querySelector("input[name='configure-auth']"), 'change', false, function (element) { if ($(element.target).prop('checked')) { gadget.props.element.querySelector(".configure input[name='username']").value = ""; gadget.props.element.querySelector(".configure input[name='password']").value = ""; return $(gadget.props.element.querySelector(".configure .auth-block")).slideDown(); } else { return $(gadget.props.element.querySelector(".configure .auth-block")).slideUp(); } } ));*/ /*promise_list.push( $(gadget.props.element.querySelector("input[name='configure-auth']")).bind( "change", function(event, ui) { var input_login = gadget.props.element.querySelector(".configure input[name='username']"), input_pwd = gadget.props.element.querySelector(".configure input[name='password']"); if ($(this).prop('checked')) { input_login.value = ""; input_pwd.value = ""; return $(gadget.props.element.querySelector(".configure .auth-block")).slideDown(); } else { input_login.value = "-"; input_pwd.value = "-"; return $(gadget.props.element.querySelector(".configure .auth-block")).slideUp(); } }) );*/ promise_list.push( $(gadget.props.element.querySelector("input[name='configure-newpwd']")).bind( "change", function(event, ui) { var confirm_pwd = gadget.props.element.querySelector(".configure input[name='new_password_confirm']"), new_pwd = gadget.props.element.querySelector(".configure input[name='new_password']"); if ($(this).prop('checked')) { confirm_pwd.value = ""; new_pwd.value = ""; return $(gadget.props.element.querySelector(".configure .new-password")).slideDown(); } else { confirm_pwd.value = "-"; new_pwd.value = "-"; return $(gadget.props.element.querySelector(".configure .new-password")).slideUp(); } }) ); promise_list.push(loopEventListener( gadget.props.element.querySelector("a.opml-delete"), 'click', true, function (element) { var key_list = [], check_list = gadget.props.element.querySelectorAll(".opml-tablelinks tr td input[type='checkbox']"), i; gadget.props.element.querySelector('.msgtext-box') .innerHTML = ""; if (!check_list) { return false; } for (i = 0; i < check_list.length; i += 1) { if ($(check_list[i]).prop('checked')) { key_list.push($(check_list[i]).prop('value')); } } if (key_list.length > 0) { return gadget.getSetting('monitor_url_description') .push(function (url_description_dict) { var i, promise_list = [], delete_promise_list = []; if(url_description_dict === undefined) { url_description_dict = {}; } for (i = 0; i < key_list.length; i += 1) { if (url_description_dict.hasOwnProperty(key_list[i])) { promise_list.push( gadget.props.login_gadget.clearSettingFromParentUrl(url_description_dict[key_list[i]].href) ); delete_promise_list.push( gadget.props.jio_gadget.clearDeletedMonitorUrl( url_description_dict[key_list[i]].href, url_description_dict[key_list[i]].title) ); delete url_description_dict[key_list[i]]; } } return gadget.setSetting('monitor_url_description', url_description_dict) .push(function () { return RSVP.all(promise_list); }) .push(function () { return RSVP.all(delete_promise_list); }) .push(function () { return gadget.reload(); }); }); } } )); promise_list.push(loopEventListener( gadget.props.element.querySelector("a.opml-test"), 'click', true, function (element) { var key_list = [], check_list = gadget.props.element.querySelectorAll(".opml-tablelinks tr td input[type='checkbox']"), i; if (!check_list) { gadget.props.element.querySelector('.msgtext-box') .innerHTML = "No URL selected!"; } for (i = 0; i < check_list.length; i += 1) { if ($(check_list[i]).prop('checked')) { key_list.push($(check_list[i]).prop('value')); } } if (key_list.length <= 0) { gadget.props.element.querySelector('.msgtext-box') .innerHTML = "No URL selected!"; return; } gadget.props.element.querySelector('.msgtext-box') .innerHTML = ""; return gadget.getSetting('monitor_url_description') .push(function (url_description_dict) { var i, promise_list = []; if(url_description_dict === undefined) { url_description_dict = {}; } $(gadget.props.element.querySelector('.loadspinner')).removeClass('ui-content-hidden'); for (i = 0; i < key_list.length; i += 1) { if (url_description_dict.hasOwnProperty(key_list[i])) { promise_list.push(testOPmlUrl(gadget, url_description_dict[key_list[i]].href, url_description_dict[key_list[i]].title)); } } return RSVP.all(promise_list); }) .push(function (result) { var i, state = true; for (i = 0; i < result.length; i += 1) { if (! result[i]) { state = false; break; } } if (state) { gadget.props.element.querySelector('.msgtext-box') .innerHTML = "All OPML URLs was successfully tested."; } $(gadget.props.element.querySelector('.loadspinner')).addClass('ui-content-hidden'); }); } )); promise_list.push(loopEventListener( gadget.props.element.querySelector('form.configure'), 'submit', true, function () { var root_name = '', credential_dict = {}, username = '', password = '', new_password = '', cnew_password = '', reload = true, rows_list = gadget.props.listbox.property_dict.data_result, instance_name_dict = {}, not_changed_dict = {}, config_promise_list = [], button_submit = gadget.props.element.querySelector('.configure button[type="submit"]'); if (rows_list.length > 0) { root_name = rows_list[0].opml_title; } $(gadget.props.element.querySelector('.configure .alert-error')) .addClass('ui-content-hidden').html(''); if ($(gadget.props.element.querySelector("input[name='configure-newpwd']")).prop('checked')) { new_password = gadget.props.element.querySelector("input[name='new_password']").value; cnew_password = gadget.props.element.querySelector("input[name='new_password_confirm']").value; if (new_password !== cnew_password) { $(gadget.props.element.querySelector('.configure .alert-error')) .removeClass('ui-content-hidden') .html('The new password and it confirmation are differents!'); return false; } } return new RSVP.Queue() .push(function () { var promise_list = [], i; $(gadget.props.element.querySelector('.spinner')) .removeClass('ui-content-hidden'); button_submit.disabled = true; username = gadget.props.element.querySelector("input[name='username']").value; password = gadget.props.element.querySelector("input[name='password']").value; for (i = 0; i < rows_list.length; i += 1) { if (rows_list[i].htmlurl) { promise_list.push(checkCredential( gadget, rows_list[i].url.replace('public', 'private'), btoa(username + ':' + password) )); } } return RSVP.all(promise_list); }) .push(function (status_list) { var i, error_msg = ''; for (i = 0; i < status_list.length; i += 1) { if (status_list[i].status !== 'OK') { error_msg += 'Login/password invalid for: ' + rows_list[i].title + '. ' + status_list[i].msg + '
'; } else if (status_list[i].hasOwnProperty('url')) { not_changed_dict[ hashCode( status_list[i].url ) ] = { hash: status_list[i].hash, login: (status_list[i].login || username) }; } } return error_msg; }) .push(function (error_msg) { var tmp, tmp_hash, i; if (error_msg !== '') { $(gadget.props.element.querySelector('.configure .alert-error')) .removeClass('ui-content-hidden') .html(error_msg); new_password = ''; reload = false; return error_msg; } if (username !== '' && password !== '') { for (i = 0; i < rows_list.length; i += 1) { if (validateHttpUrl(rows_list[i].url)) { tmp = { hash: btoa(username + ':' + password), login: username, url: rows_list[i].url.replace('public', 'private'), // XXX - Need to fix this (replace()) parent_url: gadget.props.opml_url //parent_title: root_name }; tmp_hash = hashCode( tmp.url ); if (not_changed_dict.hasOwnProperty(tmp_hash)) { tmp.hash = not_changed_dict[tmp_hash].hash; tmp.login = not_changed_dict[tmp_hash].login; } credential_dict[ tmp_hash ] = tmp; instance_name_dict[ tmp_hash ] = {title: rows_list[i].title}; } } } config_promise_list.push( setUrlConfiguration(gadget, gadget.props.opml_url, root_name) ); // sync this opml feed config_promise_list.push( gadget.props.jio_gadget.syncMonitoringOpmlData(gadget.props.opml_url) ); return RSVP.all(config_promise_list); }) .push(function (result) { // change password if needed var key, promise_list = []; if (new_password !== '') { // Change current passwords and save them to local storage for (key in credential_dict) { promise_list.push( changeMonitorPassword( gadget, credential_dict[key], key, new_password) ); } } return RSVP.all(promise_list); }) .push(function (result_list) { var i, has_error = false; for (i = 0; i < result_list.length; i += 1) { if (result_list[i].status === 'ERROR') { $(gadget.props.element.querySelector('.configure .alert-error')) .removeClass('ui-content-hidden') .append('ERROR ' + result_list[i].code + '. [' + instance_name_dict[result_list[i].key].title + '] Failed to save password, please try again
'); has_error = true; } else if (result_list[i].status === 'OK') { // Update if we could change password credential_dict[result_list[i].key].hash = btoa( credential_dict[result_list[i].key].login + ':' + new_password ); } } if (credential_dict){ // Only save provided credentials return gadget.props.login_gadget.setUrlDict(credential_dict) .push(function () { has_error; }); } return has_error; }) .push(function (has_error) { button_submit.disabled = false; if (! has_error && reload) { if (gadget.props.selected !== "manage") { return gadget.redirect({ page: 'settings_configurator', tab: 'manage' }); } else { return gadget.reload(); } } else { $(gadget.props.element.querySelector('.spinner')).addClass('ui-content-hidden'); } }); } )); sync_checkbox_list = gadget.props.element.querySelectorAll("input[name='sync-data-timer']"); for (i = 0; i < sync_checkbox_list.length; i += 1) { promise_list.push( $(sync_checkbox_list[i]) .bind("change", setSyncTimerInterval.bind(gadget, sync_checkbox_list[i])) ); } return RSVP.all(promise_list); }); }); }(window, rJS, RSVP, $, btoa, Rusha));