JSMA category
Overview
The JSMA category provides functionality for finding, creating, editing and deleting category contents. It is currently being used for sorting entities such as products and categories with similar properties into one bigger entity which can then provide its children with parent attributes. The JSMACategory can provide an insight into its dynamic contents.
What can JSMA category do for me?
You may use the JSMA category to sort an array of similar products and/or categories. If you assign attributes to a category, the children may inherit these attributes. It provides a convenient way of assigning the same attribute to several contents at once.
Index
Usage
writing data
Here is an example on how to write data to a JSMA category:
... if (entry.getClassName() === "ProductJsma") { var child = new PathProduct(entry.name); category.addChild(child); } ...
reading data
Here is an example on how to read data from a JSMA category:
CatLibrary.prototype.getCategory = function(category) { var cat, _i, _len, _ref; _ref = this.categories; for (_i = 0, _len = _ref.length; _i < _len; _i++) { cat = _ref[_i]; if (cat.name == category) { return cat; } } };
Methods
The JSMA categoryprovides the following methods:
void category.addChild(child, [index])
Adds any child of type CategoryJSMA or ProductJSMA to a category.
Parameter child (required)
child can be a portal, file or content, depending on the category-settings (content-type)
Parameter index (optional)
If provided, the index no. provides the child's position within the category.
Example
... if (entry.getClassName() === "ProductJsma") { var child = new PathProduct(entry.name); category.addChild(child); } ...
category[] category.categories
Returns an array of sub-categories contained in a category.
Example
CatLibrary.prototype.getCategory = function(category) { var cat, _i, _len, _ref; _ref = this.categories; for (_i = 0, _len = _ref.length; _i < _len; _i++) { cat = _ref[_i]; if (cat.name == category) { return cat; } } };
category[] category.categoryNames
Returns an array of the "name" attribute of sub-categories contained in a category.
Example
function getCategoriesByName(category) { return category.categoryNames; };
category[] category.children
Returns an array containing the children of a category as objects.
Example
... category.children.forEach ( function ( child ) { // retrieve the child's type var childClassName = child.getClassName(); if (childClassName === "CategoryJsma") { // handle behavior if the respective child is a category } else if (childClassName === "ProductJsma") { // handle behavior if the respective child is a product } else { // throw an error if the respective child is neither category nor product throw new Error("Unknown child in category of class " + childClassName); } }); ...
Currently the only children to be expected within a category are either categories or products, so the above case will be the typical way of handling a category's children in almost every case.
string[] category.childrenNames
Returns an array containing the FQNs (Fully qualified names) of the immediate children of a category regardless of their type.
Example
var searchCat = categories.find("product.design*", {limit: 10000}); searchCat.forEach(function(cat) { if (cat.childrenNames.join(",").indexOf("geschenk") !== -1) { sortingCats.push(cat.name); } });
category category.clone(name)
Clones the category and returns the copy as a JSMA category.
Slice aware
This entity is slice aware, which means that to use this facility:
- there must be a logged in user in the context of the execution
- this user must be currently within a slice
- this user must have the necessary privileges to edit the entity (IFE → System → Benutzer → Roles → Content-Management)
Example
... var clonedCat = category.clone() // => category ...
Parameter name: (optional)
category.contentType
Returns the content type of a category - Any, File, Content, Category, Portal, Product
Example
... if (category.contentType === "PRODUCT") { ... } ...
category.delete()
Deletes a category.
Example
... if (category.children.length === 0) { category.delete() } ...
string category.description
Retrieves a category's description string.
Example
... if (this.descriptionPanel) { this.descriptionPanel.text(category.description); } ...
string category.descriptionUnprocessed
Retrieves a category's unprocessed description string.
Example
... var descUnpro = category.descriptionUnprocessed; ...
category.flexAttributes
Returns a complete array of all flex attribute keys available with a category.
Example
... var attributes = category.flexAttributes; ...
string category.get(key)
Returns the value of key as string or null if no value is set under the key.
Example
... var url = category.get(rhino.category.url); if (url) { window.location = url; } ...
Parameter key:string (required)
The key to be looked up in the flexAttributes of the order.
string category.cacheKey
Returns a string that you can use in the fragment cache. This string will change whenever the category or one its children are changed.
Example
... category.cacheKey # => "12589387317632578294" ...
category.getCategories(deep, maxDepth)
Returns an array of sub-categories of a category as objects.
Example
... var categories = category.getCategories(deep,5); ...
Parameter deep (required)
Parameter maxDepth (required)
category.getChildren(deep, maxDepth)
Returns an array of children of a category as objects.
Example
... var children = category.getChildren(deep,5); ...
Parameter deep (required)
Parameter maxDepth (required)
category.getClassName
Returns 'CategoryJsma' for a valid category.
Example
... var className = category.getClassName; if (className !== "CategoryJsma") { return false; } ...
You may use this to test against wrongful contents as demonstrated in category.children.
category.getContents(deep, maxDepth)
Returns an array of contents of a category
Example
... var contents = category.getContents(deep,5); ...
Parameter deep (required)
Parameter maxDepth (required)
category.getFiles(deep, maxDepth)
Returns an array of files of a category
Example
... var files = category.getFiles(deep,5); ...
Parameter deep (required)
Parameter maxDepth (required)
category.getHighestPrice([depth])
Returns the highest price iterating through all sub-categories with a depth equal to the provided parameter.
Example
Object.defineProperty(this, "highestPrice", { get : function () { return Math.round(self.categoryJsma.getHighestPrice(5) * 100); } });
Parameter depth (optional)
Use a number 0 or bigger.
category.getLowestPrice([depth])
Returns the lowest price iterating through all sub-categories with a depth equal to the provided parameter.
Example
Object.defineProperty(this, "lowestPrice", { get : function () { return Math.round(self.categoryJsma.getLowestPrice(5) * 100); } });
Parameter depth (optional)
Use a number 0 or bigger.
Providing a depth parameter will limit the amount of sub-categories the method is to compare before returning the lowest price. While providing no depth parameter at all will iterate through sub-categories infinitely, providing depth parameter 0 is equal to category.lowestPrice.
category.getPortals(deep, maxDepth)
Returns an array of portals of a category
Example
... var portals = category.getPortals(deep,5); ...
Parameter deep (required)
Parameter maxDepth (required)
category.getProducts(deep, maxDepth)
Returns an array of portals of a category
Example
... var products = category.getProducts(deep,5); ...
Parameter deep (required)
Parameter maxDepth (required)
boolean category.has(key)
Returns a boolean value depending on whether key is present for a given category
Example
... if (category.has("description") === true) { ... } ...
Parameter key:string (required)
The key to be checked in the flexAttributes of the order.
category.highestPrice
Same as category.getHighestPrice(0). Returns the highest price of a category's immediate children.
Example
Object.defineProperty(this, "highestPriceNoDepth", { get : function () { return Math.round(self.categoryJsma.highestPrice * 100); } });
category.image
Returns the actual main image of a category as an object (JSMA file).
Example
... var image = category.image; if (image) { this.categoryImage.attr("src", "/static/image/get?id=" + image.uuid + "&size=" + this.previewMaxWidth + "x" + this.previewMaxHeight"); } ...
As this method is always returning the complete image object, the typical approach to render the image is using one of its attributes, mostly its UUID, as demonstrated in the code snippet above.
category.increment(key)
Increases the value of any flex attribute by 1 permanently. If two or more persons call this method at the same time, they will not receive the same value.
Example
... category.increment("amount") ...
Parameter key:string (required)
The key to be incremented in the flexAttributes of the order.
category.isAny
Returns true when the content type of a category is "Any"
Example
... if (category.isAny === true) { ... } ...
category.isCategories
Returns true when the content type of a category is "Category"
Example
... if (category.isCategories === true) { ... } ...
category.isContents
Returns true when the content type of a category is "Content"
Example
... if (category.isContents === true) { ... } ...
category.isFiles
Returns true when the content type of a category is "File"
Example
... if (category.isFiles === true) { ... } ...
category.isLeaf
Returns true when a category is a leaf
Example
... if (category.isLeaf === true) { ... } ...
category.isMixed
Returns true when a category is a mixed node
Example
... if (category.isMixed === true) { ... } ...
category.isNode
Returns true when a category is a node
Example
... if (category.isNode === true) { ... } ...
category.isPortals
Returns true when the content type of a category is "Portal"
Example
... if (category.isPortals === true) { ... } ...
category.isProducts
Returns true when the content type of a category is "Product"
Example
... if (category.isProducts === true) { ... } ...
category.lowestPrice
Same as category.getLowestPrice(0). Returns the lowest price of a category's immediate children.
Example
... if (category.lowestPrice === true) { ... } ...
category.moveChild(child, Index)
Changes the index of a child within a category (e.g. for sorting purposes).
Example
... category.moveChild(child, 7) ...
Parameter child (required)
Parameter index (required)
category.name
Returns the FQN of a category.
Example
... var fqn = category.name; ...
category.nodeType
Returns the node type of a category - Node, Mixed or Leaf
Example
... if (category.nodeType == "CONTENT") { ... } ...
category.previewImages
Returns an array containing the preview images of a category as objects. Works the same way as category.image for preview images.
Example
... var previewImageId = null; if (category.previewImages && category.previewImages.length > 0) { previewImageId = category.previewImages[0].uuid; } ...
category.productNames
Returns all product FQNs from immediate child products in a category without iterating through sub-categories.
Example
... function categoryContainsProduct(category, productName) { var productNames = category.productNames; var found = false; if (productNames && productNames.length > 0) { for (var j = 0; j < productNames.length; j++) { if (productName === productNames[j]) { found = true; break; } } } return found; } ...
category.products
Returns products contained in a category without iterating through sub-categories (i.e. immediate children with className = 'ProductJsma') as objects.
Example
... var prods = category.products; ...
category.remove(key)
Removes the key from the flexAttributes of the category
Example
... category.remove(key) ...
Parameter key:string (required)
The key to be removed from the flexAttributes of the category.
category.removeChild(child)
Removes a child from a category.
Example
... category.removeChild(child) ...
Parameter child (required)
category.set(key, value, [options])
Sets the value of a specified key for a given category.
Example
... category.set("description", "Anhänger mit Gravur"); ...
Parameter key:string (required)
The key to be set in the flexAttributes of the category.
Parameter value:string (required)
The value to be set for the key
Parameter options:object (optional)
May have a key 'broadcastEvent':bool which determines whether the log message shows up in the websocket log view.
category.setContentType(type)
Sets a category's content type.
Slice aware
This entity is slice aware, which means that to use this facility:
- there must be a logged in user in the context of the execution
- this user must be currently within a slice
- this user must have the necessary privileges to edit the entity (IFE → System → Benutzer → Roles → Content-Management)
Example
... category.setContentType("CONTENT"); ...
Parameter type (required)
category.setDescription(value)
Sets a category's description.
Example
... category.setDescription("Anhänger"); ...
Parameter value (required)
category.setImage(file)
Sets a category's image.
Slice aware
This entity is slice aware, which means that to use this facility:
- there must be a logged in user in the context of the execution
- this user must be currently within a slice
- this user must have the necessary privileges to edit the entity (IFE → System → Benutzer → Roles → Content-Management)
Example
... category.setImage(file); ...
Parameter file (required)
category.setName(value)
Sets a category's name.
Example
... category.setName("Fotobuch"); ...
Parameter value (required)
category.setNodeType(type)
Sets a category's node type.
Slice aware
This entity is slice aware, which means that to use this facility:
- there must be a logged in user in the context of the execution
- this user must be currently within a slice
- this user must have the necessary privileges to edit the entity (IFE → System → Benutzer → Roles → Content-Management)
Example
... category.setNodeType("LEAF"); ...
Parameter type (required)
category.setShortDescription(value)
Sets a category's short description.
Example
... category.setShortDescription("Anhänger") ...
Parameter value (required)
category.shortDescription
Retrieves a category's short description string.
Example
... var sDesc = category.shortDescription; ...
category.shortdescriptionUnprocessed
Retrieves a category's unprocessed short description string.
Example
... var shortDesUnproc = category.shortdescriptionUnprocessed; ...
category.sliceId
Returns the slice-ID of a category
Slice aware
This entity is slice aware, which means that to use this facility:
- there must be a logged in user in the context of the execution
- this user must be currently within a slice
- this user must have the necessary privileges to edit the entity (IFE → System → Benutzer → Roles → Content-Management)
Example
... var sliceID = category.sliceId; ...
category.title
Retrieves a category's title.
Example
... if (category.title == "Fotobuch") { ... } ...
category.update(options)
Updates a category with the given options.
Example
... category.update(options); ...
Parameter options (required)
category.uuid
Retrieves a category's UUID.
Example
... var catUUID = category.uuid; ...