From 274034a63374434298204db21e6d6513690e6eaf Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Mon, 19 May 2014 16:00:08 +0200 Subject: put models into seperate folder --- js/Gruntfile.js | 3 + js/build/app.js | 118 +++++++++++++++--------------- js/karma.conf.js | 1 + js/model/Feed.js | 20 +++++ js/model/Folder.js | 20 +++++ js/model/Item.js | 44 +++++++++++ js/model/Model.js | 88 ++++++++++++++++++++++ js/service/Feed.js | 20 ----- js/service/Folder.js | 20 ----- js/service/Item.js | 44 ----------- js/service/Model.js | 88 ---------------------- js/tests/unit/models/ItemSpec.js | 43 +++++++++++ js/tests/unit/models/ModelSpec.js | 129 +++++++++++++++++++++++++++++++++ js/tests/unit/service/ItemSpec.js | 43 ----------- js/tests/unit/service/ModelSpec.js | 129 --------------------------------- js/tests/unit/service/PublisherSpec.js | 4 +- 16 files changed, 409 insertions(+), 405 deletions(-) create mode 100644 js/model/Feed.js create mode 100644 js/model/Folder.js create mode 100644 js/model/Item.js create mode 100644 js/model/Model.js delete mode 100644 js/service/Feed.js delete mode 100644 js/service/Folder.js delete mode 100644 js/service/Item.js delete mode 100644 js/service/Model.js create mode 100644 js/tests/unit/models/ItemSpec.js create mode 100644 js/tests/unit/models/ModelSpec.js delete mode 100644 js/tests/unit/service/ItemSpec.js delete mode 100644 js/tests/unit/service/ModelSpec.js diff --git a/js/Gruntfile.js b/js/Gruntfile.js index 5dd29b9fc..7a4573c6c 100644 --- a/js/Gruntfile.js +++ b/js/Gruntfile.js @@ -70,6 +70,7 @@ module.exports = function (grunt) { 'controller/**/*.js', 'filter/**/*.js', 'service/**/*.js', + 'model/**/*.js', 'directive/**/*.js' ], dest: '<%= meta.production %>app.js' @@ -99,6 +100,7 @@ module.exports = function (grunt) { 'app/**/*.js', 'filter/**/*.js', 'service/**/*.js', + 'model/**/*.js', 'controller/**/*.js', 'directive/**/*.js', 'tests/**/*.js', @@ -118,6 +120,7 @@ module.exports = function (grunt) { 'tests/**/*.js', 'app/**/*.js', 'controller/**/*.js', + 'model/**/*.js', 'directive/**/*.js', 'filter/**/*.js', 'service/**/*.js', diff --git a/js/build/app.js b/js/build/app.js index d2a4e54c1..ffb062350 100644 --- a/js/build/app.js +++ b/js/build/app.js @@ -202,6 +202,65 @@ app.controller('SettingsController', function () { 'use strict'; console.log('here'); }); +app.service('Loading', function () { + 'use strict'; + this.loading = { + global: false, + content: false, + autopaging: false + }; + this.setLoading = function (area, isLoading) { + this.loading[area] = isLoading; + }; + this.isLoading = function (area) { + return this.loading[area]; + }; +}); +app.service('Publisher', function () { + 'use strict'; + var self = this; + this.channels = {}; + this.subscribe = function (object) { + return { + toChannels: function () { + var counter, channel; + for (counter = 0; counter < arguments.length; counter += 1) { + channel = arguments[counter]; + self.channels[channel] = self.channels[channel] || []; + self.channels[channel].push(object); + } + } + }; + }; + this.publishAll = function (data) { + var channel, counter; + for (channel in data) { + if (data.hasOwnProperty(channel) && this.channels[channel] !== undefined) { + for (counter = 0; counter < this.channels[channel].length; counter += 1) { + this.channels[channel][counter].receive(data[channel], channel); + } + } + } + }; +}); +app.service('Settings', function () { + 'use strict'; + this.settings = {}; + this.receive = function (data) { + var key; + for (key in data) { + if (data.hasOwnProperty(key)) { + this.settings[key] = data[key]; + } + } + }; + this.get = function (key) { + return this.settings[key]; + }; + this.set = function (key, value) { + this.settings[key] = value; + }; +}); app.factory('Feed', [ 'Model', function (Model) { @@ -253,20 +312,6 @@ app.factory('Item', [ return new Item(); } ]); -app.service('Loading', function () { - 'use strict'; - this.loading = { - global: false, - content: false, - autopaging: false - }; - this.setLoading = function (area, isLoading) { - this.loading[area] = isLoading; - }; - this.isLoading = function (area) { - return this.loading[area]; - }; -}); app.factory('Model', function () { 'use strict'; var Model = function (id) { @@ -330,50 +375,5 @@ app.factory('Model', function () { }; return Model; }); -app.service('Publisher', function () { - 'use strict'; - var self = this; - this.channels = {}; - this.subscribe = function (object) { - return { - toChannels: function () { - var counter, channel; - for (counter = 0; counter < arguments.length; counter += 1) { - channel = arguments[counter]; - self.channels[channel] = self.channels[channel] || []; - self.channels[channel].push(object); - } - } - }; - }; - this.publishAll = function (data) { - var channel, counter; - for (channel in data) { - if (data.hasOwnProperty(channel) && this.channels[channel] !== undefined) { - for (counter = 0; counter < this.channels[channel].length; counter += 1) { - this.channels[channel][counter].receive(data[channel], channel); - } - } - } - }; -}); -app.service('Settings', function () { - 'use strict'; - this.settings = {}; - this.receive = function (data) { - var key; - for (key in data) { - if (data.hasOwnProperty(key)) { - this.settings[key] = data[key]; - } - } - }; - this.get = function (key) { - return this.settings[key]; - }; - this.set = function (key, value) { - this.settings[key] = value; - }; -}); })(angular, jQuery, OC, oc_requesttoken); \ No newline at end of file diff --git a/js/karma.conf.js b/js/karma.conf.js index 91118736c..5b33469dd 100644 --- a/js/karma.conf.js +++ b/js/karma.conf.js @@ -29,6 +29,7 @@ module.exports = function (config) { 'controller/**/*.js', 'filter/**/*.js', 'service/**/*.js', + 'model/**/*.js', 'directive/**/*.js', 'tests/unit/**/*Spec.js' ], diff --git a/js/model/Feed.js b/js/model/Feed.js new file mode 100644 index 000000000..9223ca6e7 --- /dev/null +++ b/js/model/Feed.js @@ -0,0 +1,20 @@ +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Bernhard Posselt + * @copyright Bernhard Posselt 2014 + */ +app.factory('Feed', function (Model) { + 'use strict'; + + var Feed = function () { + Model.call(this, 'url'); + }; + + Feed.prototype = Object.create(Model.prototype); + + return new Feed(); +}); \ No newline at end of file diff --git a/js/model/Folder.js b/js/model/Folder.js new file mode 100644 index 000000000..101b8ec66 --- /dev/null +++ b/js/model/Folder.js @@ -0,0 +1,20 @@ +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Bernhard Posselt + * @copyright Bernhard Posselt 2014 + */ +app.factory('Folder', function (Model) { + 'use strict'; + + var Folder = function () { + Model.call(this, 'name'); + }; + + Folder.prototype = Object.create(Model.prototype); + + return new Folder(); +}); \ No newline at end of file diff --git a/js/model/Item.js b/js/model/Item.js new file mode 100644 index 000000000..ec3512f22 --- /dev/null +++ b/js/model/Item.js @@ -0,0 +1,44 @@ +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Bernhard Posselt + * @copyright Bernhard Posselt 2014 + */ +app.factory('Item', function (Model) { + 'use strict'; + + var Item = function () { + Model.call(this, 'id'); + }; + + Item.prototype = Object.create(Model.prototype); + + Item.prototype.receive = function (value, channel) { + switch (channel) { + + case 'newestItemId': + this.newestItemId = value; + break; + + case 'starred': + this.starredCount = value; + break; + default: + Model.prototype.receive.call(this, value, channel); + } + }; + + Item.prototype.getNewestItemId = function () { + return this.newestItemId; + }; + + Item.prototype.getStarredCount = function () { + return this.starredCount; + }; + + + return new Item(); +}); \ No newline at end of file diff --git a/js/model/Model.js b/js/model/Model.js new file mode 100644 index 000000000..dddede788 --- /dev/null +++ b/js/model/Model.js @@ -0,0 +1,88 @@ +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Bernhard Posselt + * @copyright Bernhard Posselt 2014 + */ +app.factory('Model', function () { + 'use strict'; + + var Model = function (id) { + this.id = id; + this.values = []; + this.hashMap = {}; + }; + + Model.prototype = { + receive: function (values) { + var self = this; + values.forEach(function (value) { + self.add(value); + }); + }, + + add: function (value) { + var key, + existing; + + existing = this.hashMap[value[this.id]]; + + if (existing === undefined) { + this.values.push(value); + this.hashMap[value[this.id]] = value; + } else { + // copy values from new to old object if it exists already + for (key in value) { + if (value.hasOwnProperty(key)) { + existing[key] = value[key]; + } + } + } + }, + + size: function () { + return this.values.length; + }, + + get: function (id) { + return this.hashMap[id]; + }, + + delete: function (id) { + // find index of object that should be deleted + var i, + deleteAtIndex; + + for (i = 0; i < this.values.length; i += 1) { + if (this.values[i][this.id] === id) { + deleteAtIndex = i; + break; + } + } + + if (deleteAtIndex !== undefined) { + this.values.splice(deleteAtIndex, 1); + } + + if (this.hashMap[id] !== undefined) { + delete this.hashMap[id]; + } + }, + + clear: function () { + this.hashMap = {}; + + // http://stackoverflow.com/questions/1232040/how-to-empty-an-array-in-javascript + // this is the fastes way to empty an array when you want to keep the + // reference around + while (this.values.length > 0) { + this.values.pop(); + } + } + }; + + return Model; +}); \ No newline at end of file diff --git a/js/service/Feed.js b/js/service/Feed.js deleted file mode 100644 index 9223ca6e7..000000000 --- a/js/service/Feed.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * ownCloud - News - * - * This file is licensed under the Affero General Public License version 3 or - * later. See the COPYING file. - * - * @author Bernhard Posselt - * @copyright Bernhard Posselt 2014 - */ -app.factory('Feed', function (Model) { - 'use strict'; - - var Feed = function () { - Model.call(this, 'url'); - }; - - Feed.prototype = Object.create(Model.prototype); - - return new Feed(); -}); \ No newline at end of file diff --git a/js/service/Folder.js b/js/service/Folder.js deleted file mode 100644 index 101b8ec66..000000000 --- a/js/service/Folder.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * ownCloud - News - * - * This file is licensed under the Affero General Public License version 3 or - * later. See the COPYING file. - * - * @author Bernhard Posselt - * @copyright Bernhard Posselt 2014 - */ -app.factory('Folder', function (Model) { - 'use strict'; - - var Folder = function () { - Model.call(this, 'name'); - }; - - Folder.prototype = Object.create(Model.prototype); - - return new Folder(); -}); \ No newline at end of file diff --git a/js/service/Item.js b/js/service/Item.js deleted file mode 100644 index ec3512f22..000000000 --- a/js/service/Item.js +++ /dev/null @@ -1,44 +0,0 @@ -/** - * ownCloud - News - * - * This file is licensed under the Affero General Public License version 3 or - * later. See the COPYING file. - * - * @author Bernhard Posselt - * @copyright Bernhard Posselt 2014 - */ -app.factory('Item', function (Model) { - 'use strict'; - - var Item = function () { - Model.call(this, 'id'); - }; - - Item.prototype = Object.create(Model.prototype); - - Item.prototype.receive = function (value, channel) { - switch (channel) { - - case 'newestItemId': - this.newestItemId = value; - break; - - case 'starred': - this.starredCount = value; - break; - default: - Model.prototype.receive.call(this, value, channel); - } - }; - - Item.prototype.getNewestItemId = function () { - return this.newestItemId; - }; - - Item.prototype.getStarredCount = function () { - return this.starredCount; - }; - - - return new Item(); -}); \ No newline at end of file diff --git a/js/service/Model.js b/js/service/Model.js deleted file mode 100644 index dddede788..000000000 --- a/js/service/Model.js +++ /dev/null @@ -1,88 +0,0 @@ -/** - * ownCloud - News - * - * This file is licensed under the Affero General Public License version 3 or - * later. See the COPYING file. - * - * @author Bernhard Posselt - * @copyright Bernhard Posselt 2014 - */ -app.factory('Model', function () { - 'use strict'; - - var Model = function (id) { - this.id = id; - this.values = []; - this.hashMap = {}; - }; - - Model.prototype = { - receive: function (values) { - var self = this; - values.forEach(function (value) { - self.add(value); - }); - }, - - add: function (value) { - var key, - existing; - - existing = this.hashMap[value[this.id]]; - - if (existing === undefined) { - this.values.push(value); - this.hashMap[value[this.id]] = value; - } else { - // copy values from new to old object if it exists already - for (key in value) { - if (value.hasOwnProperty(key)) { - existing[key] = value[key]; - } - } - } - }, - - size: function () { - return this.values.length; - }, - - get: function (id) { - return this.hashMap[id]; - }, - - delete: function (id) { - // find index of object that should be deleted - var i, - deleteAtIndex; - - for (i = 0; i < this.values.length; i += 1) { - if (this.values[i][this.id] === id) { - deleteAtIndex = i; - break; - } - } - - if (deleteAtIndex !== undefined) { - this.values.splice(deleteAtIndex, 1); - } - - if (this.hashMap[id] !== undefined) { - delete this.hashMap[id]; - } - }, - - clear: function () { - this.hashMap = {}; - - // http://stackoverflow.com/questions/1232040/how-to-empty-an-array-in-javascript - // this is the fastes way to empty an array when you want to keep the - // reference around - while (this.values.length > 0) { - this.values.pop(); - } - } - }; - - return Model; -}); \ No newline at end of file diff --git a/js/tests/unit/models/ItemSpec.js b/js/tests/unit/models/ItemSpec.js new file mode 100644 index 000000000..85ee5789e --- /dev/null +++ b/js/tests/unit/models/ItemSpec.js @@ -0,0 +1,43 @@ +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Bernhard Posselt + * @copyright Bernhard Posselt 2014 + */ +describe('Item', function () { + 'use strict'; + + beforeEach(module('News')); + + + it('should receive the newestItemId', inject(function (Item) { + Item.receive(3, 'newestItemId'); + + expect(Item.getNewestItemId()).toBe(3); + })); + + + it('should receive the newestItemId', inject(function (Item) { + Item.receive(2, 'starred'); + + expect(Item.getStarredCount()).toBe(2); + })); + + + it('should receive items', inject(function (Item) { + Item.receive([ + { + id: 3 + }, + { + id: 4 + } + ], 'items'); + + expect(Item.size()).toBe(2); + })); + +}); \ No newline at end of file diff --git a/js/tests/unit/models/ModelSpec.js b/js/tests/unit/models/ModelSpec.js new file mode 100644 index 000000000..57b0f422c --- /dev/null +++ b/js/tests/unit/models/ModelSpec.js @@ -0,0 +1,129 @@ +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Bernhard Posselt + * @copyright Bernhard Posselt 2014 + */ +describe('Model', function () { + 'use strict'; + + var childModel; + + beforeEach(module('News')); + + beforeEach(inject(function (Model) { + var ChildModel = function () { + Model.call(this, 'id'); + }; + ChildModel.prototype = Object.create(Model.prototype); + + childModel = new ChildModel(); + })); + + + it('should receive an object', function () { + var objects = [ + { + id: 2 + }, + { + id: 3 + } + ]; + + childModel.receive(objects); + + expect(childModel.size()).toBe(2); + }); + + + it('should add an object', function () { + var object = { + id: 3, + name: 'test' + }; + childModel.add(object); + + expect(childModel.get(3)).toBe(object); + }); + + + it('should overwrite an object if it already exists', function () { + var object1, + object2; + + object1 = { + id: 3, + name: 'test', + test: 'ho' + }; + + object2 = { + id: 3, + name: 'test2' + }; + + childModel.add(object1); + childModel.add(object2); + + expect(childModel.get(3).name).toBe('test2'); + expect(childModel.get(3).test).toBe('ho'); + expect(childModel.size()).toBe(1); + }); + + + it('should delete a model', function () { + var object1, + object2; + + object1 = { + id: 3, + name: 'test', + test: 'ho' + }; + + object2 = { + id: 4, + name: 'test2' + }; + + childModel.add(object1); + childModel.add(object2); + + childModel.delete(3); + + expect(childModel.get(3)).not.toBeDefined(); + expect(childModel.get(4).name).toBe('test2'); + expect(childModel.size()).toBe(1); + }); + + + it('should clear all models', function () { + var object1, + object2; + + object1 = { + id: 3, + name: 'test', + test: 'ho' + }; + + object2 = { + id: 4, + name: 'test2' + }; + + childModel.add(object1); + childModel.add(object2); + + childModel.clear(); + + expect(childModel.get(3)).not.toBeDefined(); + expect(childModel.get(4)).not.toBeDefined(); + expect(childModel.size()).toBe(0); + }); + +}); \ No newline at end of file diff --git a/js/tests/unit/service/ItemSpec.js b/js/tests/unit/service/ItemSpec.js deleted file mode 100644 index 85ee5789e..000000000 --- a/js/tests/unit/service/ItemSpec.js +++ /dev/null @@ -1,43 +0,0 @@ -/** - * ownCloud - News - * - * This file is licensed under the Affero General Public License version 3 or - * later. See the COPYING file. - * - * @author Bernhard Posselt - * @copyright Bernhard Posselt 2014 - */ -describe('Item', function () { - 'use strict'; - - beforeEach(module('News')); - - - it('should receive the newestItemId', inject(function (Item) { - Item.receive(3, 'newestItemId'); - - expect(Item.getNewestItemId()).toBe(3); - })); - - - it('should receive the newestItemId', inject(function (Item) { - Item.receive(2, 'starred'); - - expect(Item.getStarredCount()).toBe(2); - })); - - - it('should receive items', inject(function (Item) { - Item.receive([ - { - id: 3 - }, - { - id: 4 - } - ], 'items'); - - expect(Item.size()).toBe(2); - })); - -}); \ No newline at end of file diff --git a/js/tests/unit/service/ModelSpec.js b/js/tests/unit/service/ModelSpec.js deleted file mode 100644 index 57b0f422c..000000000 --- a/js/tests/unit/service/ModelSpec.js +++ /dev/null @@ -1,129 +0,0 @@ -/** - * ownCloud - News - * - * This file is licensed under the Affero General Public License version 3 or - * later. See the COPYING file. - * - * @author Bernhard Posselt - * @copyright Bernhard Posselt 2014 - */ -describe('Model', function () { - 'use strict'; - - var childModel; - - beforeEach(module('News')); - - beforeEach(inject(function (Model) { - var ChildModel = function () { - Model.call(this, 'id'); - }; - ChildModel.prototype = Object.create(Model.prototype); - - childModel = new ChildModel(); - })); - - - it('should receive an object', function () { - var objects = [ - { - id: 2 - }, - { - id: 3 - } - ]; - - childModel.receive(objects); - - expect(childModel.size()).toBe(2); - }); - - - it('should add an object', function () { - var object = { - id: 3, - name: 'test' - }; - childModel.add(object); - - expect(childModel.get(3)).toBe(object); - }); - - - it('should overwrite an object if it already exists', function () { - var object1, - object2; - - object1 = { - id: 3, - name: 'test', - test: 'ho' - }; - - object2 = { - id: 3, - name: 'test2' - }; - - childModel.add(object1); - childModel.add(object2); - - expect(childModel.get(3).name).toBe('test2'); - expect(childModel.get(3).test).toBe('ho'); - expect(childModel.size()).toBe(1); - }); - - - it('should delete a model', function () { - var object1, - object2; - - object1 = { - id: 3, - name: 'test', - test: 'ho' - }; - - object2 = { - id: 4, - name: 'test2' - }; - - childModel.add(object1); - childModel.add(object2); - - childModel.delete(3); - - expect(childModel.get(3)).not.toBeDefined(); - expect(childModel.get(4).name).toBe('test2'); - expect(childModel.size()).toBe(1); - }); - - - it('should clear all models', function () { - var object1, - object2; - - object1 = { - id: 3, - name: 'test', - test: 'ho' - }; - - object2 = { - id: 4, - name: 'test2' - }; - - childModel.add(object1); - childModel.add(object2); - - childModel.clear(); - - expect(childModel.get(3)).not.toBeDefined(); - expect(childModel.get(4)).not.toBeDefined(); - expect(childModel.size()).toBe(0); - }); - -}); \ No newline at end of file diff --git a/js/tests/unit/service/PublisherSpec.js b/js/tests/unit/service/PublisherSpec.js index 8d7b65e58..06cd48c6a 100644 --- a/js/tests/unit/service/PublisherSpec.js +++ b/js/tests/unit/service/PublisherSpec.js @@ -13,10 +13,10 @@ describe('Publisher', function () { beforeEach(module('News')); it('should should publish on all possible channels', inject(function (Publisher) { - var obj = { receive: jasmine.createSpy('receive') }; + Publisher.subscribe(obj).toChannels('test'); Publisher.publishAll({ @@ -28,10 +28,10 @@ describe('Publisher', function () { it('should should publish on all possible channels', inject(function (Publisher) { - var obj = { receive: jasmine.createSpy('receive') }; + Publisher.subscribe(obj).toChannels('test', 'tiny'); Publisher.publishAll({ -- cgit v1.2.3