/*global window, rJS, RSVP, jsen, Rusha, $ */ /*jslint nomen: true, indent: 2, maxerr: 3*/ (function (window, rJS, RSVP, jsen, Rusha, $) { "use strict"; function getMonitoringSetting(gadget) { return gadget.getSetting('monitor_url_description') .push(function (monitor_url_description) { return monitor_url_description; }) .push(function (monitor_url_dict) { var setting_dict = {}; if (!monitor_url_dict) { return setting_dict; } return gadget.getDeclaredGadget("jio_gadget") .push(function (jio_gadget) { return jio_gadget.get('urls'); }) .push(function (monitor_base_url_dict) { var key; setting_dict.opml_description = []; setting_dict.monitor_url = []; for (key in monitor_url_dict) { setting_dict.opml_description.push(monitor_url_dict[key]); } if (monitor_base_url_dict) { for (key in monitor_base_url_dict) { setting_dict.monitor_url.push(monitor_base_url_dict[key]); } } return setting_dict; }); }); } function validateJsonConfiguration(json_value) { var validate, json_schema = { "$schema": "http://json-schema.org/draft-04/schema#", "type" : "object", "properties": { "opml_description": { "description": "list of monitor opml URL", "type": "array", "items": { "type": "object", "properties": { "href": { "description": "OPML URL", "type": "string" }, "title": { "description": "OPML title", "type": "string" } }, "additionalProperties": false } }, "monitor_url": { "description": "list of registered monitor instance URL", "type": "array", "required": ['hash', "url", "parent_url"], "items": { "type": "object", "properties": { "hash": { "description": "hash string", "type": "string" }, "login": { "description": "login", "type": "string", "default": "" }, "url": { "description": "url of monitor instance", "type": "string" }, "parent_url": { "description": "URL to parent instance", "type": "string" } }, "additionalProperties": false } } }, "additionalProperties": false }; return new RSVP.Queue() .push(function () { validate = jsen(json_schema); return validate(json_value); }); } var gadget_klass = rJS(window), 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(); }); }) .ready(function (g) { return g.getDeclaredGadget("login_gadget") .push(function (login_gadget) { g.props.login_gadget = login_gadget; }); }) .ready(function (gadget) { return gadget.getDeclaredGadget("sync_gadget") .push(function (sync_gadget) { gadget.props.sync_gadget = sync_gadget; }); }) .ready(function (g) { return g.getDeclaredGadget("jio_gadget") .push(function (jio_gadget) { g.props.jio_gadget = jio_gadget; return jio_gadget.createJio({ type: "indexeddb", database: "setting" }, false); }); }) .declareAcquiredMethod("getSetting", "getSetting") .declareAcquiredMethod("setSetting", "setSetting") .declareAcquiredMethod("redirect", "redirect") .declareMethod("render", function (options) { var gadget = this, url_description_dict; return getMonitoringSetting(gadget) .push(function (setting_dict) { gadget.props.element.querySelector('textarea[name="settings-data"]') .innerHTML = JSON.stringify(setting_dict); return gadget.props.deferred.resolve(); }); }) .declareService(function () { var gadget = this; return new RSVP.Queue() .push(function () { return gadget.props.deferred.promise; }) .push(function () { return $(gadget.props.element.querySelector("a[href='#config-export']")).trigger('click'); }) .push(function () { var promise_list = []; promise_list.push(loopEventListener( gadget.props.element.querySelector('.btn-reload'), 'click', true, function () { return new RSVP.Queue() .push(function () { return getMonitoringSetting(gadget); }) .push(function (setting_dict) { $(gadget.props.element.querySelector('textarea[name="settings-data"]')) .val(JSON.stringify(setting_dict)); }); } )); promise_list.push(loopEventListener( gadget.props.element.querySelector('.btn-continue'), 'click', true, function () { return gadget.redirect({ page: 'settings_configurator', tab: 'manage' }); } )); promise_list.push(loopEventListener( gadget.props.element.querySelector('.btn-save'), 'click', true, function () { var json_string = $(gadget.props.element.querySelector('textarea[name="settings-data-input"]')).val(), configuration_dict, monitor_url_dict = {}, monitor_opml_url_dict = {}, error_msg = '', i; try { configuration_dict = JSON.parse(json_string); } catch (e) { return $(gadget.props.element.querySelector('.alert-error')) .removeClass('ui-content-hidden') .html('Error: Invalid json content!'); } return validateJsonConfiguration(configuration_dict) .push(function (validate_result) { if (validate_result) { for (i = 0; i < configuration_dict.opml_description.length; i += 1) { monitor_opml_url_dict[ hashCode(configuration_dict.opml_description[i].href) ] = configuration_dict.opml_description[i]; } for (i = 0; i < configuration_dict.monitor_url.length; i += 1) { monitor_url_dict[ hashCode(configuration_dict.monitor_url[i].url) ] = configuration_dict.monitor_url[i]; } $(gadget.props.element.querySelector('.btn-continue')) .addClass('ui-content-hidden'); gadget.props.element.querySelector('.btn-save') .innerHTML = "Please wait..."; return gadget.props.login_gadget.setUrlDict(monitor_url_dict) .push(function () { return gadget.setSetting('monitor_url_description', monitor_opml_url_dict); }) .push(function () { return gadget.props.jio_gadget.syncMonitoringOpmlData(); }) .push(function (result_list) { var i; for (i = 0; i < result_list.length; i += 1) { if (result_list[i].error) { error_msg += 'Failed to sync data at "' + result_list[i].url + '"
'; } } gadget.props.element.querySelector('.btn-save') .innerHTML = "Sync Data..."; return gadget.props.sync_gadget.startSync({now: true}); }) .push(function (){ if (error_msg) { $(gadget.props.element.querySelector('.alert-error')) .removeClass('ui-content-hidden') .html(error_msg); $(gadget.props.element.querySelector('.btn-continue')) .removeClass('ui-content-hidden'); gadget.props.element.querySelector('.btn-save') .innerHTML = "Retry Import"; } else { return gadget.redirect({ page: 'settings_configurator', tab: 'manage' }); } }); } else { $(gadget.props.element.querySelector('.alert-error')) .removeClass('ui-content-hidden') .html('Error: Content is not a valid Monitoring Json configuration!'); } }); } )); }); }); }(window, rJS, RSVP, jsen, Rusha, $));