summaryrefslogtreecommitdiffstats
path: root/js/service
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2014-05-20 17:01:59 +0200
committerBernhard Posselt <dev@bernhard-posselt.com>2014-05-20 17:01:59 +0200
commit5a7b1e0c36c2dd564f44568de766118b07be49ad (patch)
tree23b9f80bb4509dd51948c3851660f5fa3754031d /js/service
parentb5d5b2b22a2b4aabd4c9bb5c8b89aae10facc735 (diff)
rename model to resource
Diffstat (limited to 'js/service')
-rw-r--r--js/service/FeedResource.js20
-rw-r--r--js/service/FolderResource.js20
-rw-r--r--js/service/ItemResource.js44
-rw-r--r--js/service/Resource.js93
4 files changed, 177 insertions, 0 deletions
diff --git a/js/service/FeedResource.js b/js/service/FeedResource.js
new file mode 100644
index 000000000..09de0a607
--- /dev/null
+++ b/js/service/FeedResource.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 <dev@bernhard-posselt.com>
+ * @copyright Bernhard Posselt 2014
+ */
+app.factory('FeedResource', function (Resource, $http) {
+ 'use strict';
+
+ var FeedResource = function ($http) {
+ Resource.call(this, 'url', $http);
+ };
+
+ FeedResource.prototype = Object.create(Resource.prototype);
+
+ return new FeedResource($http);
+}); \ No newline at end of file
diff --git a/js/service/FolderResource.js b/js/service/FolderResource.js
new file mode 100644
index 000000000..30026b9a2
--- /dev/null
+++ b/js/service/FolderResource.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 <dev@bernhard-posselt.com>
+ * @copyright Bernhard Posselt 2014
+ */
+app.factory('FolderResource', function (Resource, $http) {
+ 'use strict';
+
+ var FolderResource = function ($http) {
+ Resource.call(this, 'name', $http);
+ };
+
+ FolderResource.prototype = Object.create(Resource.prototype);
+
+ return new FolderResource($http);
+}); \ No newline at end of file
diff --git a/js/service/ItemResource.js b/js/service/ItemResource.js
new file mode 100644
index 000000000..9b13f07c7
--- /dev/null
+++ b/js/service/ItemResource.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 <dev@bernhard-posselt.com>
+ * @copyright Bernhard Posselt 2014
+ */
+app.factory('ItemResource', function (Resource, $http) {
+ 'use strict';
+
+ var ItemResource = function ($http) {
+ Resource.call(this, 'id', $http);
+ };
+
+ ItemResource.prototype = Object.create(Resource.prototype);
+
+ ItemResource.prototype.receive = function (value, channel) {
+ switch (channel) {
+
+ case 'newestItemId':
+ this.newestItemId = value;
+ break;
+
+ case 'starred':
+ this.starredCount = value;
+ break;
+ default:
+ Resource.prototype.receive.call(this, value, channel);
+ }
+ };
+
+ ItemResource.prototype.getNewestItemId = function () {
+ return this.newestItemId;
+ };
+
+ ItemResource.prototype.getStarredCount = function () {
+ return this.starredCount;
+ };
+
+
+ return new ItemResource($http);
+}); \ No newline at end of file
diff --git a/js/service/Resource.js b/js/service/Resource.js
new file mode 100644
index 000000000..ecae0fcfd
--- /dev/null
+++ b/js/service/Resource.js
@@ -0,0 +1,93 @@
+/**
+ * ownCloud - News
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @copyright Bernhard Posselt 2014
+ */
+app.factory('Resource', function () {
+ 'use strict';
+
+ var Resource = function (id, http) {
+ this.id = id;
+ this.values = [];
+ this.hashMap = {};
+ this.http = http;
+ };
+
+ Resource.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();
+ }
+ },
+
+ getAll: function () {
+ return this.values;
+ }
+ };
+
+ return Resource;
+}); \ No newline at end of file