para-client-js
Version:
JavaScript Client for Para
319 lines (285 loc) • 6.44 kB
JavaScript
/*
* Copyright 2013-2022 Erudika. https://erudika.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For issues and patches go to: https://github.com/erudika
*/
/* global encodeURIComponent */
;
export default class ParaObject {
constructor(id, type) {
this.id = id || null;
this.type = type || "sysprop";
this.name = "ParaObject";
this.stored = true;
this.indexed = true;
this.cached = true;
this.version = 0;
}
/**
* The id of an object. Usually an autogenerated unique string of numbers.
*
* @return the id
*/
getId() {
return this.id;
}
/**
* Sets a new id. Must not be null or empty.
*
* @param {String} id the new id
*/
setId(id) {
this.id = id;
}
/**
* The name of the object. Can be anything.
*
* @return {String} the name. default: [type id]
*/
getName() {
return this.name;
}
/**
* Sets a new name. Must not be null or empty.
*
* @param {String} name the new name
*/
setName(name) {
this.name = name;
}
/**
* The application name. Added to support multiple separate apps.
* Every object must belong to an app.
*
* @return {String} the app id (name). default: para
*/
getAppid() {
return this.appid;
}
/**
* Sets a new app name. Must not be null or empty.
*
* @param {String} appid the new app id (name)
*/
setAppid(appid) {
this.appid = appid;
}
/**
* The id of the parent object.
*
* @return {String} the id of the parent or null
*/
getParentid() {
return this.parentid;
}
/**
* Sets a new parent id. Must not be null or empty.
*
* @param {String} parentid a new id
*/
setParentid(parentid) {
this.parentid = parentid;
}
/**
* The name of the object's class. This is equivalent to {@link Class#getSimpleName()}.toLowerCase().
*
* @return {String} the simple name of the class
*/
getType() {
return this.type;
}
/**
* Sets a new object type. Must not be null or empty.
*
* @param {String} type a new type
*/
setType(type) {
this.type = type;
}
/**
* The id of the user who created this. Should point to a {@link User} id.
*
* @return {String} the id or null
*/
getCreatorid() {
return this.creatorid;
}
/**
* Sets a new creator id. Must not be null or empty.
*
* @param {String} creatorid a new id
*/
setCreatorid(creatorid) {
this.creatorid = creatorid;
}
/**
* The URI of this object. For example: /user/123.
*
* @return {String} the URI
*/
getObjectURI() {
var def = "/" + urlEncode(this.getType());
return this.id ? def + "/" + urlEncode(this.id) : def;
}
/**
* The time when the object was created, in milliseconds.
*
* @return {Number} the timestamp of creation
*/
getTimestamp() {
return this.timestamp;
}
/**
* Sets the timestamp.
*
* @param {Number} timestamp a new timestamp in milliseconds.
*/
setTimestamp(timestamp) {
this.timestamp = timestamp;
}
/**
* The last time this object was updated. Timestamp in ms.
*
* @return {Number} timestamp in milliseconds
*/
getUpdated() {
return this.updated;
}
/**
* Sets the last updated timestamp.
*
* @param {Number} updated a new timestamp
*/
setUpdated(updated) {
this.updated = updated;
}
/**
* The tags associated with this object. Tags must not be null or empty.
*
* @return {Array} a set of tags, or an empty set
*/
getTags() {
return this.id;
}
/**
* Merges the given tags with existing tags.
*
* @param {Array} tags the additional tags, or clears all tags if set to null
*/
setTags(tags) {
this.tags = tags;
}
/**
* The votes associated with this object.
*
* @return {Number} votes or 0
*/
getVotes() {
return this.votes;
}
/**
* Sets the votes.
*
* @param {Number} votes
*/
setVotes(votes) {
this.votes = votes;
}
/**
* The version of this object.
*
* @return {Number} version
*/
getVersion() {
return this.version;
}
/**
* Sets the version.
*
* @param {Number} version
*/
setVersion(version) {
this.version = version;
}
/**
* Boolean flag which controls whether this object is stored
* in the database or not. Default is true.
*
* @return {Boolean} true if this object is stored in DB.
*/
getStored() {
return this.stored;
}
/**
* Sets the "isStored" flag.
*
* @param {Boolean} isStored when set to true, object is stored in DB.
*/
setStored(isStored) {
this.stored = isStored;
}
/**
* Boolean flat which controls whether this object is indexed
* by the search engine. Default is true.
*
* @return {Boolean} true if this object is indexed
*/
getIndexed() {
return this.indexed;
}
/**
* Sets the "isIndexed" flag.
*
* @param {Boolean} isIndexed when set to true, object is indexed.
*/
setIndexed(isIndexed) {
this.indexed = isIndexed;
}
/**
* Boolean flat which controls whether this object is cached.
* Default is true.
*
* @return {Boolean} true if this object is cached on update() and create().
*/
getCached() {
return this.cached;
}
/**
* Sets the "isCached" flag.
*
* @param {Boolean} isCached when set to true, object is cached.
*/
setCached(isCached) {
this.cached = isCached;
}
/**
* Populates this object with data from a map.
* @param {Object} map
* @return {ParaObject} this
*/
setFields(map) {
if (map && map instanceof Object) {
for (var key in map) {
this[key] = map[key];
}
}
return this;
}
}
function urlEncode(path) {
return encodeURIComponent(path).replace(/[!'()*]/g, function (c) {
return '%' + c.charCodeAt(0).toString(16).toUpperCase();
});
}