pax_global_header 0000666 0000000 0000000 00000000064 12565046077 0014526 g ustar 00root root 0000000 0000000 52 comment=94d9a7bf1595b639b1101d5b539a5bc5997687d4
knockout-transformations-2.1.0/ 0000775 0000000 0000000 00000000000 12565046077 0016612 5 ustar 00root root 0000000 0000000 knockout-transformations-2.1.0/.gitignore 0000775 0000000 0000000 00000000042 12565046077 0020601 0 ustar 00root root 0000000 0000000 /node_modules/
/tmp/
.DS_Store
knockout-transformations-2.1.0/.jscsrc 0000664 0000000 0000000 00000003613 12565046077 0020105 0 ustar 00root root 0000000 0000000 {
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"case",
"return",
"try",
"catch",
"function",
"typeof"
],
"requireCurlyBraces": [
"if",
"else",
"for",
"while",
"do",
"switch",
"catch",
"function"
],
"disallowSpacesInNamedFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"requireSpacesInAnonymousFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
},
"requireSpaceBeforeBlockStatements": true,
"requireParenthesesAroundIIFE": true,
"requireSpacesInConditionalExpression": true,
"disallowSpacesInNamedFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"disallowSpacesInFunctionDeclaration": {
"beforeOpeningRoundBrace": true
},
"requireBlocksOnNewline": 1,
"disallowEmptyBlocks": true,
"disallowSpacesInsideParentheses": true,
"requireCommaBeforeLineBreak": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowSpaceBeforePostfixUnaryOperators": true,
"disallowSpaceBeforeBinaryOperators": [","],
"disallowSpacesInCallExpression": true,
"requireSpaceBeforeBinaryOperators": true,
"requireSpaceAfterBinaryOperators": true,
"disallowKeywords": ["with"],
"validateIndentation": 4,
"disallowMixedSpacesAndTabs": "smart",
"disallowTrailingWhitespace": true,
"disallowTrailingComma": true,
"disallowKeywordsOnNewLine": ["else"],
"requireLineFeedAtFileEnd": true,
"requireCapitalizedConstructors": true,
"disallowNewlineBeforeBlockStatements": true,
"requireCamelCaseOrUpperCaseIdentifiers": true,
"requireDotNotation": true,
"validateParameterSeparator": ", ",
"safeContextKeyword": ["that"]
}
knockout-transformations-2.1.0/.jshintrc 0000664 0000000 0000000 00000012074 12565046077 0020443 0 ustar 00root root 0000000 0000000 {
// == Enforcing Options ===============================================
"bitwise" : false, // Prohibit bitwise operators (&, |, ^, etc.).
"curly" : false, // Require {} for every new block or scope. (Moved to codestyle check)
"eqeqeq" : true, // Require triple equals i.e. `===`.
"forin" : false, // Tolerate `for in` loops without `hasOwnPrototype`.
"immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
"latedef" : false, // Prohibit variable use before definition.
"newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`.
"noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`.
"noempty" : false, // Prohibit use of empty blocks.
"nonew" : false, // Prohibit use of constructors for side-effects.
"plusplus" : true, // Prohibit use of `++` & `--`.
"undef" : true, // Require all non-global variables be declared before they are used.
"unused" : "vars", // If variables defined but not used are checked.
"strict" : false, // Require `use strict` pragma in every file.
// == Relaxing Options ================================================
"asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons).
"boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments.
"debug" : false, // Allow debugger statements e.g. browser breakpoints.
"eqnull" : false, // Tolerate use of `== null`.
"esnext" : false, // Allow ES.next specific features such as `const` and `let`.
"evil" : false, // Tolerate use of `eval`.
"expr" : false, // Tolerate `ExpressionStatement` as Programs.
"funcscope" : false, // Tolerate declarations of variables inside of control structures while accessing them later from the outside.
"globalstrict" : false, // Allow global "use strict" (also enables 'strict').
"iterator" : false, // Allow usage of __iterator__ property.
"lastsemic" : false, // Tolerat missing semicolons when the it is omitted for the last statement in a one-line block.
"laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons.
"laxcomma" : false, // Suppress warnings about comma-first coding style.
"loopfunc" : true, // Allow functions to be defined within loops.
"multistr" : false, // Tolerate multi-line strings.
"proto" : false, // Tolerate __proto__ property. This property is deprecated.
"scripturl" : false, // Tolerate script-targeted URLs.
"shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`.
"sub" : true, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`.
"supernew" : false, // Tolerate `new function () { ... };` and `new Object;`.
"validthis" : false, // Tolerate strict violations when the code is running in strict mode and you use this in a non-constructor function.
// == Environments ====================================================
"browser" : false, // Standard browser globals e.g. `window`, `document`.
"couch" : false, // Enable globals exposed by CouchDB.
"devel" : false, // Allow development statements e.g. `console.log();`.
"dojo" : false, // Enable globals exposed by Dojo Toolkit.
"jquery" : false, // Enable globals exposed by jQuery JavaScript library.
"mootools" : false, // Enable globals exposed by MooTools JavaScript framework.
"node" : false, // Enable globals available when code is running inside of the NodeJS runtime environment.
"nonstandard" : false, // Define non-standard but widely adopted globals such as escape and unescape.
"phantom" : false, // Enable globals exposed by PhantomJS.
"prototypejs" : false, // Enable globals exposed by Prototype JavaScript framework.
"rhino" : false, // Enable globals available when your code is running inside of the Rhino runtime environment.
"worker" : false, // Enable globals exposed insite of a Worker.
"wsh" : false, // Enable globals available when your code is running as a script for the Windows Script Host.
"yui" : false, // Enable globals exposed by YUI framework.
// == Globals =========================================================
"globals": {
"one": true,
"Ext": true,
"INCLUDE": false,
"GETSTATICURL": false,
"GETTEXT": false,
"TR": false,
"TRPAT": false,
"TRHTML": false,
"LOCALEID": true,
"SUPPORTEDLOCALEIDS": true,
"DEFAULTLOCALEID": true,
"LOCALECOOKIENAME": true
}
}
knockout-transformations-2.1.0/LICENSE 0000664 0000000 0000000 00000001043 12565046077 0017615 0 ustar 00root root 0000000 0000000 Copyright 2015 One.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. knockout-transformations-2.1.0/Makefile 0000664 0000000 0000000 00000002033 12565046077 0020250 0 ustar 00root root 0000000 0000000 PATH := ${PATH}:./node_modules/.bin
build: lint test dist/knockout-transformations.min.js
.PHONY: lint
lint:
@jshint lib/*.js test/*.js
@jscs lib/*.js test/*.js
.PHONY: test
test:
@mocha
dist/knockout-transformations.js: package.json lib/*
@(echo '/*!' &&\
cat LICENSE &&\
echo '\n*/' &&\
cat lib/map.js &&\
cat lib/filter.js &&\
cat lib/sortBy.js &&\
cat lib/indexBy.js) > $@
dist/knockout-transformations.min.js: dist/knockout-transformations.js
@(echo '/*!' &&\
cat LICENSE &&\
echo '\n*/' &&\
uglifyjs dist/knockout-transformations.js) > $@
.PHONY: git-dirty-check
git-dirty-check:
ifneq ($(shell git describe --always --dirty | grep -- -dirty),)
$(error Working tree is dirty, please commit or stash your changes, then try again)
endif
.PHONY: release-%
release-%: git-dirty-check lint test dist/knockout-transformations.min.js
git add dist/knockout-transformations*.js && git commit -m "Build distribution files"
npm version $*
@echo $* release ready to be publised to NPM
@echo Remember to push tags
knockout-transformations-2.1.0/README.md 0000775 0000000 0000000 00000024134 12565046077 0020100 0 ustar 00root root 0000000 0000000 knockout-transformations
============
Live transform methods for Knockout observable arrays.
This plugin adds observable `map`, `filter`, `indexBy` and `sortBy` features to observable arrays, so you can transform collections in arbitrary ways and have the results automatically update whenever the underlying source data changes.
The project initialy started out as a fork of https://github.com/SteveSanderson/knockout-projections and therefore owes a lot to this project. This project is licensed under Apache 2.0 by Microsoft Corporation and the part of the code derived from this project is constrained by this license. The rest of the code is also licensed under Apache 2.0 by One.com.
Installation
============
Download a copy of `knockout-transformations.js` from [the `dist` directory](https://github.com/One-com/knockout-transformations/tree/master/dist) and reference it in your web application:
```html
```
Be sure to reference it *after* you reference Knockout itself.
If you are using NPM you can install knockout and knockout-transformations the following way:
npm install knockout knockout-transformations
Then just reference the distribution files from `node_modules`.
Using require.js you can either point to the index file in `lib` or
use the individual transformations from located in `lib`.
Usage
=====
**Mapping**
More info to follow. For now, here's a simple example:
```js
var sourceItems = ko.observableArray([1, 2, 3, 4, 5]);
```
There's a plain observable array. Now let's say we want to keep track of the squares of these values:
```js
var squares = sourceItems.map(function(x) { return x*x; });
```
Now `squares` is an observable array containing `[1, 4, 9, 16, 25]`. Let's modify the source data:
```js
sourceItems.push(6);
// 'squares' has automatically updated and now contains [1, 4, 9, 16, 25, 36]
```
This works with any transformation of the source data, e.g.:
```js
sourceItems.reverse();
// 'squares' now contains [36, 25, 16, 9, 4, 1]
```
The key point of this library is that these transformations are done *efficiently*. Specifically, your callback
function that performs the mapping is only called when strictly necessary (usually, that's only for newly-added
items). When you add new items to the source data, we *don't* need to re-map the existing ones. When you reorder
the source data, the output order is correspondingly changed *without* remapping anything.
This efficiency might not matter much if you're just squaring numbers, but when you are mapping complex nested
graphs of custom objects, it can be important to perform each mapping update with the minumum of work.
**Filtering**
As well as `map`, this plugin also provides `filter`:
```js
var evenSquares = squares.filter(function(x) { return x % 2 === 0; });
// evenSquares is now an observable containing [36, 16, 4]
sourceItems.push(9);
// This has no effect on evenSquares, because 9*9=81 is odd
sourceItems.push(10);
// evenSquares now contains [36, 16, 4, 100]
```
Again, your `filter` callbacks are only called when strictly necessary. Re-ordering or deleting source items don't
require any refiltering - the output is simply updated to match. Only newly-added source items must be subjected
to your `filter` callback.
**Sorting**
As well as `map` and `filter`, this plugin also provides `sortBy`:
```js
var sortedEvenSquares.sortBy(function (evenSquare, descending) {
return descending(evenSquare);
});
// sortedEvenSquares now contains [100, 36, 16, 4]
```
A more involved example:
```js
function Person(name, yearOfBirth) {
this.name = ko.observable(name);
this.yearOfBirth = ko.observable(yearOfBirth);
}
var persons = ko.observableArray([
new Person("Marilyn Monroe", 1926),
new Person("Abraham Lincoln", 1809),
new Person("Mother Teresa", 1910),
new Person("John F. Kennedy", 1917),
new Person("Martin Luther King", 1929),
new Person("Nelson Mandela", 1918),
new Person("Winston Churchill", 1874),
new Person("Bill Gates", 1955),
new Person("Muhammad Ali", 1942),
new Person("Mahatma Gandhi", 1869),
new Person("Queen Elizabeth II", 1926)
]);
// Persons sorted by name
var sortedByName = persons.sortBy(function (person) {
return person.name();
});
// sortedByName now contains
// [
// new Person("Abraham Lincoln", 1809),
// new Person("Bill Gates", 1955),
// new Person("John F. Kennedy", 1917),
// new Person("Mahatma Gandhi", 1869),
// new Person("Marilyn Monroe", 1926),
// new Person("Martin Luther King", 1929),
// new Person("Mother Teresa", 1910),
// new Person("Muhammad Ali", 1942)
// new Person("Nelson Mandela", 1918),
// new Person("Queen Elizabeth II", 1926),
// new Person("Winston Churchill", 1874),
// ]
// Persons sorted by year of birth descending and then by name
var sortedByYearOfBirthAndThenName = persons.sortBy(function (person, descending) {
return [descending(person.yearOfBirth()), person.name()];
});
// sortedByYearOfBirthAndThenName now contains
// [
// new Person("Abraham Lincoln", 1809),
// new Person("Mahatma Gandhi", 1869),
// new Person("Winston Churchill", 1874),
// new Person("Mother Teresa", 1910),
// new Person("John F. Kennedy", 1917),
// new Person("Nelson Mandela", 1918),
// new Person("Martin Luther King", 1929),
// new Person("Bill Gates", 1955),
// new Person("Marilyn Monroe", 1926),
// new Person("Queen Elizabeth II", 1926),
// new Person("Muhammad Ali", 1942)
// ]
```
The sorted list is only updated when items are added or removed and when properties that are sorted on changes.
**Indexing**
This transformation provides you with live updated index on a key returned
by the given function. In contrast to the `map`, `filter` and `sortBy`
this transformation returns an object and is therefore not a candidate for
chaining.
```js
var squareIndex = squares.indexBy(function (square) {
return square % 2 === 0 ? 'even' : 'odd';
});
// squareIndex now contains
// { even: [36, 16, 4], odd: [25, 9, 1] }
36, 25, 16, 9, 4, 1
```
A more involved example using the persons defined in the sorting example:
```js
// Persons indexed by year of birth
var personsIndexedByYearBirth = persons.indexBy(function (person) {
return person.yearOfBirth();
});
// personsIndexedByYearBirth now contains
// {
// 1809: [new Person("Abraham Lincoln", 1809)],
// 1869: [new Person("Mahatma Gandhi", 1869)],
// 1874: [new Person("Winston Churchill", 1874)],
// 1910: [new Person("Mother Teresa", 1910)],
// 1917: [new Person("John F. Kennedy", 1917)],
// 1918: [new Person("Nelson Mandela", 1918)],
// 1929: [new Person("Martin Luther King", 1929)],
// 1955: [new Person("Bill Gates", 1955)],
// 1926: [new Person("Marilyn Monroe", 1926),
// new Person("Queen Elizabeth II", 1926)],
// 1942: [new Person("Muhammad Ali", 1942)]
// }
// Persons indexed uniquely by name.
// Notice unique indexes requires items to map to distint keys;
// otherwise an exception is thrown.
var personsIndexedByName = persons.uniqueIndexBy(function (person) {
return person.name();
});
// personsIndexedByName now contains
// {
// "Abraham Lincoln": new Person("Abraham Lincoln", 1809),
// "Mahatma Gandhi": new Person("Mahatma Gandhi", 1869),
// "Winston Churchill": new Person("Winston Churchill", 1874),
// "Mother Teresa": new Person("Mother Teresa", 1910),
// "John F. Kennedy": new Person("John F. Kennedy", 1917),
// "Nelson Mandela": new Person("Nelson Mandela", 1918),
// "Martin Luther King": new Person("Martin Luther King", 1929),
// "Bill Gates": new Person("Bill Gates", 1955),
// "Marilyn Monroe": new Person("Marilyn Monroe", 1926),
// "Queen Elizabeth II": new Person("Queen Elizabeth II", 1926),
// "Muhammad Ali": new Person("Muhammad Ali", 1942)
// }
```
It is also possible to create an index on multiple keys to following way:
```js
var texts = ko.observableArray(['foo', 'bar', 'baz', 'qux', 'quux']);
// Index texts by
var indexedTexts = texts.indexBy(function (text) {
var firstLetter = text[0];
var lastLetter = text[text.length - 1];
return [firstLetter, lastLetter];
});
// indexedTexts now contains
// {
// f: ['foo'],
// b: ['bar', 'baz'],
// q: ['qux', 'quux'],
// o: ['foo'],
// r: ['bar'],
// z: ['baz'],
// x: ['qux', 'quux']
// }
```
**Chaining**
The above code also demonstrates that you can chain together successive `map`, `filter` and `sortBy` transformations.
When the underlying data changes, the effects will ripple out through the chain of computed arrays with the
minimum necessary invocation of your `map`, `filter` and `sortBy` callbacks.
How to build from source
========================
First, install [NPM](https://npmjs.org/) if you don't already have it. It comes with Node.js.
Second, install Grunt globally, if you don't already have it:
npm install -g grunt-cli
Third, use NPM to download all the dependencies for this module:
cd wherever_you_cloned_this_repo
npm install
Now you can build the package (linting and running tests along the way):
grunt
Or you can just run the linting tool and tests:
grunt test
Or you can make Grunt watch for changes to the sources/specs and auto-rebuild after each change:
grunt watch
The browser-ready output files will be dumped at the following locations:
* `dist/knockout-transformations.js`
* `dist/knockout-transformations.min.js`
License - Apache 2.0
====================
Copyright 2015 One.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.
knockout-transformations-2.1.0/dist/ 0000775 0000000 0000000 00000000000 12565046077 0017555 5 ustar 00root root 0000000 0000000 knockout-transformations-2.1.0/dist/knockout-transformations.js 0000664 0000000 0000000 00000120773 12565046077 0025211 0 ustar 00root root 0000000 0000000 /*!
Copyright 2015 One.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.
*/
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory(require('knockout'));
} else if (typeof define === 'function' && define.amd) {
define(['knockout'], factory);
} else {
factory(root.ko);
}
}(this, function (ko) {
ko.transformations = ko.transformations || {
fn: {}
};
function StateItem(inputItem, initialStateArrayIndex, initialOutputArrayIndex, mappingOptions, arrayOfState, outputObservableArray) {
// Capture state for later use
this.inputItem = inputItem;
this.stateArrayIndex = initialStateArrayIndex;
this.mappingOptions = mappingOptions;
this.arrayOfState = arrayOfState;
this.outputObservableArray = outputObservableArray;
this.outputArray = this.outputObservableArray.peek();
this.isIncluded = null; // Means 'not yet determined'
this.suppressNotification = false; // TODO: Instead of this technique, consider raising a sparse diff with a "mutated" entry when a single item changes, and not having any other change logic inside StateItem
// Set up observables
this.outputArrayIndex = ko.observable(initialOutputArrayIndex); // When excluded, it's the position the item would go if it became included
this.disposeFuncFromMostRecentMapping = null;
this.mappedValueComputed = ko.computed(this.mappingEvaluator, this);
this.mappedValueComputed.subscribe(this.onMappingResultChanged, this);
this.previousMappedValue = this.mappedValueComputed.peek();
}
StateItem.prototype.dispose = function () {
this.mappedValueComputed.dispose();
this.disposeResultFromMostRecentEvaluation();
};
StateItem.prototype.disposeResultFromMostRecentEvaluation = function () {
if (this.disposeFuncFromMostRecentMapping) {
this.disposeFuncFromMostRecentMapping();
this.disposeFuncFromMostRecentMapping = null;
}
if (this.mappingOptions.disposeItem) {
var mappedItem = this.mappedValueComputed();
this.mappingOptions.disposeItem(mappedItem);
}
};
StateItem.prototype.mappingEvaluator = function () {
if (this.isIncluded !== null) { // i.e., not first run
// This is a replace-in-place, so call any dispose callbacks
// we have for the earlier value
this.disposeResultFromMostRecentEvaluation();
}
var mappedValue;
if (this.mappingOptions.mapping) {
mappedValue = this.mappingOptions.mapping(this.inputItem, this.outputArrayIndex);
} else if (this.mappingOptions.mappingWithDisposeCallback) {
var mappedValueWithDisposeCallback = this.mappingOptions.mappingWithDisposeCallback(this.inputItem, this.outputArrayIndex);
if (!('mappedValue' in mappedValueWithDisposeCallback)) {
throw new Error('Return value from mappingWithDisposeCallback should have a \'mappedItem\' property.');
}
mappedValue = mappedValueWithDisposeCallback.mappedValue;
this.disposeFuncFromMostRecentMapping = mappedValueWithDisposeCallback.dispose;
} else {
throw new Error('No mapping callback given.');
}
if (this.isIncluded === null) { // first run
this.isIncluded = mappedValue !== this.mappingOptions.exclusionMarker;
}
return mappedValue;
};
StateItem.prototype.updateInclusion = function () {
var outputArrayIndex = this.outputArrayIndex.peek();
var outputArray = this.outputArray;
for (var iterationIndex = this.stateArrayIndex; iterationIndex < this.arrayOfState.length; iterationIndex += 1) {
var stateItem = this.arrayOfState[iterationIndex];
stateItem.setOutputArrayIndexSilently(outputArrayIndex);
var newValue = stateItem.mappingEvaluator();
var newInclusionState = newValue !== this.mappingOptions.exclusionMarker;
// Inclusion state changes can *only* happen as a result of changing an individual item.
// Structural changes to the array can't cause this (because they don't cause any remapping;
// they only map newly added items which have no earlier inclusion state to change).
if (newInclusionState) {
outputArray[outputArrayIndex] = newValue;
outputArrayIndex += 1;
}
stateItem.previousMappedValue = newValue;
stateItem.isIncluded = newInclusionState;
}
if (outputArrayIndex < outputArray.length) {
outputArray.length = outputArrayIndex;
}
};
StateItem.prototype.onMappingResultChanged = function (newValue) {
if (newValue !== this.previousMappedValue) {
if (!this.suppressNotification) {
this.outputObservableArray.valueWillMutate();
}
var newInclusionState = newValue !== this.mappingOptions.exclusionMarker;
if (this.isIncluded !== newInclusionState) {
this.updateInclusion();
} else {
if (newInclusionState) {
this.outputArray[this.outputArrayIndex.peek()] = newValue;
}
this.previousMappedValue = newValue;
}
if (!this.suppressNotification) {
this.outputObservableArray.valueHasMutated();
}
}
};
StateItem.prototype.setOutputArrayIndexSilently = function (newIndex) {
// We only want to raise one output array notification per input array change,
// so during processing, we suppress notifications
this.suppressNotification = true;
this.outputArrayIndex(newIndex);
this.suppressNotification = false;
};
function getDiffEntryPostOperationIndex(diffEntry, editOffset) {
// The diff algorithm's "index" value refers to the output array for additions,
// but the "input" array for deletions. Get the output array position.
if (!diffEntry) { return null; }
switch (diffEntry.status) {
case 'added':
return diffEntry.index;
case 'deleted':
return diffEntry.index + editOffset;
default:
throw new Error('Unknown diff status: ' + diffEntry.status);
}
}
function insertOutputItem(diffEntry, movedStateItems, stateArrayIndex, outputArrayIndex, mappingOptions, arrayOfState, outputObservableArray, outputArray) {
// Retain the existing mapped value if this is a move, otherwise perform mapping
var isMoved = typeof diffEntry.moved === 'number',
stateItem = isMoved ?
movedStateItems[diffEntry.moved] :
new StateItem(diffEntry.value, stateArrayIndex, outputArrayIndex, mappingOptions, arrayOfState, outputObservableArray);
arrayOfState.splice(stateArrayIndex, 0, stateItem);
if (stateItem.isIncluded) {
outputArray.splice(outputArrayIndex, 0, stateItem.mappedValueComputed.peek());
}
// Update indexes
if (isMoved) {
// We don't change the index until *after* updating this item's position in outputObservableArray,
// because changing the index may trigger re-mapping, which in turn would cause the new
// value to be written to the 'index' position in the output array
stateItem.stateArrayIndex = stateArrayIndex;
stateItem.setOutputArrayIndexSilently(outputArrayIndex);
}
return stateItem;
}
function deleteOutputItem(diffEntry, arrayOfState, stateArrayIndex, outputArrayIndex, outputArray) {
var stateItem = arrayOfState.splice(stateArrayIndex, 1)[0];
if (stateItem.isIncluded) {
outputArray.splice(outputArrayIndex, 1);
}
if (typeof diffEntry.moved !== 'number') {
// Be careful to dispose only if this item really was deleted and not moved
stateItem.dispose();
}
}
function updateRetainedOutputItem(stateItem, stateArrayIndex, outputArrayIndex) {
// Just have to update its indexes
stateItem.stateArrayIndex = stateArrayIndex;
stateItem.setOutputArrayIndexSilently(outputArrayIndex);
// Return the new value for outputArrayIndex
return outputArrayIndex + (stateItem.isIncluded ? 1 : 0);
}
function makeLookupOfMovedStateItems(diff, arrayOfState) {
// Before we mutate arrayOfComputedMappedValues at all, grab a reference to each moved item
var movedStateItems = {};
for (var diffIndex = 0; diffIndex < diff.length; diffIndex += 1) {
var diffEntry = diff[diffIndex];
if (diffEntry.status === 'added' && (typeof diffEntry.moved === 'number')) {
movedStateItems[diffEntry.moved] = arrayOfState[diffEntry.moved];
}
}
return movedStateItems;
}
function getFirstModifiedOutputIndex(firstDiffEntry, arrayOfState, outputArray) {
// Work out where the first edit will affect the output array
// Then we can update outputArrayIndex incrementally while walking the diff list
if (!outputArray.length || !arrayOfState[firstDiffEntry.index]) {
// The first edit is beyond the end of the output or state array, so we must
// just be appending items.
return outputArray.length;
} else {
// The first edit corresponds to an existing state array item, so grab
// the first output array index from it.
return arrayOfState[firstDiffEntry.index].outputArrayIndex.peek();
}
}
function respondToArrayStructuralChanges(inputObservableArray, arrayOfState, outputArray, outputObservableArray, mappingOptions) {
return inputObservableArray.subscribe(function (diff) {
if (!diff.length) {
return;
}
if (arrayOfState.length === 0) {
// Only add items
var newOutputItems = [];
ko.utils.arrayForEach(diff, function (diffEntry, i) {
var inputItem = diffEntry.value;
var stateItem = new StateItem(inputItem, i, newOutputItems.length, mappingOptions, arrayOfState, outputObservableArray);
var mappedValue = stateItem.mappedValueComputed.peek();
arrayOfState.push(stateItem);
if (stateItem.isIncluded) {
newOutputItems.push(mappedValue);
}
});
outputObservableArray.push.apply(outputObservableArray, newOutputItems);
return;
}
outputObservableArray.valueWillMutate();
var movedStateItems = makeLookupOfMovedStateItems(diff, arrayOfState),
diffIndex = 0,
diffEntry = diff[0],
editOffset = 0, // A running total of (num(items added) - num(items deleted)) not accounting for filtering
outputArrayIndex = diffEntry && getFirstModifiedOutputIndex(diffEntry, arrayOfState, outputArray);
// Now iterate over the state array, at each stage checking whether the current item
// is the next one to have been edited. We can skip all the state array items whose
// indexes are less than the first edit index (i.e., diff[0].index).
for (var stateArrayIndex = diffEntry.index; diffEntry || (stateArrayIndex < arrayOfState.length); stateArrayIndex += 1) {
// Does the current diffEntry correspond to this position in the state array?
if (getDiffEntryPostOperationIndex(diffEntry, editOffset) === stateArrayIndex) {
// Yes - insert or delete the corresponding state and output items
switch (diffEntry.status) {
case 'added':
// Add to output, and update indexes
var stateItem = insertOutputItem(diffEntry, movedStateItems, stateArrayIndex, outputArrayIndex, mappingOptions, arrayOfState, outputObservableArray, outputArray);
if (stateItem.isIncluded) {
outputArrayIndex += 1;
}
editOffset += 1;
break;
case 'deleted':
// Just erase from the output, and update indexes
deleteOutputItem(diffEntry, arrayOfState, stateArrayIndex, outputArrayIndex, outputArray);
editOffset -= 1;
stateArrayIndex -= 1; // To compensate for the "for" loop incrementing it
break;
default:
throw new Error('Unknown diff status: ' + diffEntry.status);
}
// We're done with this diff entry. Move on to the next one.
diffIndex += 1;
diffEntry = diff[diffIndex];
} else if (stateArrayIndex < arrayOfState.length) {
// No - the current item was retained. Just update its index.
outputArrayIndex = updateRetainedOutputItem(arrayOfState[stateArrayIndex], stateArrayIndex, outputArrayIndex);
}
}
outputObservableArray.valueHasMutated();
}, null, 'arrayChange');
}
ko.observableArray.fn.map = ko.transformations.fn.map = function map(mappingOptions) {
var that = this,
arrayOfState = [],
outputArray = [],
outputObservableArray = ko.observableArray(outputArray),
originalInputArrayContents = that.peek();
// Shorthand syntax - just pass a function instead of an options object
if (typeof mappingOptions === 'function') {
mappingOptions = { mapping: mappingOptions };
}
if (!mappingOptions.exclusionMarker) {
mappingOptions.exclusionMarker = {};
}
// Validate the options
if (mappingOptions.mappingWithDisposeCallback) {
if (mappingOptions.mapping || mappingOptions.disposeItem) {
throw new Error('\'mappingWithDisposeCallback\' cannot be used in conjunction with \'mapping\' or \'disposeItem\'.');
}
} else if (!mappingOptions.mapping) {
throw new Error('Specify either \'mapping\' or \'mappingWithDisposeCallback\'.');
}
// Initial state: map each of the inputs
for (var i = 0; i < originalInputArrayContents.length; i += 1) {
var inputItem = originalInputArrayContents[i],
stateItem = new StateItem(inputItem, i, outputArray.length, mappingOptions, arrayOfState, outputObservableArray),
mappedValue = stateItem.mappedValueComputed.peek();
arrayOfState.push(stateItem);
if (stateItem.isIncluded) {
outputArray.push(mappedValue);
}
}
// If the input array changes structurally (items added or removed), update the outputs
var inputArraySubscription = respondToArrayStructuralChanges(that, arrayOfState, outputArray, outputObservableArray, mappingOptions);
var outputComputed = ko.computed(outputObservableArray);
if ('throttle' in mappingOptions) {
outputComputed = outputComputed.extend({ throttle: mappingOptions.throttle });
}
// Return value is a readonly computed which can track its own changes to permit chaining.
// When disposed, it cleans up everything it created.
var returnValue = outputComputed.extend({ trackArrayChanges: true }),
originalDispose = returnValue.dispose;
returnValue.dispose = function () {
inputArraySubscription.dispose();
ko.utils.arrayForEach(arrayOfState, function (stateItem) {
stateItem.dispose();
});
originalDispose.call(this, arguments);
};
// Make transformations chainable
ko.utils.extend(returnValue, ko.transformations.fn);
return returnValue;
};
return ko.transformations.fn.map;
}));
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory(require('knockout'), require('./map'));
} else if (typeof define === 'function' && define.amd) {
define(['knockout', './map'], factory);
} else {
var ko = root.ko;
factory(ko, ko.transformations.fn.map);
}
}(this, function (ko, map) {
ko.observable.fn.filter = ko.transformations.fn.filter = function filter(mappingOptions) {
// Shorthand syntax - just pass a function instead of an options object
if (typeof mappingOptions === 'function') {
mappingOptions = { mapping: mappingOptions };
}
var predicate = mappingOptions.mapping;
var exclusionMarker = {};
mappingOptions.mapping = function (item) {
return predicate(item) ? item : exclusionMarker;
};
mappingOptions.exclusionMarker = exclusionMarker;
return map.call(this, mappingOptions);
};
return ko.transformations.fn.filter;
}));
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory(require('knockout'));
} else if (typeof define === 'function' && define.amd) {
define(['knockout'], factory);
} else {
factory(root.ko);
}
}(this, function (ko) {
ko.transformations = ko.transformations || {
fn: {}
};
function sortingKeysEquals(aSortKeys, bSortKeys) {
var Descending = SortByTransformation.Descending;
if (!Array.isArray(aSortKeys)) {
aSortKeys = [aSortKeys];
bSortKeys = [bSortKeys];
}
var aSortKey, bSortKey;
for (var i = 0; i < aSortKeys.length; i += 1) {
aSortKey = aSortKeys[i];
bSortKey = bSortKeys[i];
if (aSortKey instanceof Descending) {
if (bSortKey instanceof Descending) {
aSortKey = aSortKey.value;
bSortKey = bSortKey.value;
} else {
return false;
}
}
if (aSortKey !== bSortKey) {
return false;
}
}
return true;
}
function compareSortingKeys(aSortKeys, bSortKeys, comparator) {
var Descending = SortByTransformation.Descending;
if (!Array.isArray(aSortKeys)) {
aSortKeys = [aSortKeys];
bSortKeys = [bSortKeys];
}
var aSortKey, bSortKey, comparison;
for (var i = 0; i < aSortKeys.length; i += 1) {
aSortKey = aSortKeys[i];
bSortKey = bSortKeys[i];
if (aSortKey instanceof Descending) {
comparison = comparator(bSortKey.value, aSortKey.value);
} else {
comparison = comparator(aSortKey, bSortKey);
}
if (comparison !== 0) {
return comparison;
}
}
return 0;
}
// Sorting
function mappingToComparefn(mapping, comparator) {
var Descending = SortByTransformation.Descending;
return function (a, b) {
var aSortKeys = mapping(a, Descending.create);
var bSortKeys = mapping(b, Descending.create);
return compareSortingKeys(aSortKeys, bSortKeys, comparator);
};
}
function binarySearch(items, item, comparefn) {
var left = -1,
right = items.length,
mid;
while (right - left > 1) {
mid = (left + right) >>> 1;
var c = comparefn(items[mid], item);
if (c < 0) {
left = mid;
} else {
right = mid;
if (!c) {
break;
}
}
}
return (right === items.length || comparefn(items[right], item)) ? -right - 1 : right;
}
function findInsertionIndex(items, newItem, comparefn) {
var left = -1,
right = items.length,
mid;
while (right - left > 1) {
mid = (left + right) >>> 1;
if (comparefn(items[mid], newItem) < 0) {
left = mid;
} else {
right = mid;
}
}
return right;
}
function binaryIndexOf(items, item, comparefn) {
var index = binarySearch(items, item, comparefn);
if (index < 0 || items.length <= index || comparefn(items[index], item) !== 0) {
return -1;
} else {
var startIndex = index;
// find the first index of an item that looks like the item
while (index - 1 >= 0 && comparefn(items[index - 1], item) === 0) {
index -= 1;
}
// find the index of the item
while (index <= startIndex) {
if (items[index] === item) {
return index;
}
index += 1;
}
while (index < items.length) {
if (comparefn(items[index], item) !== 0) {
return -1;
}
if (items[index] === item) {
return index;
}
index += 1;
}
return -1;
}
}
function SortedStateItem(transformation, inputItem) {
this.transformation = transformation;
this.inputItem = inputItem;
this.mappedValueComputed = ko.computed(this.mappingEvaluator, this);
this.mappedValueComputed.subscribe(this.onMappingResultChanged, this);
this.previousMappedValue = this.mappedValueComputed.peek();
}
SortedStateItem.prototype.dispose = function () {
var mappedItem = this.mappedValueComputed();
this.mappedValueComputed.dispose();
if (this.transformation.options.disposeItem) {
this.transformation.options.disposeItem(mappedItem);
}
};
SortedStateItem.prototype.mappingEvaluator = function () {
return this.transformation.mapping(this.inputItem, SortByTransformation.Descending.create);
};
SortedStateItem.prototype.onMappingResultChanged = function (newValue) {
if (!sortingKeysEquals(newValue, this.previousMappedValue)) {
var transformation = this.transformation;
var outputObservable = transformation.outputObservable;
var outputArray = outputObservable.peek();
var stateItems = transformation.stateItems;
var oldIndex = binaryIndexOf(stateItems, this, mappingToComparefn(function (stateItem) {
return stateItem.previousMappedValue;
}, transformation.comparator));
if (stateItems[oldIndex] === this) {
outputObservable.valueWillMutate();
// It seems the sort order of the underlying array is still usable
outputArray.splice(oldIndex, 1);
stateItems.splice(oldIndex, 1);
var index = findInsertionIndex(outputArray, this.inputItem, transformation.comparefn);
outputArray.splice(index, 0, this.inputItem);
stateItems.splice(index, 0, this);
this.previousMappedValue = newValue;
outputObservable.valueHasMutated();
} else {
ko.utils.arrayForEach(stateItems, function (stateItem) {
stateItem.previousMappedValue = stateItem.mappingEvaluator();
});
// The underlying array needs to be recalculated from scratch
stateItems.sort(mappingToComparefn(function (stateItem) {
return stateItem.previousMappedValue;
}, transformation.comparator));
outputArray = [];
ko.utils.arrayForEach(stateItems, function (stateItem) {
outputArray.push(stateItem.inputItem);
});
outputObservable(outputArray);
}
}
};
function SortByTransformation(inputObservableArray, options) {
var that = this;
this.options = options;
this.mapping = options.mapping;
if (options.comparator) {
this.comparator = options.comparator;
} else {
this.comparator = function (a, b) {
if (a > b) {
return 1;
} else if (b > a) {
return -1;
} else {
return 0;
}
};
}
this.comparefn = mappingToComparefn(this.mapping, this.comparator);
this.stateItems = ko.utils.arrayMap(inputObservableArray.peek(), function (inputItem) {
return new SortedStateItem(that, inputItem);
});
this.stateItems.sort(function (a, b) {
return compareSortingKeys(a.mappedValueComputed(), b.mappedValueComputed(), that.comparator);
});
this.outputObservable = ko.observable(ko.utils.arrayMap(this.stateItems, function (stateItem) {
return stateItem.inputItem;
}));
// If the input array changes structurally (items added or removed), update the outputs
var inputArraySubscription = inputObservableArray.subscribe(this.onStructuralChange, this, 'arrayChange');
var outputComputed = ko.computed(this.outputObservable);
if ('throttle' in options) {
outputComputed = outputComputed.extend({ throttle: options.throttle });
}
// Return value is a readonly computed which can track its own changes to permit chaining.
// When disposed, it cleans up everything it created.
this.output = outputComputed.extend({ trackArrayChanges: true });
var originalDispose = this.output.dispose;
this.output.dispose = function () {
inputArraySubscription.dispose();
ko.utils.arrayForEach(that.stateItems, function (stateItem) {
stateItem.dispose();
});
originalDispose.call(this, arguments);
};
ko.utils.extend(this.output, ko.transformations.fn);
}
SortByTransformation.Descending = function Descending(value) {
this.value = value;
};
SortByTransformation.Descending.create = function (value) {
return new SortByTransformation.Descending(value);
};
SortByTransformation.prototype.onStructuralChange = function (diff) {
if (!diff.length) {
return;
}
this.outputObservable.valueWillMutate();
var that = this;
var addQueue = [];
var deleteQueue = [];
ko.utils.arrayForEach(diff, function (diffEntry) {
if (typeof diffEntry.moved !== 'number') {
switch (diffEntry.status) {
case 'added':
addQueue.push(diffEntry);
break;
case 'deleted':
deleteQueue.push(diffEntry);
break;
}
}
});
var outputArray = this.outputObservable.peek();
ko.utils.arrayForEach(deleteQueue, function (diffEntry) {
var index = binaryIndexOf(outputArray, diffEntry.value, that.comparefn);
if (index !== -1) {
outputArray.splice(index, 1);
that.stateItems[index].dispose();
that.stateItems.splice(index, 1);
}
});
if (deleteQueue.length === 0 && this.stateItems.length === 0) {
// Adding to an empty array
this.stateItems = ko.utils.arrayMap(addQueue, function (diffEntry) {
return new SortedStateItem(that, diffEntry.value);
});
this.stateItems.sort(function (a, b) {
return compareSortingKeys(a.mappedValueComputed(), b.mappedValueComputed(), that.comparator);
});
ko.utils.arrayForEach(this.stateItems, function (stateItem) {
outputArray.push(stateItem.inputItem);
});
} else {
ko.utils.arrayForEach(addQueue, function (diffEntry) {
var index = findInsertionIndex(outputArray, diffEntry.value, that.comparefn);
var stateItem = new SortedStateItem(that, diffEntry.value);
outputArray.splice(index, 0, stateItem.inputItem);
that.stateItems.splice(index, 0, stateItem);
});
}
this.outputObservable.valueHasMutated();
};
ko.observableArray.fn.sortBy = ko.transformations.fn.sortBy = function sortBy(options) {
// Shorthand syntax - just pass a function instead of an options object
if (typeof options === 'function') {
options = { mapping: options };
}
var transformation = new SortByTransformation(this, options);
return transformation.output;
};
return ko.transformations.fn.sortBy;
}));
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory(require('knockout'));
} else if (typeof define === 'function' && define.amd) {
define(['knockout'], factory);
} else {
factory(root.ko);
}
}(this, function (ko) {
ko.transformations = ko.transformations || {
fn: {}
};
function IndexByTransformation(inputObservableArray, options) {
var that = this;
this.options = options;
this.outputObservable = ko.observable({});
this.stateItems = {};
this.mapping = function (item) {
return [].concat(options.mapping(item));
};
var inputArray = inputObservableArray.peek();
for (var i = 0; i < inputArray.length; i += 1) {
this.addToIndex(inputArray[i], i);
}
// If the input array changes structurally (items added or removed), update the outputs
var inputArraySubscription = inputObservableArray.subscribe(this.onStructuralChange, this, 'arrayChange');
var outputComputed = ko.computed(this.outputObservable);
if ('throttle' in options) {
outputComputed = outputComputed.extend({ throttle: options.throttle });
}
// Return value is a readonly, when disposed, it cleans up everything it created.
this.output = outputComputed;
var originalDispose = this.output.dispose;
this.output.dispose = function () {
inputArraySubscription.dispose();
for (var prop in that.stateItems) {
if (that.stateItems.hasOwnProperty(prop)) {
that.stateItems[prop].dispose();
}
}
originalDispose.call(this, arguments);
};
ko.utils.extend(this.output, ko.transformations.fn);
}
IndexByTransformation.prototype.arraysEqual = function (a, b) {
if (a === b) {
return true;
}
if (typeof a === 'undefined' || typeof b === 'undefined') {
return false;
}
if (a.length !== b.length) {
return false;
}
for (var i = 0; i < a.length; i += 1) {
if ((ko.observable.fn.equalityComparer &&
ko.isObservable(a[i]) &&
!ko.observable.fn.equalityComparer(a[i], b[i])) ||
a[i] !== b[i]) {
return false;
}
}
return true;
};
IndexByTransformation.prototype.appendToEntry = function (obj, key, item) {
var entry = obj[key];
if (!entry) {
entry = obj[key] = [];
}
entry.push(item);
};
IndexByTransformation.prototype.removeFromEntry = function (obj, key, item) {
var entry = obj[key];
if (entry) {
var index = entry.indexOf(item);
if (index !== -1) {
if (entry.length === 1) {
delete obj[key];
} else {
entry.splice(index, 1);
}
}
}
};
IndexByTransformation.prototype.insertByKeyAndItem = function (indexMapping, key, item) {
this.appendToEntry(indexMapping, key, item);
};
IndexByTransformation.prototype.removeByKeyAndItem = function (indexMapping, key, item) {
this.removeFromEntry(indexMapping, key, item);
};
IndexByTransformation.prototype.addStateItemToIndex = function (stateItem) {
var key = this.mapping(stateItem.inputItem)[0];
this.appendToEntry(this.stateItems, key, stateItem);
};
IndexByTransformation.prototype.findStateItem = function (inputItem) {
var key = this.mapping(inputItem)[0];
var entry = this.stateItems[key];
if (!entry) {
return null;
}
var result = ko.utils.arrayFilter(entry, function (stateItem) {
return stateItem.inputItem === inputItem;
});
return result[0] || null;
};
IndexByTransformation.prototype.removeStateItem = function (stateItem) {
var key = stateItem.mappedValueComputed()[0];
this.removeFromEntry(this.stateItems, key, stateItem);
stateItem.dispose();
};
IndexByTransformation.prototype.addToIndex = function (inputItem) {
var that = this;
var keys = this.mapping(inputItem);
var output = this.outputObservable.peek();
ko.utils.arrayForEach(keys, function (key) {
that.insertByKeyAndItem(output, key, inputItem);
});
var stateItem = new IndexedStateItem(this, inputItem);
this.addStateItemToIndex(stateItem);
};
IndexByTransformation.prototype.removeItem = function (inputItem) {
var that = this;
var stateItem = this.findStateItem(inputItem);
if (stateItem) {
var keys = stateItem.mappedValueComputed();
var output = this.outputObservable.peek();
ko.utils.arrayForEach(keys, function (key) {
that.removeByKeyAndItem(output, key, inputItem);
});
this.removeStateItem(stateItem);
}
};
IndexByTransformation.prototype.onStructuralChange = function (diff) {
var that = this;
if (!diff.length) {
return;
}
var addQueue = [];
var deleteQueue = [];
ko.utils.arrayForEach(diff, function (diffEntry) {
if (typeof diffEntry.moved !== 'number') {
switch (diffEntry.status) {
case 'added':
addQueue.push(diffEntry);
break;
case 'deleted':
deleteQueue.push(diffEntry);
break;
}
}
});
ko.utils.arrayForEach(deleteQueue, function (diffEntry) {
that.removeItem(diffEntry.value, diffEntry.index);
});
ko.utils.arrayForEach(addQueue, function (diffEntry) {
that.addToIndex(diffEntry.value, diffEntry.index);
});
this.outputObservable.valueHasMutated();
};
function IndexedStateItem(transformation, inputItem) {
this.transformation = transformation;
this.inputItem = inputItem;
this.mappedValueComputed = ko.computed(this.mappingEvaluator, this);
this.mappedValueComputed.subscribe(this.onMappingResultChanged, this);
this.previousMappedValue = this.mappedValueComputed.peek();
}
IndexedStateItem.prototype.dispose = function () {
var mappedItem = this.mappedValueComputed();
this.mappedValueComputed.dispose();
if (this.transformation.options.disposeItem) {
this.transformation.options.disposeItem(mappedItem);
}
};
IndexedStateItem.prototype.mappingEvaluator = function () {
return this.transformation.mapping(this.inputItem);
};
function toArray(value) {
return Array.isArray(value) ? value : [value];
}
IndexedStateItem.prototype.onMappingResultChanged = function (newValue) {
var transformation = this.transformation;
if (!transformation.arraysEqual(this.newValue, this.previousMappedValue)) {
var outputObservable = transformation.outputObservable;
var output = outputObservable.peek();
outputObservable.valueWillMutate();
var that = this;
ko.utils.arrayForEach(toArray(this.previousMappedValue), function (key) {
transformation.removeByKeyAndItem(output, key, that.inputItem);
transformation.removeByKeyAndItem(transformation.stateItems, key, that);
});
ko.utils.arrayForEach(toArray(newValue), function (key) {
transformation.insertByKeyAndItem(output, key, that.inputItem);
});
transformation.addStateItemToIndex(this);
this.previousMappedValue = newValue;
outputObservable.valueHasMutated();
}
};
function UniqueIndexByTransformation(inputObservableArray, options) {
IndexByTransformation.call(this, inputObservableArray, options);
}
ko.utils.extend(UniqueIndexByTransformation.prototype, IndexByTransformation.prototype);
UniqueIndexByTransformation.prototype.insertByKeyAndItem = function (indexMapping, key, item) {
if (key in indexMapping) {
throw new Error('Unique indexes requires items must map to different keys; duplicate key: ' + key);
}
indexMapping[key] = item;
};
UniqueIndexByTransformation.prototype.removeByKeyAndItem = function (indexMapping, key) {
delete indexMapping[key];
};
UniqueIndexByTransformation.prototype.addStateItemToIndex = function (stateItem) {
var key = stateItem.mappedValueComputed()[0];
this.stateItems[key] = stateItem;
};
UniqueIndexByTransformation.prototype.findStateItem = function (inputItem) {
var key = this.mapping(inputItem)[0];
return this.stateItems[key] || null;
};
UniqueIndexByTransformation.prototype.removeStateItem = function (stateItem) {
var key = stateItem.mappedValueComputed()[0];
if (this.stateItems[key] === stateItem) {
delete this.stateItems[key];
}
stateItem.dispose();
};
UniqueIndexByTransformation.prototype.addToIndex = function (inputItem) {
var that = this;
var keys = this.mapping(inputItem);
var output = this.outputObservable.peek();
ko.utils.arrayForEach(keys, function (key) {
that.insertByKeyAndItem(output, key, inputItem);
});
var stateItem = new UniqueIndexedStateItem(this, inputItem);
this.addStateItemToIndex(stateItem);
};
UniqueIndexByTransformation.prototype.removeItem = function (inputItem) {
var that = this;
var stateItem = this.findStateItem(inputItem);
if (stateItem) {
var keys = stateItem.mappedValueComputed();
var output = this.outputObservable.peek();
ko.utils.arrayForEach(keys, function (key) {
that.removeByKeyAndItem(output, key, inputItem);
});
this.removeStateItem(stateItem);
}
};
function UniqueIndexedStateItem(transformation, inputItem) {
IndexedStateItem.call(this, transformation, inputItem);
}
ko.utils.extend(UniqueIndexedStateItem.prototype, IndexedStateItem.prototype);
ko.observableArray.fn.indexBy = ko.transformations.fn.indexBy = function indexBy(options) {
// Shorthand syntax - just pass a function instead of an options object
if (typeof options === 'function') {
options = { mapping: options, unique: false };
}
var transformation = options.unique ?
new UniqueIndexByTransformation(this, options) :
new IndexByTransformation(this, options);
return transformation.output;
};
ko.observableArray.fn.uniqueIndexBy = ko.transformations.fn.uniqueIndexBy = function uniqueIndexBy(options) {
// Shorthand syntax - just pass a function instead of an options object
if (typeof options === 'function') {
options = { mapping: options };
}
options.unique = true;
var transformation = new UniqueIndexByTransformation(this, options);
return transformation.output;
};
return ko.transformations.fn.indexBy;
}));
knockout-transformations-2.1.0/dist/knockout-transformations.min.js 0000664 0000000 0000000 00000062213 12565046077 0025765 0 ustar 00root root 0000000 0000000 /*!
Copyright 2015 One.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.
*/
(function(root,factory){if(typeof exports==="object"){module.exports=factory(require("knockout"))}else if(typeof define==="function"&&define.amd){define(["knockout"],factory)}else{factory(root.ko)}})(this,function(ko){ko.transformations=ko.transformations||{fn:{}};function StateItem(inputItem,initialStateArrayIndex,initialOutputArrayIndex,mappingOptions,arrayOfState,outputObservableArray){this.inputItem=inputItem;this.stateArrayIndex=initialStateArrayIndex;this.mappingOptions=mappingOptions;this.arrayOfState=arrayOfState;this.outputObservableArray=outputObservableArray;this.outputArray=this.outputObservableArray.peek();this.isIncluded=null;this.suppressNotification=false;this.outputArrayIndex=ko.observable(initialOutputArrayIndex);this.disposeFuncFromMostRecentMapping=null;this.mappedValueComputed=ko.computed(this.mappingEvaluator,this);this.mappedValueComputed.subscribe(this.onMappingResultChanged,this);this.previousMappedValue=this.mappedValueComputed.peek()}StateItem.prototype.dispose=function(){this.mappedValueComputed.dispose();this.disposeResultFromMostRecentEvaluation()};StateItem.prototype.disposeResultFromMostRecentEvaluation=function(){if(this.disposeFuncFromMostRecentMapping){this.disposeFuncFromMostRecentMapping();this.disposeFuncFromMostRecentMapping=null}if(this.mappingOptions.disposeItem){var mappedItem=this.mappedValueComputed();this.mappingOptions.disposeItem(mappedItem)}};StateItem.prototype.mappingEvaluator=function(){if(this.isIncluded!==null){this.disposeResultFromMostRecentEvaluation()}var mappedValue;if(this.mappingOptions.mapping){mappedValue=this.mappingOptions.mapping(this.inputItem,this.outputArrayIndex)}else if(this.mappingOptions.mappingWithDisposeCallback){var mappedValueWithDisposeCallback=this.mappingOptions.mappingWithDisposeCallback(this.inputItem,this.outputArrayIndex);if(!("mappedValue"in mappedValueWithDisposeCallback)){throw new Error("Return value from mappingWithDisposeCallback should have a 'mappedItem' property.")}mappedValue=mappedValueWithDisposeCallback.mappedValue;this.disposeFuncFromMostRecentMapping=mappedValueWithDisposeCallback.dispose}else{throw new Error("No mapping callback given.")}if(this.isIncluded===null){this.isIncluded=mappedValue!==this.mappingOptions.exclusionMarker}return mappedValue};StateItem.prototype.updateInclusion=function(){var outputArrayIndex=this.outputArrayIndex.peek();var outputArray=this.outputArray;for(var iterationIndex=this.stateArrayIndex;iterationIndex1){mid=left+right>>>1;var c=comparefn(items[mid],item);if(c<0){left=mid}else{right=mid;if(!c){break}}}return right===items.length||comparefn(items[right],item)?-right-1:right}function findInsertionIndex(items,newItem,comparefn){var left=-1,right=items.length,mid;while(right-left>1){mid=left+right>>>1;if(comparefn(items[mid],newItem)<0){left=mid}else{right=mid}}return right}function binaryIndexOf(items,item,comparefn){var index=binarySearch(items,item,comparefn);if(index<0||items.length<=index||comparefn(items[index],item)!==0){return-1}else{var startIndex=index;while(index-1>=0&&comparefn(items[index-1],item)===0){index-=1}while(index<=startIndex){if(items[index]===item){return index}index+=1}while(indexb){return 1}else if(b>a){return-1}else{return 0}}}this.comparefn=mappingToComparefn(this.mapping,this.comparator);this.stateItems=ko.utils.arrayMap(inputObservableArray.peek(),function(inputItem){return new SortedStateItem(that,inputItem)});this.stateItems.sort(function(a,b){return compareSortingKeys(a.mappedValueComputed(),b.mappedValueComputed(),that.comparator)});this.outputObservable=ko.observable(ko.utils.arrayMap(this.stateItems,function(stateItem){return stateItem.inputItem}));var inputArraySubscription=inputObservableArray.subscribe(this.onStructuralChange,this,"arrayChange");var outputComputed=ko.computed(this.outputObservable);if("throttle"in options){outputComputed=outputComputed.extend({throttle:options.throttle})}this.output=outputComputed.extend({trackArrayChanges:true});var originalDispose=this.output.dispose;this.output.dispose=function(){inputArraySubscription.dispose();ko.utils.arrayForEach(that.stateItems,function(stateItem){stateItem.dispose()});originalDispose.call(this,arguments)};ko.utils.extend(this.output,ko.transformations.fn)}SortByTransformation.Descending=function Descending(value){this.value=value};SortByTransformation.Descending.create=function(value){return new SortByTransformation.Descending(value)};SortByTransformation.prototype.onStructuralChange=function(diff){if(!diff.length){return}this.outputObservable.valueWillMutate();var that=this;var addQueue=[];var deleteQueue=[];ko.utils.arrayForEach(diff,function(diffEntry){if(typeof diffEntry.moved!=="number"){switch(diffEntry.status){case"added":addQueue.push(diffEntry);break;case"deleted":deleteQueue.push(diffEntry);break}}});var outputArray=this.outputObservable.peek();ko.utils.arrayForEach(deleteQueue,function(diffEntry){var index=binaryIndexOf(outputArray,diffEntry.value,that.comparefn);if(index!==-1){outputArray.splice(index,1);that.stateItems[index].dispose();that.stateItems.splice(index,1)}});if(deleteQueue.length===0&&this.stateItems.length===0){this.stateItems=ko.utils.arrayMap(addQueue,function(diffEntry){return new SortedStateItem(that,diffEntry.value)});this.stateItems.sort(function(a,b){return compareSortingKeys(a.mappedValueComputed(),b.mappedValueComputed(),that.comparator)});ko.utils.arrayForEach(this.stateItems,function(stateItem){outputArray.push(stateItem.inputItem)})}else{ko.utils.arrayForEach(addQueue,function(diffEntry){var index=findInsertionIndex(outputArray,diffEntry.value,that.comparefn);var stateItem=new SortedStateItem(that,diffEntry.value);outputArray.splice(index,0,stateItem.inputItem);that.stateItems.splice(index,0,stateItem)})}this.outputObservable.valueHasMutated()};ko.observableArray.fn.sortBy=ko.transformations.fn.sortBy=function sortBy(options){if(typeof options==="function"){options={mapping:options}}var transformation=new SortByTransformation(this,options);return transformation.output};return ko.transformations.fn.sortBy});(function(root,factory){if(typeof exports==="object"){module.exports=factory(require("knockout"))}else if(typeof define==="function"&&define.amd){define(["knockout"],factory)}else{factory(root.ko)}})(this,function(ko){ko.transformations=ko.transformations||{fn:{}};function IndexByTransformation(inputObservableArray,options){var that=this;this.options=options;this.outputObservable=ko.observable({});this.stateItems={};this.mapping=function(item){return[].concat(options.mapping(item))};var inputArray=inputObservableArray.peek();for(var i=0;i