mirror of
https://github.com/chylex/Nextcloud-News.git
synced 2025-08-17 00:31:42 +02:00
.github
.tx
appinfo
bin
css
docs
img
js
admin
app
controller
directive
filter
gui
plugin
service
FeedResource.js
FolderResource.js
ItemResource.js
Loading.js
OPMLImporter.js
OPMLParser.js
Publisher.js
Resource.js
SettingsResource.js
ShareResource.js
tests
.jshintignore
.jshintrc
README.md
gulpfile.js
karma.conf.js
package-lock.json
package.json
protractor.conf.js
l10n
lib
screenshots
templates
tests
.editorconfig
.gitignore
.mailmap
AUTHORS.md
CHANGELOG.md
CONTRIBUTING.md
COPYING
Makefile
README.md
composer.json
composer.lock
mkdocs.yml
phpstan.neon.dist
phpunit.xml
90 lines
2.1 KiB
JavaScript
90 lines
2.1 KiB
JavaScript
/**
|
|
* Nextcloud - 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 (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];
|
|
});
|
|
}
|
|
};
|
|
|
|
|
|
Resource.prototype.size = function () {
|
|
return this.values.length;
|
|
};
|
|
|
|
|
|
Resource.prototype.get = function (id) {
|
|
return this.hashMap[id];
|
|
};
|
|
|
|
|
|
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;
|
|
});
|
|
|
|
if (deleteAtIndex !== undefined) {
|
|
this.values.splice(deleteAtIndex, 1);
|
|
}
|
|
|
|
if (this.hashMap[id] !== undefined) {
|
|
delete this.hashMap[id];
|
|
}
|
|
};
|
|
|
|
|
|
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();
|
|
}
|
|
};
|
|
|
|
|
|
Resource.prototype.getAll = function () {
|
|
return this.values;
|
|
};
|
|
|
|
|
|
return Resource;
|
|
}); |