Commit 81a065b4 by Samuel Padgett

Add KeywordService, highlightKeywords filter

parent f250d002
...@@ -2340,6 +2340,59 @@ angular.module('openshiftCommonServices') ...@@ -2340,6 +2340,59 @@ angular.module('openshiftCommonServices')
}); });
;'use strict'; ;'use strict';
angular.module("openshiftCommonServices")
.service("KeywordService", function(){
var generateKeywords = function(filterText) {
if (!filterText) {
return [];
}
var keywords = _.uniq(filterText.match(/\S+/g));
// Sort the longest keyword first.
keywords.sort(function(a, b){
return b.length - a.length;
});
// Convert the keyword to a case-insensitive regular expression for the filter.
return _.map(keywords, function(keyword) {
return new RegExp(_.escapeRegExp(keyword), "i");
});
};
var filterForKeywords = function(objects, filterFields, keywords) {
var filteredObjects = objects;
if (_.isEmpty(keywords)) {
return filteredObjects;
}
// Find resources that match all keywords.
angular.forEach(keywords, function(regex) {
var matchesKeyword = function(obj) {
var i;
for (i = 0; i < filterFields.length; i++) {
var value = _.get(obj, filterFields[i]);
if (value && regex.test(value)) {
return true;
}
}
return false;
};
filteredObjects = _.filter(filteredObjects, matchesKeyword);
});
return filteredObjects;
};
return {
filterForKeywords: filterForKeywords,
generateKeywords: generateKeywords
};
});
;'use strict';
angular.module('openshiftCommonServices') angular.module('openshiftCommonServices')
.provider('Logger', function() { .provider('Logger', function() {
this.$get = function() { this.$get = function() {
......
...@@ -749,6 +749,59 @@ angular ...@@ -749,6 +749,59 @@ angular
;'use strict'; ;'use strict';
angular.module('openshiftCommonUI') angular.module('openshiftCommonUI')
.filter('highlightKeywords', function(KeywordService) {
// Returns HTML wrapping the matching words in a `mark` tag.
return function(str, keywords, caseSensitive) {
if (!str) {
return str;
}
if (_.isEmpty(keywords)) {
return _.escape(str);
}
// If passed a plain string, get the keywords from KeywordService.
if (_.isString(keywords)) {
keywords = KeywordService.generateKeywords(keywords);
}
// Combine the keywords into a single regex.
var source = _.map(keywords, function(keyword) {
if (_.isRegExp(keyword)) {
return keyword.source;
}
return _.escapeRegExp(keyword);
}).join('|');
// Search for matches.
var match;
var result = '';
var lastIndex = 0;
var flags = caseSensitive ? 'g' : 'ig';
var regex = new RegExp(source, flags);
while ((match = regex.exec(str)) !== null) {
// Escape any text between the end of the last match and the start of
// this match, and add it to the result.
if (lastIndex < match.index) {
result += _.escape(str.substring(lastIndex, match.index));
}
// Wrap the match in a `mark` element to use the Bootstrap / Patternfly highlight styles.
result += "<mark>" + _.escape(match[0]) + "</mark>";
lastIndex = regex.lastIndex;
}
// Escape any remaining text and add it to the result.
if (lastIndex < str.length) {
result += _.escape(str.substring(lastIndex));
}
return result;
};
});
;'use strict';
angular.module('openshiftCommonUI')
.filter('orderObjectsByDate', function(toArrayFilter) { .filter('orderObjectsByDate', function(toArrayFilter) {
return function(items, reverse) { return function(items, reverse) {
items = toArrayFilter(items); items = toArrayFilter(items);
......
...@@ -24,6 +24,13 @@ ...@@ -24,6 +24,13 @@
.dialog-btn:first-of-type { .dialog-btn:first-of-type {
margin-right: 0; margin-right: 0;
} }
mark {
padding: 0;
background-color: rgba(252, 248, 160, 0.5);
}
.ui-select-choices-row.active > span mark {
background-color: rgba(252, 248, 160, 0.75);
}
ul.messenger.messenger-theme-flat { ul.messenger.messenger-theme-flat {
margin: 0; margin: 0;
padding: 0; padding: 0;
......
...@@ -954,6 +954,59 @@ angular ...@@ -954,6 +954,59 @@ angular
;'use strict'; ;'use strict';
angular.module('openshiftCommonUI') angular.module('openshiftCommonUI')
.filter('highlightKeywords', ["KeywordService", function(KeywordService) {
// Returns HTML wrapping the matching words in a `mark` tag.
return function(str, keywords, caseSensitive) {
if (!str) {
return str;
}
if (_.isEmpty(keywords)) {
return _.escape(str);
}
// If passed a plain string, get the keywords from KeywordService.
if (_.isString(keywords)) {
keywords = KeywordService.generateKeywords(keywords);
}
// Combine the keywords into a single regex.
var source = _.map(keywords, function(keyword) {
if (_.isRegExp(keyword)) {
return keyword.source;
}
return _.escapeRegExp(keyword);
}).join('|');
// Search for matches.
var match;
var result = '';
var lastIndex = 0;
var flags = caseSensitive ? 'g' : 'ig';
var regex = new RegExp(source, flags);
while ((match = regex.exec(str)) !== null) {
// Escape any text between the end of the last match and the start of
// this match, and add it to the result.
if (lastIndex < match.index) {
result += _.escape(str.substring(lastIndex, match.index));
}
// Wrap the match in a `mark` element to use the Bootstrap / Patternfly highlight styles.
result += "<mark>" + _.escape(match[0]) + "</mark>";
lastIndex = regex.lastIndex;
}
// Escape any remaining text and add it to the result.
if (lastIndex < str.length) {
result += _.escape(str.substring(lastIndex));
}
return result;
};
}]);
;'use strict';
angular.module('openshiftCommonUI')
.filter('orderObjectsByDate', ["toArrayFilter", function(toArrayFilter) { .filter('orderObjectsByDate', ["toArrayFilter", function(toArrayFilter) {
return function(items, reverse) { return function(items, reverse) {
items = toArrayFilter(items); items = toArrayFilter(items);
...@@ -3349,6 +3402,59 @@ angular.module('openshiftCommonServices') ...@@ -3349,6 +3402,59 @@ angular.module('openshiftCommonServices')
}); });
;'use strict'; ;'use strict';
angular.module("openshiftCommonServices")
.service("KeywordService", function(){
var generateKeywords = function(filterText) {
if (!filterText) {
return [];
}
var keywords = _.uniq(filterText.match(/\S+/g));
// Sort the longest keyword first.
keywords.sort(function(a, b){
return b.length - a.length;
});
// Convert the keyword to a case-insensitive regular expression for the filter.
return _.map(keywords, function(keyword) {
return new RegExp(_.escapeRegExp(keyword), "i");
});
};
var filterForKeywords = function(objects, filterFields, keywords) {
var filteredObjects = objects;
if (_.isEmpty(keywords)) {
return filteredObjects;
}
// Find resources that match all keywords.
angular.forEach(keywords, function(regex) {
var matchesKeyword = function(obj) {
var i;
for (i = 0; i < filterFields.length; i++) {
var value = _.get(obj, filterFields[i]);
if (value && regex.test(value)) {
return true;
}
}
return false;
};
filteredObjects = _.filter(filteredObjects, matchesKeyword);
});
return filteredObjects;
};
return {
filterForKeywords: filterForKeywords,
generateKeywords: generateKeywords
};
});
;'use strict';
angular.module('openshiftCommonServices') angular.module('openshiftCommonServices')
.provider('Logger', function() { .provider('Logger', function() {
this.$get = function() { this.$get = function() {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
'use strict';
angular.module('openshiftCommonUI')
.filter('highlightKeywords', function(KeywordService) {
// Returns HTML wrapping the matching words in a `mark` tag.
return function(str, keywords, caseSensitive) {
if (!str) {
return str;
}
if (_.isEmpty(keywords)) {
return _.escape(str);
}
// If passed a plain string, get the keywords from KeywordService.
if (_.isString(keywords)) {
keywords = KeywordService.generateKeywords(keywords);
}
// Combine the keywords into a single regex.
var source = _.map(keywords, function(keyword) {
if (_.isRegExp(keyword)) {
return keyword.source;
}
return _.escapeRegExp(keyword);
}).join('|');
// Search for matches.
var match;
var result = '';
var lastIndex = 0;
var flags = caseSensitive ? 'g' : 'ig';
var regex = new RegExp(source, flags);
while ((match = regex.exec(str)) !== null) {
// Escape any text between the end of the last match and the start of
// this match, and add it to the result.
if (lastIndex < match.index) {
result += _.escape(str.substring(lastIndex, match.index));
}
// Wrap the match in a `mark` element to use the Bootstrap / Patternfly highlight styles.
result += "<mark>" + _.escape(match[0]) + "</mark>";
lastIndex = regex.lastIndex;
}
// Escape any remaining text and add it to the result.
if (lastIndex < str.length) {
result += _.escape(str.substring(lastIndex));
}
return result;
};
});
'use strict';
angular.module("openshiftCommonServices")
.service("KeywordService", function(){
var generateKeywords = function(filterText) {
if (!filterText) {
return [];
}
var keywords = _.uniq(filterText.match(/\S+/g));
// Sort the longest keyword first.
keywords.sort(function(a, b){
return b.length - a.length;
});
// Convert the keyword to a case-insensitive regular expression for the filter.
return _.map(keywords, function(keyword) {
return new RegExp(_.escapeRegExp(keyword), "i");
});
};
var filterForKeywords = function(objects, filterFields, keywords) {
var filteredObjects = objects;
if (_.isEmpty(keywords)) {
return filteredObjects;
}
// Find resources that match all keywords.
angular.forEach(keywords, function(regex) {
var matchesKeyword = function(obj) {
var i;
for (i = 0; i < filterFields.length; i++) {
var value = _.get(obj, filterFields[i]);
if (value && regex.test(value)) {
return true;
}
}
return false;
};
filteredObjects = _.filter(filteredObjects, matchesKeyword);
});
return filteredObjects;
};
return {
filterForKeywords: filterForKeywords,
generateKeywords: generateKeywords
};
});
...@@ -27,3 +27,12 @@ ...@@ -27,3 +27,12 @@
} }
} }
// Mark is used for highlighting terms like search terms. See http://getbootstrap.com/css/#type-inline-text
mark {
// Remove the default padding as it makes letters jump for as-you-type updates.
padding: 0;
background-color: rgba(252, 248, 160, 0.5);
.ui-select-choices-row.active > span & {
background-color: rgba(252, 248, 160, 0.75);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment