Sequence.prototype.setDifference = function(other) { var result = new Sequence(); var found; for(var i=0; i<this.sequence.length; i++) { found = false; for(var j=0; j<other.sequence.length; j++) { if (helper.eq(this.sequence[i], other.sequence[j])) { found = true; break; } } if (found === false) { for(var j=0; j<result.sequence.length; j++) { if (helper.eq(this.sequence[i], result.sequence[j])) { found = true; break; } } } if (found === false) { result.push(this.sequence[i]); } } return result; }
Sequence.prototype.setInsert = function(value) { var result = new Sequence(); var found; // Remove duplicates for(var i=0; i<this.sequence.length; i++) { found = false; for(var j=0; j<result.sequence.length; j++) { if (helper.eq(this.sequence[i], result.sequence[j])) { found = true; break; } } if (found === false) { result.push(this.sequence[i]); } } // Try to add value found = false; for(var j=0; j<result.sequence.length; j++) { if (helper.eq(value, result.sequence[j])) { found = true; break; } } if (found === false) { result.push(value); } return result; }
Sequence.prototype.indexesOf = function(predicate, query) { var result = new Sequence(); var value, varId, predicateResult; if ((Array.isArray(predicate) === false) || (predicate[0] !== 69)) { //TODO COntext value = query.evaluate(predicate, query) } for(var i=0; i<this.sequence.length; i++) { if (value !== undefined) { if (helper.eq(this.sequence[i], value)) { result.push(i); } } else { // We have a function here varId = predicate[1][0][1][0]; query.context[varId] = this.sequence[i]; predicateResult = query.evaluate(predicate, query); if ((predicateResult !== null) && (predicateResult !== false)) { result.push(i); } delete query.context[varId]; } } return result; }
Sequence.prototype.distinct = function(options, query) { // options is undefined for Sequence var copy = this.toSequence(); copy.sequence.sort(function(left, right) { if (util.lt(left, right)) { return -1; } else if (util.eq(left, right)) { return 0; } else { return 1 } }); var result = new Sequence(); for(var i=0; i<copy.sequence.length; i++) { if ((result.sequence.length === 0) || !util.eq(result.sequence[result.sequence.length-1], copy.sequence[i])) { result.push(copy.sequence[i]); } } return result; }
copy.sequence.sort(function(left, right) { if (util.lt(left, right)) { return -1; } else if (util.eq(left, right)) { return 0; } else { return 1 } });
Group.prototype.push = function(group, value) { var found = false; for(var i=0; i<this.groups.length; i++) { if (helper.eq(this.groups[i].group, group)) { this.groups[i].reduction.push(value); found = true; break; } } if (found === false) { this.groups.push({ group: group, reduction: new Sequence([value]) }); } return this; }
Sequence.prototype.setIntersection = function(other, query) { var result = new Sequence(); var found; for(var i=0; i<this.sequence.length; i++) { found = false; for(var j=0; j<other.sequence.length; j++) { if (util.eq(this.sequence[i], other.sequence[j])) { found = true; break; } } if (found === true) { result.push(this.sequence[i]); } } return result; }
Sequence.prototype.difference = function(other, query) { var result = new Sequence(); var found; // Remove duplicates for(var i=0; i<this.sequence.length; i++) { found = false; for(var j=0; j<other.sequence.length; j++) { if (util.eq(this.sequence[i], other.sequence[j])) { found = true; break; } } if (found === false) { result.push(this.sequence[i]); } } return result; }
result.sequence.sort(function(left, right) { var index = 0; var field, leftValue, rightValue; if (typeof options.index === "string") { //TODO Send the appropriate message throw new Error("Cannot use an index on a sequence") } while(index <= fields.length) { field = fields[index]; if (Array.isArray(field) && (field[0] === 69)) { // newValue is a FUNC term var varId = field[1][0][1]; // 0 to select the array, 1 to select the first element query.context[varId] = left; leftValue = query.evaluate(field); delete query.context[varId]; query.context[varId] = right; rightValue = query.evaluate(field); delete query.context[varId]; } else { field = query.evaluate(field); //TODO Are we really doing that? Seriously? leftValue = (typeof left.getField === "function") ? left.getfield(field) : left[field]; rightValue = (typeof right.getField === "function") ? right.getfield(field) : right[field]; } if (helper.gt(leftValue, rightValue)) { return 1 } else if (helper.eq(leftValue, rightValue)) { index++; } else { return -1 } } return 0; });
Sequence.prototype.count = function(predicate, query, internalOptions) { if (predicate !== undefined) { var result = 0; if (util.isFunction(predicate)) { for(var i=0; i<this.sequence.length; i++) { var varId = util.getVarId(predicate); query.context[varId] = this.sequence[i]; var predicateResult = query.evaluate(predicate, internalOptions); util.assertType(predicateResult, 'DATUM', query); if (util.isTrue(predicateResult)) { result++; } delete query.context[varId]; } } else { // predicate is a value predicateResult = query.evaluate(predicate); if (typeof predicateResult === 'function') { for(var i=0; i<this.length; i++) { if (util.isTrue(predicateResult(this.get(i)))) { result++; } } } else { for(var i=0; i<this.length; i++) { if (util.eq(util.toDatum(this.get(i)), util.toDatum(predicateResult))) { result++; } } } } return result; } else { return this.sequence.length; } }
Sequence.prototype.distinct = function() { var copy = this.toSequence(); copy.sequence.sort(function(left, right) { if (helper.lt(left, right)) { return -1; } else if (helper.eq(left, right)) { return 0; } else { return 1 } }); var result = new Sequence(); for(var i=0; i<copy.sequence.length; i++) { if ((result.sequence.length === 0) || !helper.eq(result.sequence[result.sequence.length-1], copy.sequence[i])) { result.push(copy.sequence[i]); } } return result; }
Sequence.prototype.contains = function(predicate, query) { if ((Array.isArray(predicate)) && (predicate[0] === 69)) { var varId = predicate[1][0][1][0]; for(var i=0; i<this.sequence.length; i++) { query.context[varId] = this.sequence[i]; var filterResult = query.evaluate(predicate, query); delete query.context[varId]; if ((filterResult !== false) && (filterResult !== null)) { return true; } } } else { var predicate = query.evaluate(predicate, query); for(var i=0; i<this.sequence.length; i++) { if (helper.eq(helper.toDatum(this.sequence[i]), predicate)) { return true; } } } return false; }
Sequence.prototype.offsetsOf = function(predicate, query) { var result = new Sequence(); var predicateResult; if (!util.isFunction(predicate)) { predicateResult = query.evaluate(predicate, query); query.frames.push(1); util.assertType(predicateResult, 'DATUM', query); query.frames.pop(); } for(var i=0; i<this.sequence.length; i++) { if (util.isFunction(predicate)) { var varId = util.getVarId(predicate); query.context[varId] = this.get(i); query.frames.push(1); query.frames.push(1); predicateResult = query.evaluate(predicate, query); util.assertType(predicateResult, 'DATUM', query); query.frames.pop(); query.frames.pop(); // We don't have to check that predicateResult is a DATUM // as this check will be perfomed with FUNC if (util.isTrue(predicateResult)) { result.push(this.get(i)); } delete query.context[varId]; } else { if (util.eq(this.sequence[i], predicateResult)) { result.push(i); } } } return result; }
Table.prototype.distinct = function(options, query, internalOptions) { var copy; if (util.isPlainObject(options) && (options.index !== undefined)) { query.frames.push('index'); util.assertType(options.index, 'STRING', query); query.frames.pop(); copy = new Sequence(); if (this.indexes[options.index] == null) { throw new Error.ReqlRuntimeError("Index `"+options.index+"` was not found on table `"+this.db+"."+this.name+"`", query.frames) } fn = this.indexes[options.index].fn; var varId = util.getVarId(fn); var keys = Object.keys(this.documents); for(var i=0; i<keys.length; i++) { // Iterate on all the documents of the tablepay attention to what browserify does here query.context[varId] = this.documents[keys[i]]; try{ next = query.evaluate(fn, query, internalOptions); if (this.indexes[options.index].multi === true) { for(var j=0; j<next.sequence.length; j++) { copy.push(next.sequence[j]); } } else { copy.push(next); } } catch(err) { if (err.message.match(/^No attribute `/) === null) { throw err; } // else we just skip the non existence error } } delete query.context[varId]; } else { copy = this.toSequence(); } copy.sequence.sort(function(left, right) { if (util.lt(left, right)) { return -1; } else if (util.eq(left, right)) { return 0; } else { return 1 } }); var result = new Sequence(); for(var i=0; i<copy.sequence.length; i++) { if ((result.sequence.length === 0) || !util.eq(result.sequence[result.sequence.length-1], copy.sequence[i])) { result.push(copy.sequence[i]); } } return result; }
Table.prototype.getAll = function(args, options, query, internalOptions) { // This work only on a TABLE, not on a TABLE_SLICE //TODO Implement frames var selection = new Selection([], this, {}); // If no secondary index is provided, we are dealing with the primary key if (!util.isPlainObject(options)) { options = {}; } if (typeof options.index !== 'string') { options.index = this.options.primaryKey; }; var keys = Object.keys(this.documents); //TODO Throw if the index does not exist var varId = util.getVarId(this.indexes[options.index].fn) for(var i=0; i<keys.length; i++) { // Iterate on all the documents of the tablepay attention to what browserify does here query.context[varId] = this.documents[keys[i]].doc; if (this.indexes[options.index].multi === true) { var valuesIndex = undefined; try { valuesIndex = query.evaluate(this.indexes[options.index].fn, internalOptions); } catch(err) { if (err.message.match(/^No attribute `/) === null) { throw err; } } if (valuesIndex !== undefined) { for(var j=0; j<args.length; j++) { for(var k=0; k<valuesIndex.length; k++) { var valueIndex = valuesIndex.get(k); if (util.eq(util.toDatum(valueIndex), util.toDatum(args[j]))) { selection.push(this.documents[keys[i]]); break; } } } } } else { var valueIndex = undefined; try { valueIndex = query.evaluate(this.indexes[options.index].fn, query, internalOptions); } catch(err) { if (err.message.match(/^No attribute `/) === null) { throw err; } } if (valueIndex !== undefined) { for(var j=0; j<args.length; j++) { if (util.eq(util.toDatum(valueIndex), util.toDatum(args[j]))) { selection.push(this.documents[keys[i]]); break; } } } } delete query.context[varId]; } return selection; }
define("Bisna/EventTarget",[],function(){var e=function(){this.listenerList={}};return e.prototype={dispose:function(){var e;for(e in this.listenerList)this.listenerList.hasOwnProperty(e)&&(this.listenerList[e]=null,delete this.listenerList[e]);this.listenerList={}},addEventListener:function(e,t,n){var r;return n=n||this,r=this.listenerList[e],r===undefined&&(this.listenerList[e]=[]),this.listenerList[e].push({handler:t,scope:n}),this},removeEventListener:function(e,t){var n,r=0,i;n=this.listenerList[e];if(n===undefined)return this;for(i=n.length;r<i;r+=1)if(n[r].handler===t){n.splice(r,1);break}return n.length===0&&(this.listenerList[e]=null,delete this.listenerList[e]),this},hasEventListener:function(e){var t=this.listenerList[e];return t!==undefined&&t.length>0},dispatchEvent:function(e,t,n){var r,i=0,s=typeof e=="object"?e:this.createEvent(e,t,n),o;r=this.listenerList[s.type];if(r===undefined)return!0;for(o=r.length;i<o;i+=1){if(s.stopPropagation)break;s.currentTarget=r[i].scope,r[i].handler.call(s.currentTarget,s)}return!s.preventDefault},createEvent:function(e,t,n){return{type:e,target:n||this,currentTarget:this,userData:t||null,stopPropagation:!1,preventDefault:!1}}},e}),define("Bisna/HttpRequest",["jquery","Bisna/EventTarget"],function(e,t){var n=function(e,n){t.call(this),this.method=e,this.url=n,this.contentType=!1,this.dataType=!1,this.processData=!0,this.jqXHR=null,this.async=!0,this.headerList={}};return e.extend(n.prototype,t.prototype,{getMethod:function(){return this.method},getUrl:function(){return this.url},setRequestType:function(e){if(e===!1){this.contentType=!1;return}e=String(e).toLowerCase();switch(e){case"xml":this.contentType="application/xml; charset=utf-8";break;case"html":this.contentType="text/html; charset=utf-8";break;case"json":this.contentType="application/json; charset=utf-8";break;case"form":this.contentType="application/x-www-form-urlencoded; charset=UTF-8";break;case"formdata":this.contentType=!1,this.processData=!1;break;default:throw new Error("Unsupported request type: "+e)}},addHeader:function(e,t){this.headerList[e]=t},getHeaderList:function(){return this.headerList},setResponseType:function(e){e=String(e).toLowerCase();switch(e){case"xml":case"html":case"json":this.dataType=e;break;default:throw new Error("Unsupported response type: "+e)}},setAsync:function(e){this.async=e},getResponseType:function(){return this.dataType},setData:function(e){this.data=e},getData:function(){return this.data},send:function(){var t=this.compile();return this.dispatchEvent("load.start",null)?(this.abort(),this.jqXHR=e.ajax(t),this.jqXHR):null},compile:function(){var t={},n=this;return t.type=this.method,t.url=this.url,t.contentType=this.contentType,t.dataType=this.dataType,t.data=this.data,t.processData=this.processData,t.headers=this.headerList,t.async=this.async,t.success=function(e,r,i){var s=i.status<300?"success":"redirect",o;o={response:e,textStatus:r,jqXHR:i,originalData:t.data},n.dispatchEvent(s,o)},t.error=function(r,i,s){var o=r.responseText,u=r.status,a=[403,404],f;a.indexOf(u)===-1&&r.getResponseHeader("Content-Type")==="application/json"&&(o=e.parseJSON(r.responseText)),f={response:o,textStatus:i,jqXHR:r,originalData:t.data,errorThrown:s},n.dispatchEvent("error",f)},t.complete=function(e,t){var r={textStatus:t,jqXHR:e};n.dispatchEvent("complete",r),n.dispatchEvent("load.end",null)},t.xhr=function(){var t=e.ajaxSettings.xhr();return t.upload===undefined?t:(t.upload.addEventListener("progress",e.proxy(function(e){n.dispatchEvent("progress",e)},n),!1),t.upload.addEventListener("abort",e.proxy(function(e){n.dispatchEvent("abort",e)},n),!1),t)},t},abort:function(){if(!this.jqXHR)return;if(!this.dispatchEvent("load.abort",null))return;this.jqXHR.abort()}}),n}),function(e){function n(t,n){var r=e.extend({},e._data(t[0],"events")),i={};if(n&&n!==!0){i=n instanceof Array?n:n.replace(/\s+/gi," ").split(" ");for(var s in r){if(!r.hasOwnProperty(s))continue;i.indexOf(s)===-1&&(r[s]=null,delete r[s])}}return r}function r(t){for(var n in t){if(!t.hasOwnProperty(n))continue;for(var r=0,i=t[n].length;r<i;r++)e.event.add(this[0],n,t[n][r])}}var t="cloneEvent";e.fn[t]=function(t,i){if(this.length){var s=typeof t=="string"?e(t):t,o=n(s,i);this.each(e.proxy(r,this,o))}return this}}(jQuery),define("jquery.cloneEvent",function(){}),define("Portlet/Portlet",["jquery","Bisna/EventTarget","Bisna/HttpRequest","jquery.cloneEvent"],function(e,t,n){var r=function(e){t.call(this);var n=typeof e=="string"?this.$context.find(e):e;this.initialize(n)};return e.extend(r.prototype,t.prototype,{httpRequest:null,config:{},getElement:function(){return this.$element},setConfig:function(t,n){if(e.isPlainObject(t)){this.config=n?t:e.extend(this.config,t);return}return this.config[t]=n,this},getConfig:function(e,t){return e===undefined?this.config:this.hasConfig(e)?this.config[e]:t},hasConfig:function(e){return typeof this.config[e]!="undefined"},initialize:function(t){this.$element=t,this.config=e.extend({},this.$element.data())},replaceWith:function(e,t){var n=this.getElement(),r=e.config?e.getElement().clone():e;t!==!1&&r.cloneEvent(n,t||!0),n.replaceWith(r),this.initialize(r),this.dispatchEvent("replace")},load:function(e){var t={requestType:"html",method:this.getConfig("method","GET"),uri:this.getConfig("uri"),data:!1,animation:e,prefix:"load"};this.asyncCall(t)},asyncCall:function(t){if(!this.dispatchEvent(t.prefix))return;this.httpRequest&&this.httpRequest.abort(),this.httpRequest=new n(t.method,t.uri),this.httpRequest.setRequestType(t.requestType),this.httpRequest.setResponseType("html"),this.httpRequest.setData(t.data),this.httpRequest.addEventListener("load.start",function(e){t.animation&&t.animation.dispatchEvent("start"),e.type=t.prefix+".start",this.dispatchEvent(e)},this),this.httpRequest.addEventListener("load.end",function(e){t.animation&&t.animation.dispatchEvent("end"),e.type=t.prefix+".end",this.dispatchEvent(e)},this),this.httpRequest.addEventListener("success",function(n){var r=e(n.userData.response),i=this.$element?"replaceWith":"initialize";n.type=t.prefix+".success",this[i](r),this.dispatchEvent(n)},this),this.httpRequest.addEventListener("error",function(e){e.type=t.prefix+".error",this.dispatchEvent(e)},this),this.httpRequest.addEventListener("complete",function(e){this.httpRequest=null,e.type=t.prefix+".complete",this.dispatchEvent(e)},this),this.httpRequest.send()}}),r}),define("Portlet/Form",["jquery","Portlet/Portlet"],function(e,t){var n=function(e){var n={};t.call(this,e),this.addEventListener("load.start",this.disableButton),this.addEventListener("update.start",function(){n={},this.getElement().find("input:password").each(function(){n[this.getAttribute("name")]=this.value})},this),this.addEventListener("update.success",function(){var e=this.getElement(),t=null;for(var r in n)n.hasOwnProperty(r)&&(t=e.find('[name="'+r+'"]'),t.length&&t.val(n[r]),n[r]=null,delete n[r])},this),this.delegateSubmitEvent()};return e.extend(n.prototype,t.prototype,{getButtonList:function(){var e=[":submit",":reset","button"].join(",");return this.getElement().find(e)},disableButton:function(){this.getButtonList().attr("disabled","disabled")},enableButton:function(){var t=null;this.getButtonList().each(function(){t=e(this),t.attr("disabled")&&t.attr("disabled",null).removeAttr("disabled")})},getFormElement:function(){var e=this.getElement();return e.prop("tagName").toLowerCase()!=="form"?e.find("form"):e},update:function(t){var n=this.getFormElement(),r=n.find("input:password"),i=r.length,s=n.serializeArray(),o=n.attr("action")||"",u={uri:o+(/\?+/i.test(o)?"&":"?")+"updateField=1",animation:t,prefix:"update"};i&&(s=s.filter(function(e){for(var t=0;t<i;t++)return r[t].getAttribute("name")!==e.name}),u.data=e.param(s)),this.asyncCall(u)},submit:function(e){var t={animation:e,prefix:"submit"};this.asyncCall(t)},asyncCall:function(n){var r=this.getFormElement();n=e.extend({requestType:"form",method:r.attr("method")||"POST",uri:r.attr("action")||"#",data:r.serialize()},n),t.prototype.asyncCall.call(this,n)},delegateSubmitEvent:function(){var t=this.getElement(),n=this.getFormElement(),r=["submit"];n!==t&&r.push("form"),r.push(e.proxy(function(e){e.preventDefault(),this.submit()},this)),t.on.apply(t,r)}}),n}),define("Portlet/Factory",["jquery","Portlet/Portlet"],function(e,t){var n={fromHTML:function(t){return this.fromElement(e(t))},fromElement:function(e){var t=e.data("portlet")||"Portlet/Portlet",n=require(t);return new n(e)}};return n}),define("Portlet/Manager",["jquery","Portlet/Factory"],function(e,t){var n=function(){this.portletList={}};return e.extend(n.prototype,{get:function(e){if(!e)return this.portletList;if(!this.portletList[e])throw new Error('Portlet "'+e+'" not found!');return this.portletList[e]},add:function(e){var t=e.getConfig("name");if(this.portletList[t])throw new Error('A Portlet called "'+t+'" already exists!');this.portletList[t]=e},initialize:function(){var n=e(".portlet"),r=this;n.length&&n.map(function(e){var i=n.eq(e);r.add(t.fromElement(i))})}}),n}),require(["Portlet/Portlet","Portlet/Form","Portlet/Manager","Portlet/Factory"]),define("portlet",function(){});