Author: smaisonneuve Date: 2015-06-09 15:51:00 +0000 (Tue, 09 Jun 2015) New Revision: 1672 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1672 Log: [Refactor] Turning SearchResults into es6 class Modified: oipf/lib/js/impl/model/SearchResults.js Modified: oipf/lib/js/impl/model/SearchResults.js =================================================================== --- oipf/lib/js/impl/model/SearchResults.js 2015-06-09 15:48:11 UTC (rev 1671) +++ oipf/lib/js/impl/model/SearchResults.js 2015-06-09 15:51:00 UTC (rev 1672) @@ -1,5 +1,5 @@ /* -* Description: +* Description: * The SearchResults class represents the results of a metadata search. Since the result set may contain a large number * of items, applications request a ‘window’ on to the result set, similar to the functionality provided by the OFFSET and * LIMIT clauses in SQL. @@ -14,113 +14,105 @@ * In addition to the properties and methods defined below a SearchResults object SHALL support the array notation to * access the results in this collection. */ -var SearchResults = Array.extend({ +class SearchResults extends Array { - /* - * Description: - * The number of items in the current window within the overall result set. The value of this property SHALL be - * zero until getResults() has been called and a MetadataSearch event notifying the application that results - * are available has been dispatched. If the current window onto the result set is in fact the whole result set then - * length will be the same as totalSize. Otherwise length will be less than totalSize. - * - * Visibility: readonly - */ - length: null, - - /* - * Description : - * The current offset into the total result set. - * - * Visibility Type: readonly Integer - */ - offset: null, - - /* - * Description: - * The total number of items in the result set. - * The value of this property SHALL be zero until getResults() has been called and a MetadataSearch - * event notifying the application that results are available has been dispatched. - * - * Visibility: readonly - */ - totalSize: null, - - _cachedResults: null, - - _search: null, - - init: function(search) { + constructor(search) { + super(); + + /* + * Description: + * The number of items in the current window within the overall result set. The value of this property SHALL be + * zero until getResults() has been called and a MetadataSearch event notifying the application that results + * are available has been dispatched. If the current window onto the result set is in fact the whole result set then + * length will be the same as totalSize. Otherwise length will be less than totalSize. + * + * Visibility: readonly + */ + this.length = 0; + + /* + * Description : + * The current offset into the total result set. + * + * Visibility Type: readonly Integer + */ + this.offset = null; + + /* + * Description: + * The total number of items in the result set. + * The value of this property SHALL be zero until getResults() has been called and a MetadataSearch + * event notifying the application that results are available has been dispatched. + * + * Visibility: readonly + */ + this.totalSize = null; + this._cachedResults = []; + this._search = search; this._timerManager = new TimerManager(); this._timeout = this._timerManager.createTimer.bind(this._timerManager, 0); - this._search = search; - this.length = 0; this.totalSize = 0; - - this._fireEventUncompleted = this._search._searchManager - ._fireEventUncompleted.bind(this._search._searchManager); - this._fireEventFinished = this._search._searchManager - ._fireEventFinished.bind(this._search._searchManager); + this._methodToCall = { + 0: "equals", + 1: "notEquals", + 2: "superior", + 3: "superiorOrEquals", + 4: "inferior", + 5: "inferiorOrEquals", + 6: "contains" + }; - }, - - _methodToCall: { - 0: "equals", - 1: "notEquals", - 2: "superior", - 3: "superiorOrEquals", - 4: "inferior", - 5: "inferiorOrEquals", - 6: "contains" - }, - + this._fireEventUncompleted = this._search._searchManager._fireEventUncompleted.bind(this._search._searchManager); + this._fireEventFinished = this._search._searchManager._fireEventFinished.bind(this._search._searchManager); + } + /* - * Description: - * Perform the search and retrieve the specified subset of the items that match the query. - * Results SHALL be returned asynchronously. A MetadataSearch event with state=0 - * SHALL be dispatched when results are available. - * This method SHALL always return false. - * - * Arguments: - * - offset: The number of items at the start of the result set to be skipped before data is retrieved. - * - * - count: The number of results to retrieve. - * FIXME: Yannis - 08/04/2015 - Verification about results ordering constraint - */ - getResults: function(offset, count) { + * Description: + * Perform the search and retrieve the specified subset of the items that match the query. + * Results SHALL be returned asynchronously. A MetadataSearch event with state=0 + * SHALL be dispatched when results are available. + * This method SHALL always return false. + * + * Arguments: + * - offset: The number of items at the start of the result set to be skipped before data is retrieved. + * + * - count: The number of results to retrieve. + * FIXME: Yannis - 08/04/2015 - Verification about results ordering constraint + */ + getResults(offset, count) { var parameters = arguments; - if (parameters.length != 2) { + if (parameters.length !== 2) { throw new TypeError("Insufficient number of arguments"); } - + if (!Number.isInteger(offset) || !Number.isInteger(count)) { - throw new TypeError - ("This function cannot be called with these arguments."); + throw new TypeError("This function cannot be called with these arguments."); } - + this.offset = offset; this._count = count; - var self = this; + var programmes = this._search._searchManager._metadata; - + /* * Verify some conditions before beginning of - * search. + * search. */ if (this._search._currentQuery && programmes) { - + /* - * A verification must be done on the presence of cached results - * in order to not restart a complete search when + * A verification must be done on the presence of cached results + * in order to not restart a complete search when * that's not necessary. */ - if (this._cachedResults.length != 0) { + if (this._cachedResults.length !== 0) { this._timeout() .then(this._getResultFromInterval.bind(this)); - + } else { this._timeout() @@ -128,49 +120,49 @@ .then(this._timeout) .then(this._getResultFromInterval.bind(this)); } - + } else { this._fireEventUncompleted(this._search); } - + return false; - }, - - begin: function(programmes) { + } + + begin(programmes) { /* - * When the search corresponds to current programme - * the code differs from other usual query. - */ - if (this._search._currentQuery._type == "current") { + * When the search corresponds to current programme + * the code differs from other usual query. + */ + if (this._search._currentQuery._type === "current") { - this._getCurrentProgramme(programmes); + this._getCurrentProgramme(programmes); - } else { + } else { - this._getAnyProgramme(programmes); - } - }, - + this._getAnyProgramme(programmes); + } + } + /* - * Description: + * Description: * Abort any outstanding request for results and remove any query, constraints or ordering * rules set on the MetadataSearch object that is associated with this SearchResults * object. Regardless of whether or not there is an outstanding request for results, items * currently in the collection SHALL be removed (i.e. the value of the length property SHALL * be 0 and any calls to item() SHALL return undefined ). All cached search results SHALL * be discarded. - * + * */ - abort: function() { + abort() { this._timerManager.clearTimer(); this._search._removeConstraints(); this._search._removeQuery(); this._search._removeOrdering(); this._cachedResults.length = 0; this.length = 0; - }, - - _getResultFromInterval: function() { + } + + _getResultFromInterval() { this.length = 0; if (this._cachedResults) { @@ -183,124 +175,117 @@ console.log("[Info]No results found."); } } - + this._fireEventFinished(this._search); this.totalSize = this._cachedResults.length; } - }, - - _getAnyProgramme: function(programmes) { - + } + + _getAnyProgramme(programmes) { + for (var i = 0, l = programmes.length; i < l; i++) { var programme = programmes[i]; if (this._evaluateQuery(this._search._currentQuery, programme)) { var constraints = this._search. _constraints.channels; - + var next = true; - if (constraints.length > 0 && + if (constraints.length > 0 && oipf.utils.isPresent(constraints, programme.channel.name)) { this._cachedResults.push(programme); - var next = false; + next = false; } - if (next && constraints.length == 0) { + if (next && constraints.length === 0) { this._cachedResults.push(programme); - + } } } - }, - - _getCurrentProgramme: function(programmes) { + } + + _getCurrentProgramme(programmes) { var time = new Date().getTime() / 1000; var constraints = this._search. _constraints.channels; - + for (var i = 0, l = programmes.length; i < l; i++) { var programme = programmes[i]; - + if (constraints.length > 0 && oipf.utils.isPresent(constraints, programme.channel.name) && programme.duration) { var stopTime = programme.startTime + programme.duration; - var self = this; - + if (time >= programme.startTime && time <= stopTime) { - + this._cachedResults.push(programme); i = l; } } } - }, - + } + /* - * Description: + * Description: * Return the item at position index in the collection of currently available results, or * undefined if no item is present at that position. This function SHALL only return objects * that are instances of Programme , CODAsset , CODFolder , or CODService . - * + * * Arguments: * - index: The index into the result set. - * + * */ - item: function(index) { + item(index) { return this[index]; - }, + } - _evaluateQuery: function(query, programme) { + _evaluateQuery(query, programme) { var field = this._getPropertyName(this._search._currentQuery._field); var comparison = this._search._currentQuery._comparison; var value = this._search._currentQuery._value; var methodName = this._methodToCall[comparison]; - - if (query._and.length > 0 && query._or.length > 0) { - return - (oipf.utils[methodName].call(null, programme[field], value) && - this._evaluateQueryLoop(query._and, programme)) || - (oipf.utils[methodName].call(null, programme[field], value) || - this._evaluateQueryLoop(query._and, programme)); + if (query._and.length > 0 && query._or.length > 0) { - } else if (query._and.length > 0 && query._or.length == 0) { + return (oipf.utils[methodName].call(null, programme[field], value) && this._evaluateQueryLoop(query._and, programme)) + || (oipf.utils[methodName].call(null, programme[field], value) + || this._evaluateQueryLoop(query._and, programme)); - return - (oipf.utils[methodName].call(null, programme[field], value) && - this._evaluateQueryLoop(query._and, programme)); + } else if (query._and.length > 0 && query._or.length === 0) { - } else if (query._or.length > 0 && query._and.length == 0) { + return (oipf.utils[methodName].call(null, programme[field], value) && this._evaluateQueryLoop(query._and, programme)); - return - (oipf.utils[methodName].call(null, programme[field], value) && - this._evaluateQueryLoop(query._or, programme)); + } else if (query._or.length > 0 && query._and.length === 0) { - } else if (query._and.length == 0 && query._or.length == 0) { - return (oipf.utils[methodName].call(null, programme[field], value)); - } - - }, - - _evaluateQueryLoop: function(query, programme) { + return (oipf.utils[methodName].call(null, programme[field], value) && this._evaluateQueryLoop(query._or, programme)); + + } else if (query._and.length === 0 && query._or.length === 0) { + return (oipf.utils[methodName].call(null, programme[field], value)); + } + + } + + _evaluateQueryLoop(query, programme) { for (var i = 0, l = query; i < l; i++) { if (!this._evaluateQuery(query, programme)) { return false; } } - }, - - _getPropertyName: function(field) { + } + + _getPropertyName(field) { var pattern = /\w+.(\w+)/; var propertyName = pattern.exec(field); - + return propertyName[1]; } - -}); \ No newline at end of file + +}
participants (1)
-
smaisonneuve@users.nuiton.org