From 1c4c9992417ccadf51a7425d70cafb12f0405a21 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Fri, 8 Apr 2016 19:34:45 +0200 Subject: Fix #924 --- js/controller/SettingsController.js | 125 +++++++++++---------- js/tests/unit/controller/SettingsControllerSpec.js | 12 +- 2 files changed, 75 insertions(+), 62 deletions(-) (limited to 'js') diff --git a/js/controller/SettingsController.js b/js/controller/SettingsController.js index e9d93a1eb..2bb932aa8 100644 --- a/js/controller/SettingsController.js +++ b/js/controller/SettingsController.js @@ -8,76 +8,83 @@ * @copyright Bernhard Posselt 2014 */ app.controller('SettingsController', -function ($route, $q, SettingsResource, ItemResource, OPMLParser, - OPMLImporter, Publisher) { - 'use strict'; + function ($route, $q, SettingsResource, ItemResource, OPMLParser, + OPMLImporter, Publisher) { + 'use strict'; + this.isOPMLImporting = false; + this.isArticlesImporting = false; + this.opmlImportError = false; + this.articleImportError = false; + this.opmlImportEmptyError = false; + var self = this; - this.isOPMLImporting = false; - this.isArticlesImporting = false; - this.opmlImportError = false; - this.articleImportError = false; + var set = function (key, value) { + SettingsResource.set(key, value); - var set = function (key, value) { - SettingsResource.set(key, value); + if (['showAll', 'oldestFirst', 'compact'].indexOf(key) >= 0) { + $route.reload(); + } + }; - if (['showAll', 'oldestFirst', 'compact'].indexOf(key) >= 0) { - $route.reload(); - } - }; + this.toggleSetting = function (key) { + set(key, !this.getSetting(key)); + }; - this.toggleSetting = function (key) { - set(key, !this.getSetting(key)); - }; + this.getSetting = function (key) { + return SettingsResource.get(key); + }; - this.getSetting = function (key) { - return SettingsResource.get(key); - }; + this.importOPML = function (content) { + self.opmlImportError = false; + self.opmlImportEmptyError = false; + self.articleImportError = false; - this.importOPML = function (content) { - this.opmlImportError = false; - this.articleImportError = false; + try { + this.isOPMLImporting = false; + var parsedContent = OPMLParser.parse(content); - try { - this.isOPMLImporting = false; - var parsedContent = OPMLParser.parse(content); + var jobSize = 5; - var self = this; - var jobSize = 5; + if (parsedContent.folders.length === 0 && + parsedContent.feeds.length === 0) { + self.opmlImportEmptyError = true; + } else { + OPMLImporter.importFolders(parsedContent) + .then(function (feedQueue) { + return OPMLImporter.importFeedQueue(feedQueue, + jobSize); + }).finally(function () { + self.isOPMLImporting = false; + }); + } - OPMLImporter.importFolders(parsedContent) - .then(function (feedQueue) { - return OPMLImporter.importFeedQueue(feedQueue, jobSize); - }).finally(function () { - self.isOPMLImporting = false; - }); + } catch (error) { + this.opmlImportError = true; + console.error(error); + this.isOPMLImporting = false; + } + }; - } catch (error) { - console.error(error); - this.isOPMLImporting = false; - this.opmlImportError = true; - } - }; - - this.importArticles = function (content) { - this.opmlImportError = false; - this.articleImportError = false; + this.importArticles = function (content) { + this.opmlImportError = false; + this.articleImportError = false; - try { - this.isArticlesImporting = true; - var articles = JSON.parse(content); + try { + this.isArticlesImporting = true; + var articles = JSON.parse(content); - var self = this; - ItemResource.importArticles(articles).success(function (data) { - Publisher.publishAll(data); - }).finally(function () { - self.isArticlesImporting = false; - }); + var self = this; + ItemResource.importArticles(articles).success(function (data) { + Publisher.publishAll(data); + }).finally(function () { + self.isArticlesImporting = false; + }); - } catch (error) { - console.error(error); - this.articleImportError = true; - this.isArticlesImporting = false; - } - }; + } catch (error) { + console.error(error); + this.articleImportError = true; + this.isArticlesImporting = false; + } + }; -}); \ No newline at end of file + }); \ No newline at end of file diff --git a/js/tests/unit/controller/SettingsControllerSpec.js b/js/tests/unit/controller/SettingsControllerSpec.js index 4214f9d81..19973860e 100644 --- a/js/tests/unit/controller/SettingsControllerSpec.js +++ b/js/tests/unit/controller/SettingsControllerSpec.js @@ -112,7 +112,13 @@ describe('SettingsController', function () { OPMLParser) { var queue = 4; - OPMLParser.parse = jasmine.createSpy('parse').and.returnValue(2); + var opml = { + feeds: [ + {name: 'hi'} + ], + folders: [] + }; + OPMLParser.parse = jasmine.createSpy('parse').and.returnValue(opml); OPMLImporter.importFolders = jasmine.createSpy('importFolders') .and.returnValue({ @@ -133,12 +139,12 @@ describe('SettingsController', function () { OPMLParser: OPMLParser }); - var content = '{"test":1}'; + var content = '{"folders":[{name:"b"}]}'; ctrl.importOPML(content); expect(OPMLParser.parse).toHaveBeenCalledWith(content); - expect(OPMLImporter.importFolders).toHaveBeenCalledWith(2); + expect(OPMLImporter.importFolders).toHaveBeenCalledWith(opml); expect(OPMLImporter.importFeedQueue).toHaveBeenCalledWith(4, 5); expect(ctrl.isOPMLImporting).toBe(false); expect(ctrl.opmlImportError).toBe(false); -- cgit v1.2.3