/*global window, rJS, btoa, Handlebars, $ */ /*jslint nomen: true, indent: 2, maxerr: 3*/ (function (window, rJS, btoa, Handlebars, $) { "use strict"; var gadget_klass = rJS(window), templater = gadget_klass.__template_element, parameters_widget_template = Handlebars.compile( templater.getElementById("parameters-widget-template").innerHTML ), instance_widget_template = Handlebars.compile( templater.getElementById("instance-details-widget-overview").innerHTML ); function safeGet(gadget, url) { var document_id = 'monitor.global'; return gadget.props.jio_gadget.get(document_id) .push(function (doc) { return doc; }, function (error) { console.log(error); gadget.props.error_list.push(url); return undefined; }); } function getHostingData(gadget, url_list) { var promise_list = [], i; for (i = 0; i < url_list.length; i += 1) { gadget.props.jio_gadget.createJio({ type: "query", sub_storage: { type: "drivetojiomapping", sub_storage: { type: "dav", url: url_list[i].replace('public', 'private') // Hard path to change!! } } }); promise_list.push(safeGet(gadget, url_list[i])); } return RSVP.all(promise_list); } function getMonitorCredentials(gadget, monitor, hosting_title, index) { return gadget.props.login_gadget.loginRedirect( monitor.url, { page: 'hosting_overview', select: index }, monitor.title, hosting_title ); } function getInstanceDict(monitor_dict) { var instance_dict, tmp_url, tmp_process_url; // Resource view URLs tmp_url = "#page=resource_view&title=" + monitor_dict.title + "&root=" + monitor_dict['hosting-title'] + "&jio_for=" + monitor_dict._links.private_url.href; tmp_process_url = "#page=process_view&title=" + monitor_dict.title + "&root=" + monitor_dict['hosting-title'] + "&jio_for=" + monitor_dict._links.private_url.href; instance_dict = { title: monitor_dict.title, root_title: monitor_dict['hosting-title'], date: monitor_dict.date, status: monitor_dict.status, instance: monitor_dict._embedded.instance || '', public_url: monitor_dict._links.hasOwnProperty('public_url') ? monitor_dict._links.public_url.href : '', private_url: monitor_dict._links.hasOwnProperty('private_url') ? monitor_dict._links.private_url.href : '', rss_url: monitor_dict._links.hasOwnProperty('rss_url') ? monitor_dict._links.rss_url.href : '', resource_url: tmp_url, process_url: tmp_process_url, warning: (monitor_dict.status.toUpperCase() === "WARNING") ? true : false }; return instance_dict; } gadget_klass .ready(function (gadget) { gadget.props = {}; gadget.props.hosting_list = []; gadget.props.opml_key_list = []; gadget.props.error_list = []; gadget.props.title = 'Hosting Subscriptions View'; return gadget.getDeclaredGadget("login_gadget") .push(function (login_gadget) { gadget.props.login_gadget = login_gadget; }); }) .ready(function (gadget) { return gadget.getElement() .push(function (element) { gadget.props.element = element; gadget.props.render_deferred = RSVP.defer(); }); }) .ready(function (gadget) { return gadget.getDeclaredGadget("jio_gadget") .push(function (jio_gadget) { gadget.props.jio_gadget = jio_gadget; }); }) /*.ready(function (gadget) { return gadget.getDeclaredGadget("config_gadget") .push(function (config_gadget) { gadget.props.config_gadget = config_gadget; }); })*/ .declareAcquiredMethod("redirect", "redirect") .declareAcquiredMethod("getSetting", "getSetting") .declareAcquiredMethod("setSetting", "setSetting") .declareAcquiredMethod("updateHeader", "updateHeader") .declareMethod("render", function (options) { var gadget = this, opml_tmp_key_list, hosting_url_list; //return gadget.props.jio_gadget.getMonitorUrlList() gadget.props.options = options; return gadget.updateHeader({ title: gadget.props.title }) .push(function () { return gadget.getSetting('monitor_url_description'); }) .push(function (url_description_dict) { if (url_description_dict.hasOwnProperty(options.key)) { gadget.props.hosting = url_description_dict[options.key]; return gadget.props.jio_gadget.getMonitorUrlList(undefined, url_description_dict[options.key].title); } else { return redirect({ page: 'notfound', reference: options.key }); } }) .push(function (url_list) { return getHostingData(gadget, url_list); }) .push(function (document_list) { var parameter_content, instance_list = [], parameter_list = [], status_url = '', i, instance_content; gadget.props.document_list = document_list; gadget.props.element.querySelector('.hosting-block .overview-title span') .innerHTML = gadget.props.hosting.title; // + '{{status}}'; for (i = 0; i < document_list.length; i += 1) { if (!document_list[i]) { continue; } instance_list.push( getInstanceDict(document_list[i]) ); if (document_list[i].hasOwnProperty('parameters')) { parameter_list.push({ title: document_list[i].title, parameters: document_list[i].parameters, base_url: document_list[i]._links.hasOwnProperty('private_url') ? document_list[i]._links.private_url.href : '', index: i }); } } status_url = "#page=status_list&search=" + gadget.props.hosting.title + "&reset_filter=1"; parameter_content = parameters_widget_template({parameter_list: parameter_list}); instance_content = instance_widget_template({ instance_list: instance_list, status_list_url: status_url }); gadget.props.element.querySelector('.hosting-block .instances-parameters') .innerHTML = parameter_content; gadget.props.element.querySelector('.hosting-block .instances-status') .innerHTML = instance_content; return gadget.props.render_deferred.resolve(); }) .push(function () { $(".hosting-block .signal").addClass("ui-content-hidden"); return $(gadget.props.element.querySelectorAll('.hosting-block .ui-listview-outer')).listview().listview("refresh"); }); }) .declareService(function () { var gadget = this; function bindOnClick(element) { var fieldset = $(element.parentNode.querySelector('.ui-collapse-content')), line = $(element); if (line.hasClass('ui-icon-plus')) { line.removeClass('ui-icon-plus'); line.addClass('ui-icon-minus'); } else { line.removeClass('ui-icon-minus'); line.addClass('ui-icon-plus'); } if (fieldset !== undefined) { fieldset.toggleClass('ui-content-hidden'); } return false; } function updateParameterBox(parameter_list, title) { var element = gadget.props.element.querySelector('table[title="' + title + '"]'), tmp, i; if (!element) { return; } for (i = 0; i < parameter_list.length; i += 1) { if (!parameter_list[i].key) { continue; } element.querySelector('.v-' + parameter_list[i].key).innerHTML = parameter_list[i].value; } } function editMonitorProps (element) { var index = parseInt($(element).attr('rel'), 10), promise_list = []; if (isNaN(index) || gadget.props.document_list.length < index) { return; } return new RSVP.Queue() .push(function () { if (gadget.props.config_gadget) { return gadget.dropGadget('config_gadget'); } return false; }) .push(function () { gadget.props.config_gadget = null; return gadget.declareGadget("gadget_monitoring_document_edit.html", { element: gadget.props.element, scope: 'config_gadget', sandbox: "public" } ); }) .push(function (config_gadget) { gadget.props.config_gadget = config_gadget; return gadget.props.config_gadget.popupEdit({ url: gadget.props.document_list[index]._links.private_url.href, parameters: gadget.props.document_list[index].parameters, title: gadget.props.document_list[index].title, root_title: gadget.props.document_list[index]['hosting-title'], page_options: gadget.props.options, path: 'config', key: 'config.tmp' }, function (data) { var update_promise = [], i, monitor_user = '', monitor_password = ''; // Try to save monitor credential if they are pres for (i = 0; i < data.length; i += 1) { if (data[i].key === 'monitor-password') { monitor_password = data[i].value; } if ((data[i].key || data[i].title) === 'monitor-user') { monitor_user = data[i].value; } } if (monitor_password && monitor_user) { update_promise.push(gadget.props.login_gadget.updateUrlValue( gadget.props.document_list[index]._links.private_url.href, monitor_user, monitor_password )); } gadget.props.document_list[index].parameters = data; updateParameterBox(data, gadget.props.document_list[index].title); $(gadget.props.element.querySelector('.alert-info')) .removeClass('ui-content-hidden'); return RSVP.all(update_promise); }); }); } function getMonitorDocument(url, auth_login) { var jio_options = { type: "query", sub_storage: { type: "drivetojiomapping", sub_storage: { type: "dav", url: url } } }, document_id = 'monitor.global'; if (auth_login) { jio_options.sub_storage.sub_storage.basic_login = auth_login; } gadget.props.jio_gadget.createJio(jio_options); return gadget.props.jio_gadget.get(document_id); } return new RSVP.Queue() .push(function () { return gadget.props.render_deferred.promise; }) .push(function () { var promise_list = [], element_list = gadget.props.element.querySelectorAll('.hosting-block .ui-listview-container li > a'), edit_list = gadget.props.element.querySelectorAll('.hosting-block .prop-edit'), i; for (i = 0; i < element_list.length; i += 1) { promise_list.push(loopEventListener( element_list[i], 'click', false, bindOnClick.bind(gadget, element_list[i]) )); } for (i = 0; i < edit_list.length; i += 1) { promise_list.push(loopEventListener( edit_list[i], 'click', false, editMonitorProps.bind(gadget, edit_list[i]) )); } return RSVP.all(promise_list); }); }); }(window, rJS, btoa, Handlebars, $));