diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-09-11 03:55:52 +0200 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-09-11 03:55:52 +0200 |
commit | ea9ebd4826fe9807af5bc17e786b3dc58f163970 (patch) | |
tree | 2894b40614ebe977797cea5745b215e2a2851f61 /js/service | |
parent | 594b92f649d8ed8a705f1af23639463078170d46 (diff) |
port to es5 and add es6 shims for object prototypes instead
Diffstat (limited to 'js/service')
-rw-r--r-- | js/service/FeedResource.js | 305 | ||||
-rw-r--r-- | js/service/FolderResource.js | 133 | ||||
-rw-r--r-- | js/service/ItemResource.js | 199 | ||||
-rw-r--r-- | js/service/Loading.js | 4 | ||||
-rw-r--r-- | js/service/Publisher.js | 31 | ||||
-rw-r--r-- | js/service/Resource.js | 117 | ||||
-rw-r--r-- | js/service/SettingsResource.js | 22 |
7 files changed, 420 insertions, 391 deletions
diff --git a/js/service/FeedResource.js b/js/service/FeedResource.js index 4466b502a..5c85fdc4e 100644 --- a/js/service/FeedResource.js +++ b/js/service/FeedResource.js @@ -7,221 +7,224 @@ * @author Bernhard Posselt <dev@bernhard-posselt.com> * @copyright Bernhard Posselt 2014 */ -app.factory('FeedResource', (Resource, $http, BASE_URL) => { +app.factory('FeedResource', function (Resource, $http, BASE_URL) { 'use strict'; - class FeedResource extends Resource { - - constructor ($http, BASE_URL) { - super($http, BASE_URL, 'url'); - this.ids = {}; - this.unreadCount = 0; - this.folderUnreadCount = {}; - this.folderIds = {}; - this.deleted = null; - } + var FeedResource = function ($http, BASE_URL) { + Resource.call(this, $http, BASE_URL, 'url'); + this.ids = {}; + this.unreadCount = 0; + this.folderUnreadCount = {}; + this.folderIds = {}; + this.deleted = null; + }; + FeedResource.prototype = Object.create(Resource.prototype); - receive (data) { - super.receive(data); - this.updateUnreadCache(); - this.updateFolderCache(); - } + FeedResource.prototype.receive = function (data) { + Resource.prototype.receive.call(this, data); + this.updateUnreadCache(); + this.updateFolderCache(); + }; - updateUnreadCache () { - this.unreadCount = 0; - this.folderUnreadCount = {}; + FeedResource.prototype.updateUnreadCache = function () { + this.unreadCount = 0; + this.folderUnreadCount = {}; - for (let value of this.values) { - if (value.unreadCount) { - this.unreadCount += value.unreadCount; - } - if (value.folderId !== undefined) { - this.folderUnreadCount[value.folderId] = - this.folderUnreadCount[value.folderId] || 0; - this.folderUnreadCount[value.folderId] += value.unreadCount; - } + var self = this; + this.values.forEach(function (feed) { + if (feed.unreadCount) { + self.unreadCount += feed.unreadCount; } - } + if (feed.folderId !== undefined) { + self.folderUnreadCount[feed.folderId] = + self.folderUnreadCount[feed.folderId] || 0; + self.folderUnreadCount[feed.folderId] += feed.unreadCount; + } + }); + }; - updateFolderCache () { - this.folderIds = {}; + FeedResource.prototype.updateFolderCache = function () { + this.folderIds = {}; - for (let feed of this.values) { - this.folderIds[feed.folderId] = - this.folderIds[feed.folderId] || []; - this.folderIds[feed.folderId].push(feed); - } - } + var self = this; + this.values.forEach(function (feed) { + self.folderIds[feed.folderId] = + self.folderIds[feed.folderId] || []; + self.folderIds[feed.folderId].push(feed); + }); + }; - add (value) { - super.add(value); - if (value.id !== undefined) { - this.ids[value.id] = this.hashMap[value.url]; - } + FeedResource.prototype.add = function (value) { + Resource.prototype.add.call(this, value); + if (value.id !== undefined) { + this.ids[value.id] = this.hashMap[value.url]; } + }; - delete (url) { - let feed = this.get(url); - this.deleted = feed; - delete this.ids[feed.id]; + FeedResource.prototype.delete = function (url) { + var feed = this.get(url); + this.deleted = feed; + delete this.ids[feed.id]; - super.delete(url); + Resource.prototype.delete.call(this, url); - this.updateUnreadCache(); - this.updateFolderCache(); + this.updateUnreadCache(); + this.updateFolderCache(); - return this.http.delete(`${this.BASE_URL}/feeds/${feed.id}`); - } + return this.http.delete(this.BASE_URL + '/feeds/' + feed.id); + }; - markRead () { - for (let feed of this.values) { - feed.unreadCount = 0; - } - this.unreadCount = 0; - this.folderUnreadCount = {}; - } + FeedResource.prototype.markRead = function () { + this.values.forEach(function (feed) { + feed.unreadCount = 0; + }); + this.unreadCount = 0; + this.folderUnreadCount = {}; + }; - markFeedRead (feedId) { - this.ids[feedId].unreadCount = 0; - this.updateUnreadCache(); - } + FeedResource.prototype.markFeedRead = function (feedId) { + this.ids[feedId].unreadCount = 0; + this.updateUnreadCache(); + }; - markFolderRead (folderId) { - for (let feed of this.values) { - if (feed.folderId === folderId) { - feed.unreadCount = 0; - } - } - this.updateUnreadCache(); - } + FeedResource.prototype.markFolderRead = function (folderId) { + this.values.forEach(function (feed) { + if (feed.folderId === folderId) { + feed.unreadCount = 0; + } + }); - markItemOfFeedRead (feedId) { - this.ids[feedId].unreadCount -= 1; - this.updateUnreadCache(); - } + this.updateUnreadCache(); + }; - markItemsOfFeedsRead (feedIds) { - for (let feedId of feedIds) { - this.ids[feedId].unreadCount -= 1; - } - this.updateUnreadCache(); - } + FeedResource.prototype.markItemOfFeedRead = function (feedId) { + this.ids[feedId].unreadCount -= 1; + this.updateUnreadCache(); + }; - markItemOfFeedUnread (feedId) { - this.ids[feedId].unreadCount += 1; - this.updateUnreadCache(); - } + FeedResource.prototype.markItemsOfFeedsRead = function (feedIds) { + var self = this; + feedIds.forEach(function (feedId) { + self.ids[feedId].unreadCount -= 1; + }); + this.updateUnreadCache(); + }; - getUnreadCount () { - return this.unreadCount; - } + FeedResource.prototype.markItemOfFeedUnread = function (feedId) { + this.ids[feedId].unreadCount += 1; + this.updateUnreadCache(); + }; - getFolderUnreadCount (folderId) { - return this.folderUnreadCount[folderId]; - } + FeedResource.prototype.getUnreadCount = function () { + return this.unreadCount; + }; - getByFolderId (folderId) { - return this.folderIds[folderId] || []; - } + FeedResource.prototype.getFolderUnreadCount = function (folderId) { + return this.folderUnreadCount[folderId]; + }; - getById (feedId) { - return this.ids[feedId]; - } + FeedResource.prototype.getByFolderId = function (folderId) { + return this.folderIds[folderId] || []; + }; - rename (url, name) { - let feed = this.get(url); - feed.title = name; - return this.http({ - method: 'POST', - url: `${this.BASE_URL}/feeds/${feed.id}/rename`, - data: { - feedTitle: name - } - }); - } + FeedResource.prototype.getById = function (feedId) { + return this.ids[feedId]; + }; - move (url, folderId) { - let feed = this.get(url); - feed.folderId = folderId; + FeedResource.prototype.rename = function (url, name) { + var feed = this.get(url); + feed.title = name; - this.updateFolderCache(); + return this.http({ + method: 'POST', + url: this.BASE_URL + '/feeds/' + feed.id + '/rename', + data: { + feedTitle: name + } + }); + }; - return this.http({ - method: 'POST', - url: `${this.BASE_URL}/feeds/${feed.id}/move`, - data: { - parentFolderId: folderId - } - }); - } + FeedResource.prototype.move = function (url, folderId) { + var feed = this.get(url); + feed.folderId = folderId; + this.updateFolderCache(); - create (url, folderId, title=null) { - if (title) { - title = title.toUpperCase(); + return this.http({ + method: 'POST', + url: this.BASE_URL + '/feeds/' + feed.id + '/move', + data: { + parentFolderId: folderId } + }); - let feed = { - url: url, - folderId: folderId, - title: title, - faviconLink: '../css/loading.gif' - }; + }; - if (!this.get(url)) { - this.add(feed); - } - this.updateFolderCache(); + FeedResource.prototype.create = function (url, folderId, title) { + if (title) { + title = title.toUpperCase(); + } - console.log(feed); + var feed = { + url: url, + folderId: folderId, + title: title, + faviconLink: '../css/loading.gif' + }; - /*return this.http({ - method: 'POST', - url: `${this.BASE_URL}/feeds`, - data: { - url: url, - parentFolderId: folderId, - title: title - } - });*/ + if (!this.get(url)) { + this.add(feed); } + this.updateFolderCache(); - undoDelete () { - if (this.deleted) { - this.add(this.deleted); + console.log(feed); - return this.http.post( - `${this.BASE_URL}/feeds/${this.deleted.id}/restore` - ); + /*return this.http({ + method: 'POST', + url: this.BASE_URL + '/feeds', + data: { + url: url, + parentFolderId: folderId, + title: title } + });*/ + }; + + + FeedResource.prototype.undoDelete = function () { + if (this.deleted) { + this.add(this.deleted); - this.updateFolderCache(); - this.updateUnreadCache(); + return this.http.post( + this.BASE_URL + '/feeds/${this.deleted.id}/restore' + ); } + this.updateFolderCache(); + this.updateUnreadCache(); + }; - } return new FeedResource($http, BASE_URL); });
\ No newline at end of file diff --git a/js/service/FolderResource.js b/js/service/FolderResource.js index 2e67fa690..6453f33b8 100644 --- a/js/service/FolderResource.js +++ b/js/service/FolderResource.js @@ -7,100 +7,97 @@ * @author Bernhard Posselt <dev@bernhard-posselt.com> * @copyright Bernhard Posselt 2014 */ -app.factory('FolderResource', (Resource, $http, BASE_URL) => { +app.factory('FolderResource', function (Resource, $http, BASE_URL) { 'use strict'; - class FolderResource extends Resource { + var FolderResource = function ($http, BASE_URL) { + Resource.call(this, $http, BASE_URL, 'name'); + this.deleted = null; + }; - constructor ($http, BASE_URL) { - super($http, BASE_URL, 'name'); - this.deleted = null; - } + FolderResource.prototype = Object.create(Resource.prototype); + FolderResource.prototype.delete = function (folderName) { + var folder = this.get(folderName); + this.deleted = folder; - delete (folderName) { - let folder = this.get(folderName); - this.deleted = folder; + Resource.prototype.delete.call(this, folderName); - super.delete(folderName); + return this.http.delete(this.BASE_URL + '/folders/' + folder.id); + }; - return this.http.delete(`${this.BASE_URL}/folders/${folder.id}`); - } + FolderResource.prototype.toggleOpen = function (folderName) { + var folder = this.get(folderName); + folder.opened = !folder.opened; - toggleOpen (folderName) { - let folder = this.get(folderName); - folder.opened = !folder.opened; + return this.http({ + url: this.BASE_URL + '/folders/' + folder.id + '/open', + method: 'POST', + data: { + folderId: folder.id, + open: folder.opened + } + }); + }; - return this.http({ - url: `${this.BASE_URL}/folders/${folder.id}/open`, - method: 'POST', - data: { - folderId: folder.id, - open: folder.opened - } - }); - } + FolderResource.prototype.rename = function (folderName, toFolderName) { + toFolderName = toFolderName.toUpperCase(); + var folder = this.get(folderName); - rename (folderName, toFolderName) { - toFolderName = toFolderName.toUpperCase(); - let folder = this.get(folderName); + // still do http request if folder exists but dont change the name + // to have one point of failure + if (!this.get(toFolderName)) { + folder.name = toFolderName; - // still do http request if folder exists but dont change the name - // to have one point of failure - if (!this.get(toFolderName)) { - folder.name = toFolderName; + delete this.hashMap[folderName]; + this.hashMap[toFolderName] = folder; + } - delete this.hashMap[folderName]; - this.hashMap[toFolderName] = folder; + return this.http({ + url: this.BASE_URL + '/folders/' + folder.id + '/rename', + method: 'POST', + data: { + folderName: toFolderName } + }); + }; - return this.http({ - url: `${this.BASE_URL}/folders/${folder.id}/rename`, - method: 'POST', - data: { - folderName: toFolderName - } - }); - } + FolderResource.prototype.create = function (folderName) { + folderName = folderName.toUpperCase(); - create (folderName) { - folderName = folderName.toUpperCase(); + // still do http request if folder exists but dont change the name + // to have one point of failure + if (!this.get(folderName)) { + var folder = { + name: folderName + }; - // still do http request if folder exists but dont change the name - // to have one point of failure - if (!this.get(folderName)) { - let folder = { - name: folderName - }; + this.add(folder); + } - this.add(folder); + return this.http({ + url: this.BASE_URL + '/folders', + method: 'POST', + data: { + folderName: folderName } - - return this.http({ - url: `${this.BASE_URL}/folders`, - method: 'POST', - data: { - folderName: folderName - } - }); - } + }); + }; - undoDelete () { - if (this.deleted) { - this.add(this.deleted); + FolderResource.prototype.undoDelete = function () { + if (this.deleted) { + this.add(this.deleted); - return this.http.post( - `${this.BASE_URL}/folders/${this.deleted.id}/restore` - ); - } + return this.http.post( + this.BASE_URL + '/folders/' + this.deleted.id + '/restore' + ); } + }; - } - return new FolderResource($http, BASE_URL); });
\ No newline at end of file diff --git a/js/service/ItemResource.js b/js/service/ItemResource.js index 420e20775..cc67cc003 100644 --- a/js/service/ItemResource.js +++ b/js/service/ItemResource.js @@ -7,134 +7,155 @@ * @author Bernhard Posselt <dev@bernhard-posselt.com> * @copyright Bernhard Posselt 2014 */ -app.factory('ItemResource', (Resource, $http, BASE_URL, ITEM_BATCH_SIZE) => { +app.factory('ItemResource', function (Resource, $http, BASE_URL, + ITEM_BATCH_SIZE) { 'use strict'; - class ItemResource extends Resource { + var ItemResource = function ($http, BASE_URL, ITEM_BATCH_SIZE) { + Resource.call(this, $http, BASE_URL); + this.starredCount = 0; + this.batchSize = ITEM_BATCH_SIZE; + }; - constructor ($http, BASE_URL, ITEM_BATCH_SIZE) { - super($http, BASE_URL); - this.starredCount = 0; - this.batchSize = ITEM_BATCH_SIZE; - } + ItemResource.prototype = Object.create(Resource.prototype); - receive (value, channel) { - switch (channel) { + ItemResource.prototype.receive = function (value, channel) { + switch (channel) { - case 'newestItemId': - this.newestItemId = value; - break; + case 'newestItemId': + this.newestItemId = value; + break; - case 'starred': - this.starredCount = value; - break; + case 'starred': + this.starredCount = value; + break; - default: - super.receive(value, channel); - } + default: + Resource.prototype.receive.call(this, value, channel); } + }; - getNewestItemId () { - return this.newestItemId; - } + ItemResource.prototype.getNewestItemId = function () { + return this.newestItemId; + }; + + + ItemResource.prototype.getStarredCount = function () { + return this.starredCount; + }; - getStarredCount () { - return this.starredCount; + ItemResource.prototype.star = function (itemId, isStarred) { + if (isStarred === undefined) { + isStarred = true; } + var it = this.get(itemId); + var url = this.BASE_URL + + '/items/' + it.feedId + '/' + it.guidHash + '/star'; - star (itemId, isStarred=true) { - let it = this.get(itemId); - let url = `${this.BASE_URL}/items/${it.feedId}/${it.guidHash}/star`; + it.starred = isStarred; - it.starred = isStarred; + if (isStarred) { + this.starredCount += 1; + } else { + this.starredCount -= 1; + } - if (isStarred) { - this.starredCount += 1; - } else { - this.starredCount -= 1; + return this.http({ + url: url, + method: 'POST', + data: { + isStarred: isStarred } + }); + }; + - return this.http({ - url: url, - method: 'POST', - data: { - isStarred: isStarred - } - }); + ItemResource.prototype.toggleStar = function (itemId) { + if (this.get(itemId).starred) { + this.star(itemId, false); + } else { + this.star(itemId, true); } + }; - toggleStar (itemId) { - if (this.get(itemId).starred) { - this.star(itemId, false); - } else { - this.star(itemId, true); - } + ItemResource.prototype.markItemRead = function (itemId, isRead) { + if (isRead === undefined) { + isRead = true; } + this.get(itemId).unread = !isRead; + return this.http({ + url: this.BASE_URL + '/items/' + itemId + '/read', + method: 'POST', + data: { + isRead: isRead + } + }); + }; - markItemRead (itemId, isRead=true) { - this.get(itemId).unread = !isRead; - return this.http({ - url: `${this.BASE_URL}/items/${itemId}/read`, - method: 'POST', - data: { - isRead: isRead - } - }); - } + ItemResource.prototype.markItemsRead = function (itemIds) { + var self = this; - markItemsRead (itemIds) { - for (let itemId of itemIds) { - this.get(itemId).unread = false; + itemIds.forEach(function(itemId) { + self.get(itemId).unread = false; + }); + + return this.http({ + url: this.BASE_URL + '/items/read/multiple', + method: 'POST', + data: { + itemIds: itemIds } + }); + }; + - return this.http({ - url: `${this.BASE_URL}/items/read/multiple`, - method: 'POST', - data: { - itemIds: itemIds - } - }); + ItemResource.prototype.markFeedRead = function (feedId, read) { + if (read === undefined) { + read = true; } + var items = this.values.filter(function (element) { + return element.feedId === feedId; + }); - markFeedRead (feedId, read=true) { - for (let item of this.values.filter(i => i.feedId === feedId)) { - item.unread = !read; - } - return this.http.post(`${this.BASE_URL}/feeds/${feedId}/read`); - } + items.forEach(function (item) { + item.unread = !read; + }); + return this.http.post(this.BASE_URL + '/feeds/' + 'feedId' + '/read'); + }; - markRead () { - for (let item of this.values) { - item.unread = false; - } - return this.http.post(`${this.BASE_URL}/items/read`); - } + ItemResource.prototype.markRead = function () { + this.values.forEach(function (item) { + item.unread = false; + }); - autoPage (type, id) { - return this.http({ - url: `${this.BASE_URL}/items`, - method: 'GET', - params: { - type: type, - id: id, - offset: this.size(), - limit: this.batchSize - } - }); - } + return this.http.post(this.BASE_URL + '/items/read'); + }; + + + ItemResource.prototype.autoPage = function (type, id) { + return this.http({ + url: this.BASE_URL + '/items', + method: 'GET', + params: { + type: type, + id: id, + offset: this.size(), + limit: this.batchSize + } + }); + }; - } return new ItemResource($http, BASE_URL, ITEM_BATCH_SIZE); });
\ No newline at end of file diff --git a/js/service/Loading.js b/js/service/Loading.js index c07dba9fe..eb42655a5 100644 --- a/js/service/Loading.js +++ b/js/service/Loading.js @@ -16,11 +16,11 @@ app.service('Loading', function () { autopaging: false }; - this.setLoading = (area, isLoading) => { + this.setLoading = function (area, isLoading) { this.loading[area] = isLoading; }; - this.isLoading = (area) => { + this.isLoading = function (area) { return this.loading[area]; }; diff --git a/js/service/Publisher.js b/js/service/Publisher.js index dd420f629..b5d44c264 100644 --- a/js/service/Publisher.js +++ b/js/service/Publisher.js @@ -14,26 +14,31 @@ app.service('Publisher', function () { this.channels = {}; - this.subscribe = (obj) => { + this.subscribe = function (obj) { + var self = this; + return { - toChannels: (...channels) => { - for (let channel of channels) { - this.channels[channel] = this.channels[channel] || []; - this.channels[channel].push(obj); - } + toChannels: function (channels) { + channels.forEach(function (channel) { + self.channels[channel] = self.channels[channel] || []; + self.channels[channel].push(obj); + }); } }; }; - this.publishAll = (data) => { - for (let [channel, messages] of items(data)) { - if (this.channels[channel] !== undefined) { - for (let listener of this.channels[channel]) { - listener.receive(messages, channel); - } + this.publishAll = function (data) { + var self = this; + + Object.keys(data).forEach(function (channel) { + var listeners = self.channels[channel]; + if (listeners !== undefined) { + listeners.forEach(function (listener) { + listener.receive(data[channel], channel); + }); } - } + }); }; });
\ No newline at end of file diff --git a/js/service/Resource.js b/js/service/Resource.js index 072abca18..c2633c83e 100644 --- a/js/service/Resource.js +++ b/js/service/Resource.js @@ -7,85 +7,84 @@ * @author Bernhard Posselt <dev@bernhard-posselt.com> * @copyright Bernhard Posselt 2014 */ -app.factory('Resource', () => { +app.factory('Resource', function () { 'use strict'; - class Resource { - - - constructor (http, BASE_URL, id='id') { - this.id = id; - this.values = []; - this.hashMap = {}; - this.http = http; - this.BASE_URL = BASE_URL; - } - - - receive (objs) { - for (let obj of objs) { - this.add(obj); - } + var Resource = function (http, BASE_URL, id) { + this.id = id || 'id'; + this.values = []; + this.hashMap = {}; + this.http = http; + this.BASE_URL = BASE_URL; + }; + + + Resource.prototype.receive = function (objs) { + var self = this; + objs.forEach(function (obj) { + self.add(obj); + }); + }; + + + Resource.prototype.add = function (obj) { + var existing = this.hashMap[obj[this.id]]; + + if (existing === undefined) { + this.values.push(obj); + this.hashMap[obj[this.id]] = obj; + } else { + // copy values from new to old object if it exists already + Object.keys(obj).forEach(function (key) { + existing[key] = obj[key]; + }); } + }; - add (obj) { - let existing = this.hashMap[obj[this.id]]; + Resource.prototype.size = function () { + return this.values.length; + }; - if (existing === undefined) { - this.values.push(obj); - this.hashMap[obj[this.id]] = obj; - } else { - // copy values from new to old object if it exists already - for (let [key, value] of items(obj)) { - existing[key] = value; - } - } - } + Resource.prototype.get = function (id) { + return this.hashMap[id]; + }; - size () { - return this.values.length; - } + Resource.prototype.delete = function (id) { + // find index of object that should be deleted + var self = this; + var deleteAtIndex = this.values.findIndex(function(element) { + return element[self.id] === id; + }); - get (id) { - return this.hashMap[id]; + if (deleteAtIndex !== undefined) { + this.values.splice(deleteAtIndex, 1); } - - delete (id) { - // find index of object that should be deleted - let deleteAtIndex = this.values.findIndex(e => e[this.id] === id); - - if (deleteAtIndex !== undefined) { - this.values.splice(deleteAtIndex, 1); - } - - if (this.hashMap[id] !== undefined) { - delete this.hashMap[id]; - } + if (this.hashMap[id] !== undefined) { + delete this.hashMap[id]; } + }; - clear () { - this.hashMap = {}; + Resource.prototype.clear = function () { + this.hashMap = {}; - // http://stackoverflow.com/questions/1232040 - // 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(); - } + // http://stackoverflow.com/questions/1232040 + // this is the fastes way to empty an array when you want to kee |