/*global window, rJS, Handlebars */ /*jslint nomen: true, indent: 2, maxerr: 3*/ (function (window, rJS, Handlebars) { "use strict"; var gadget_klass = rJS(window), templater = gadget_klass.__template_element, hosting_widget_template = Handlebars.compile( templater.getElementById("template-hostings-list").innerHTML ); function safeGet(gadget, title, url) { var document_id = 'monitor.global'; gadget.props.jio_gadget.createJio({ type: "query", sub_storage: { type: "drivetojiomapping", sub_storage: { type: "dav", url: url } } }); return gadget.props.jio_gadget.get(document_id) .push(function (doc) { return doc; }, function (error) { var value = {type: 'error', method: 'Hosting Subscription list'}; console.log(error); if (error.status_code) { value.title = title + ": " + error.name + " "+ error.status_code + ", " + error.message; } else { value.title = title + ": Cannot find document: " + document_id; } value.message = "Failed to get " + document_id + " from local indexdb storage.\n" + "The document is probably not synchronized yet.\n Source URL: " + url; return gadget.props.log_gadget.log(value) .push(function () { return undefined; }); }); } function getHostingData(gadget, title, url_list) { var promise_list = [], i; for (i = 0; i < url_list.length; i += 1) { promise_list.push(safeGet(gadget, title, url_list[i])); } return RSVP.all(promise_list); } gadget_klass .ready(function (gadget) { gadget.props = {}; gadget.props.hosting_list = []; gadget.props.opml_key_list = []; gadget.props.title = 'Monitoring Hosting Subscriptions'; 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 (g) { return g.getDeclaredGadget('log_gadget') .push(function (log_gadget) { g.props.log_gadget = log_gadget; }); }) .declareAcquiredMethod("getSetting", "getSetting") .declareAcquiredMethod("setSetting", "setSetting") .declareAcquiredMethod("updateHeader", "updateHeader") .declareAcquiredMethod("renderApplication", "renderApplication") .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) { var key, promise_list = []; gadget.props.opml_dict = url_description_dict; opml_tmp_key_list = []; for (key in url_description_dict) { opml_tmp_key_list.push(key); promise_list.push(gadget.props.jio_gadget.getMonitorUrlList(undefined, url_description_dict[key].title)); } return RSVP.all(promise_list); }) .push(function (url_list) { var i, j, promise_list = []; for (i = 0; i < url_list.length; i += 1) { if (url_list[i]) { promise_list.push(getHostingData( gadget, gadget.props.opml_dict[opml_tmp_key_list[i]].title, url_list[i])); } else { //This Feed is not available, remove from key_list opml_tmp_key_list[i] = undefined; } } return RSVP.all(promise_list); }) .push(function (document_list) { var i, status, hosting_dict, hosting_title, date, content, j; for (i = 0; i < opml_tmp_key_list.length; i += 1) { if (opml_tmp_key_list[i] !== undefined) { gadget.props.opml_key_list.push(opml_tmp_key_list[i]); } } for (i = 0; i < document_list.length; i += 1) { hosting_dict = {}; hosting_dict.amount = 0; hosting_dict.error = 0; hosting_dict.success = 0; status = 'OK'; hosting_dict.instance_list = []; date = ''; for (j = 0; j < document_list[i].length; j += 1) { if (document_list[i][j] === undefined) { status = 'ERROR'; continue; } hosting_dict.amount += 1; hosting_dict.instance_list.push({ title: document_list[i][j].title, status: document_list[i][j].status, url: document_list[i][j]._links.private_url.href }); if (document_list[i][j].status == "OK") { hosting_dict.success += 1; } if (document_list[i][j].status === 'ERROR') { hosting_dict.error += 1; if (status !== 'ERROR') { status = 'ERROR'; date = document_list[i][j].date || ''; hosting_title = document_list[i][j]['hosting-title'] || ''; } } else if (document_list[i][j].status === 'WARNING') { if (status !== 'ERROR' && status !== 'WARNING') { status = 'WARNING'; date = document_list[i][j].date || ''; hosting_title = document_list[i][j]['hosting-title'] || ''; } } else if (date === '') { date = document_list[i][j].date || ''; hosting_title = document_list[i][j]['hosting-title'] || ''; } } if (hosting_dict.instance_list.length === 0) { // No instance data was found! hosting_dict.status = 'ERROR'; hosting_dict.title = gadget.props.opml_dict[ gadget.props.opml_key_list[i]].title; hosting_dict.date = 'Not Synchronized'; // Redirect to configurator to sync data hosting_dict.href = '#page=settings_configurator&tab=manage'; } else { hosting_dict.status = status; hosting_dict.title = hosting_title; hosting_dict.date = date; hosting_dict.href = "#page=hosting_subscription_view&key=" + gadget.props.opml_key_list[i]; } gadget.props.hosting_list.push(hosting_dict); } gadget.props.hosting_list.sort(function(a, b){ if(a.title < b.title) return -1; if(a.title > b.title) return 1; return 0; }); content = hosting_widget_template({ hosting_list: gadget.props.hosting_list }); gadget.props.element.querySelector('.hosting-list table tbody') .innerHTML = content; return gadget.props.render_deferred.resolve(); }); }) .declareService(function () { var gadget = this, current_sync_date; return gadget.getSetting('latest_sync_time') .push(function (sync_time) { current_sync_date = sync_time; return gadget.getSetting('status_list_refresh_id'); }) .push(function (timer_id) { var new_timer_id; if (timer_id) { clearInterval(timer_id); } new_timer_id = setInterval(function(){ var hash = window.location.toString().split('#')[1], scroll_position, doc = document.documentElement; if (hash.indexOf('page=hosting_subscription_list') >= 0) { return gadget.getSetting('latest_sync_time') .push(function (sync_time) { if (sync_time > current_sync_date) { scroll_position = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0); current_sync_date = sync_time; return gadget.renderApplication({args: gadget.props.options}) .push(function () { $(document).scrollTop(scroll_position); }); } }); } }, 60000); return gadget.setSetting('status_list_refresh_id', new_timer_id); }); }); }(window, rJS, Handlebars));