-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
152 additions
and
55 deletions.
There are no files selected for viewing
149 changes: 113 additions & 36 deletions
149
newscoop/src/Newscoop/NewscoopBundle/Resources/public/js/angular-ui-tree-filter.min.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,114 @@ | ||
/*! angular-ui-tree-filter 0.1.0, 06-06-2014 */ ! function(a) { | ||
"use strict"; | ||
a.module("ui.tree-filter", []).provider("uiTreeFilterSettings", function() { | ||
var a = this; | ||
this.addresses = ["title"], this.regexFlags = "gi", this.descendantCollection = "items", this.$get = function() { | ||
return { | ||
addresses: a.addresses, | ||
regexFlags: a.regexFlags, | ||
descendantCollection: a.descendantCollection | ||
(function (angular) { | ||
'use strict'; | ||
|
||
angular.module('ui.tree-filter', []) | ||
/** | ||
* @ngdoc object | ||
* @name ui.tree-filter.provider:uiTreeFilterSettings | ||
*/ | ||
.provider('uiTreeFilterSettings', function () { | ||
|
||
var uiTreeFilterSettings = this; | ||
|
||
this.addresses = ['title']; | ||
this.regexFlags = 'gi'; | ||
this.descendantCollection = 'items'; | ||
|
||
this.$get = function () { | ||
return { | ||
addresses: uiTreeFilterSettings.addresses, | ||
regexFlags: uiTreeFilterSettings.regexFlags, | ||
descendantCollection: uiTreeFilterSettings.descendantCollection, | ||
}; | ||
}; | ||
}) | ||
/** | ||
* @ngdoc function | ||
* @name project.factory:projectWbsFilter | ||
*/ | ||
.filter('uiTreeFilter', ["uiTreeFilterSettings", function (uiTreeFilterSettings) { | ||
/** | ||
* Iterates through given collection if flag is not true and sets a flag to true on first match. | ||
* | ||
* @param {Array} collection | ||
* @param {string} pattern | ||
* @param {string} address | ||
* | ||
* @returns {boolean} | ||
*/ | ||
function visit(collection, pattern, address, scope) { | ||
collection = collection || []; | ||
var foundSoFar = false; | ||
|
||
collection.forEach(function (collectionItem) { | ||
foundSoFar = foundSoFar || testForField(collectionItem, pattern, address, scope); | ||
if (foundSoFar) { | ||
scope.expanded = true; | ||
} else { | ||
scope.expanded = false; | ||
} | ||
}); | ||
|
||
return foundSoFar; | ||
} | ||
|
||
/** | ||
* Resolves object value from dot-delimited address. | ||
* | ||
* @param object | ||
* @param path | ||
* @returns {*} | ||
*/ | ||
function resolveAddress(object, path) { | ||
var parts = path.split('.'); | ||
return parts.length < 2 ? object[parts[0]] : resolveAddress(object[parts[0]], parts.slice(1).join('.')); | ||
} | ||
|
||
/** | ||
* Checks if object or its children matches a pattern on a given field | ||
* | ||
* First it resolves the property address and gets the value. | ||
* If the value is a string it matches it against provided pattern. | ||
* If item matches because its property matches it's children are not checked. | ||
* Otherwise all item descendants are checked as well | ||
* | ||
* @param {Object} item | ||
* @param {string} pattern | ||
* @param {string} address property name or dot-delimited path to property. | ||
* | ||
* @returns {boolean} | ||
*/ | ||
function testForField(item, pattern, address, scope) { | ||
var value = resolveAddress(item, address); | ||
var found = typeof value === 'string' ? | ||
!!value.match(new RegExp(pattern, uiTreeFilterSettings.regexFlags)) : | ||
false; | ||
return found || visit(item[uiTreeFilterSettings.descendantCollection], pattern, address, scope); | ||
} | ||
} | ||
}).filter("uiTreeFilter", ["uiTreeFilterSettings", function(a) { | ||
function b(a, b, c) { | ||
a = a || []; | ||
var e = !1; | ||
return a.forEach(function(a) { | ||
e = e || d(a, b, c) | ||
}), e | ||
} | ||
|
||
function c(a, b) { | ||
var d = b.split("."); | ||
return d.length < 2 ? a[d[0]] : c(a[d[0]], d.slice(1).join(".")) | ||
} | ||
|
||
function d(d, e, f) { | ||
var g = c(d, f), | ||
h = "string" == typeof g ? !!g.match(new RegExp(e, a.regexFlags)) : !1; | ||
return h || b(d[a.descendantCollection], e, f) | ||
} | ||
return function(b, c, e) { | ||
return e = e || a.addresses, void 0 === c || e.reduce(function(a, e) { | ||
return a || d(b, c, e) | ||
}, !1) | ||
} | ||
}]) | ||
}(angular); | ||
|
||
/** | ||
* Checks if pattern matches any of addresses | ||
* | ||
* @param {object} item | ||
* @param {string} pattern | ||
* | ||
* @returns {boolean} | ||
*/ | ||
return function (item, pattern, addresses, scope) { | ||
addresses = addresses || uiTreeFilterSettings.addresses; | ||
if (pattern !== undefined && pattern.length <= 3) { | ||
scope.expanded = false; | ||
|
||
return true; | ||
} | ||
|
||
return pattern === undefined || addresses.reduce(function (foundSoFar, fieldName) { | ||
if (!foundSoFar || !testForField(item, pattern, fieldName, scope)) { | ||
scope.expanded = false; | ||
} | ||
|
||
return foundSoFar || testForField(item, pattern, fieldName, scope); | ||
}, false); | ||
}; | ||
}]); | ||
})(angular); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters