UNPKG

flysh

Version:

DOM Document Object Artifact Collector

290 lines 10.4 kB
import { FlyshException } from '../exception/FlyshException'; import { NavPane } from '../model/NavPane'; import { SPC } from '../model/SPC'; /** * 'InputMessage' class model */ export class InputMessage { /** * Constructor * * @param domain Defines the 'URI' domain * @param pagepath Contains the 'URI' path * @param timeout Optional parameter that defines the timeout value * */ constructor(domain, pagepath, timeout) { /** * Private class constants */ this.DEFAULT_INSTANCE_TIMEOUT_VALUE = 1500; this.DOM_VALIDATION_NAVPANE_MIN_OCCURS_VALUE = 0; this.ID_GENERATED_FLOOR_FUNC_COMPLEXITY_VALUE = 100000000000; this.REGEX_FS_URI_VALIDATION_VALUE = /^(www|http:|https:)+[^\s]+[\w]$/; this._doms = new Array(); this._fs = true; this._navpane = false; this._domain = this._domainValidator(domain); this._id = Math.floor(Math.random() * Math.floor(this.ID_GENERATED_FLOOR_FUNC_COMPLEXITY_VALUE)); this._pagepath = pagepath; this._timeout = timeout || this.DEFAULT_INSTANCE_TIMEOUT_VALUE; } /** * Validates if the domain is only locally based (filesytem) or from the network. If the domain is under an URL format * then the private filesystem property is set to 'false' * * @param domain Input 'string' parameter that contains the domain value * @returns A 'string' formatted value that contains the passed parameter */ _domainValidator(domain) { let _retVal = domain; // Evaluates if the domain/path is empty if ((domain.length === 0)) throw new FlyshException(6500005100, new TypeError(), InputMessage.EXCEPTION_ID_6500005100_MESSAGE_VALUE); // Evaluates if the domain is a well formatted URL let regEx = new RegExp(this.REGEX_FS_URI_VALIDATION_VALUE); if (domain.match(regEx) !== null) this._fs = false; return _retVal; } /** * Validates the content of the '_doms' class property * * - 'SPC' class instance, verifies that is no other instance(s) having the same 'filter selector' * - 'NavPane' ('Paginator') class instance, verifies that there is not more than one instance within the stack * * @param Class Parameter that contains the class name to validate */ domsValidate(Class) { switch (true) { case Class instanceof NavPane: { if (this.findDOMElement(NavPane).length > this.DOM_VALIDATION_NAVPANE_MIN_OCCURS_VALUE) throw new FlyshException(6500001200, new Error, InputMessage.EXCEPTION_ID_6500001200_MESSAGE_VALUE); break; } case Class instanceof SPC: { for (let elem of this.findDOMElement(SPC)) if (elem.getFilterSelector == Class.getFilterSelector) throw new FlyshException(6500001100, new Error, InputMessage.EXCEPTION_ID_6500001100_MESSAGE_VALUE); break; } } } /** * Getter 'ID' number property * * @return Returns the '_id' class property */ get ID() { return this._id; } /** * Getter 'doms' array property * * @return Returns the '_doms' class property */ get doms() { return this._doms; } /** * Getter 'hasNavpane' ('Paginator') boolean property * * @return Returns the '_navpane' class property */ get hasNavpane() { return this._navpane; } /** * Getter 'domain' string property * * @return Returns the '_domain' class property */ get domain() { return this._domain; } /** * Getter 'pagePath' string property * * @return Returns the '_pagepath' class property */ get pagepath() { return this._pagepath; } /** * Getter 'filesystem' boolean property * * @return Returns the '_filesystem' class property */ get filesystem() { return this._fs; } /** * Getter 'timeout' number property * * @return Returns the current class timeout value */ get timeout() { return this._timeout; } /** * Returns 'URI/URL' string properties ('_domain' + '_pagePath') * * @return Returns the complete 'URI' ('_domain' + '_pagePath') */ get URI() { return this._domain + this._pagepath; } /** * Returns the 'DOM' element(s) list under JSON format * * @return Returns all the 'DOM' element(s) contained within the '_dom' class property */ getDomsEntries() { let retVal = ""; this._doms.forEach((e) => { if ((e instanceof NavPane)) retVal += ((e).getEntry + '\n'); if ((e instanceof SPC)) retVal += ((e).getEntry + '\n'); }); return retVal; } /** * Returns the 'DOM' element(s) list under JSON format * * @return Returns all the 'DOM' element(s) contained within the '_dom' class property */ getDomsEntriesToJSON() { let retVal; retVal = new Array(); this._doms.forEach((e) => { //if ((e instanceof NavPane)) retVal += ((<NavPane>(e)).getEntry + '\n'); //if ((e instanceof SPC)) retVal.push(JSON.stringify((<SPC>(e)).toJSON)); if ((e instanceof SPC)) retVal.push(JSON.stringify((e))); }); return retVal; } /** * 'Stringify' the object properties * * @return Returns a string that shows up all all the class properties */ get toString() { return 'ID : ' + this._id + '\n' + 'URI : ' + this.URI + '\n' + 'FS : ' + this._fs + '\n' + 'NavPane : ' + this._navpane + '\n' + 'TimeOut : ' + this._timeout + '\n' + 'Doms : ' + '\n' + this.getDomsEntries(); } /** * 'Stringify' the `InputMessage` class instance * * TODO : invoke the inner mapper */ static toJSON(instance) { return JSON.stringify(instance); } /** * Returns a JavaScript Object Notation (JSON) string into an object * * TODO : invoke the inner mapper in order to send back a plain 'InputMessage' class instance * * @param inJson * @returns */ static fromJSON(inJson) { return JSON.parse(inJson); } /** * Set the '_navpane' property value * * @param flag Contains the boolean value that sets the '_navpane' class property */ set navPane(flag) { this._navpane = flag; } /** * Returns the newly added 'NavPane' element [deprecated] * * @deprecated this method is now deprecated, please use 'addPaginator()' method instead * * @param filterSelector Defines the 'NavPane' filter selector * @param attrib Contains the attrib(ute) of the element/tag (i.e : 'href') * @returns Returns the 'NavPane' class instance itself */ addNavPane(filterSelector, attrib) { let newNavPane = new NavPane(filterSelector, attrib); newNavPane.validate(); this.domsValidate(newNavPane); this.navPane = true; return this._doms.push(new NavPane(filterSelector, attrib)); } /** * Adds a Paginator and returns the newly added 'NavPane' element * * Note : For the sake of good reading and interpretation, this function has been publicly renamed ('addNavPane()') * * @param filterSelector Defines the 'Paginator' filter selector * @param attrib Contains the attrib(ute) of the element/tag (i.e : 'href') * @returns Returns the 'Paginator' ('NavPane') class instance itself */ addPaginator(filterSelector, attrib) { let newPaginator = new NavPane(filterSelector, attrib); newPaginator.validate(); this.domsValidate(newPaginator); this.navPane = true; return this._doms.push(new NavPane(filterSelector, attrib)); } /** * Returns the newly added 'SPC' type element [deprecated] * * TODO : (new SPC(filterSelector, new Array<Sibling>())).validate() : returns SPC (this) * * @deprecated this method is now deprecated, please use 'addFilterSelector()' method instead * * @param filterselector Contains the current class filter selector * @returns Returns the 'SPC' class instance itself */ addSPC(filterSelector) { let newSPC = new SPC(filterSelector, new Array()); newSPC.validate(); this.domsValidate(newSPC); return this._doms[this._doms.push(newSPC) - 1]; } /** * Returns the newly added Filter Selector ('SPC' type element) * * Note : For the sake of good reading and interpretation, this function has been publicly renamed ('addSPC()') * * TODO : Implement validate() as (new SPC(filterSelector, new Array<Sibling>())).validate() : returns SPC (this) * * @param filterselector Contains the current class filter selector * @returns Returns the 'Filter Selector' ('SPC') class instance itself */ addFilterSelector(filterSelector) { let newFilterSelector = new SPC(filterSelector, new Array()); newFilterSelector.validate(); this.domsValidate(newFilterSelector); return this._doms[this._doms.push(newFilterSelector) - 1]; } /** * Returns all the inherited 'DOMElement' instance(s) from the '_doms' stack property that match the 'Class' parameter * * NOTE : The returned value in case of 'navpane'/'paginator' should always be 1 element * * @param Class Contains the class name to retrieve * @return Returns an array of 'DomElement' that contains the found occurrence(s) */ findDOMElement(Class) { return this._doms.filter(e => e instanceof Class); } } /** * Public class constants */ InputMessage.EXCEPTION_ID_6500001100_MESSAGE_VALUE = "Another filter selector object has the same signature"; InputMessage.EXCEPTION_ID_6500001200_MESSAGE_VALUE = "A 'Paginator' has already been set"; InputMessage.EXCEPTION_ID_6500005100_MESSAGE_VALUE = "Invalid domain value"; //# sourceMappingURL=InputMessage.js.map