Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

De-duplicate entries over labels #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@ app.controller('MainCtrl', function($scope, $timeout) {
sub: [
{
icon: '<img src="https://cdn1.iconfinder.com/data/icons/google_jfk_icons_by_carlosjj/32/chrome.png" />',
name: "Chrome"
name: "Chrome",
engine: "Blink"
},
{
id: 2,
icon: '<img src="https://cdn2.iconfinder.com/data/icons/humano2/32x32/apps/firefox-icon.png" />',
name: "Firefox"
name: "Firefox",
engine: "Gecko"
},
{
icon: '<img src="https://cdn1.iconfinder.com/data/icons/google_jfk_icons_by_carlosjj/32/chrome.png" />',
name: "Chrome"
},
{
icon: '<img src="https://cdn2.iconfinder.com/data/icons/new_google_product_icons_by_carlosjj-dwke/32/chromium.png" />',
name: "Chromium"
name: "Chromium",
engine: "Blink"
}
]
},
Expand All @@ -27,11 +34,18 @@ app.controller('MainCtrl', function($scope, $timeout) {
{
icon: '<img src="https://cdn0.iconfinder.com/data/icons/fatcow/32x32/safari_browser.png" />',
name: "Safari",
engine: "Blink",
checked: true
},
{
icon: '<img src="https://cdn1.iconfinder.com/data/icons/google_jfk_icons_by_carlosjj/32/chrome.png" />',
name: "Chrome",
engine: "Blink"
},
{
icon: '<img src="https://cdn1.iconfinder.com/data/icons/logotypes/32/opera-32.png" />',
name: "Opera",
engine: "Blink",
checked: true
}
]
Expand Down
8 changes: 4 additions & 4 deletions demo/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<meta charset="utf-8" />
<link rel="stylesheet" href="/src/angular-multi-select.css" />

<script src="/node_modules/angular/angular.js"></script>
<script src="/node_modules/angular-filter/dist/angular-filter.js"></script>
<script src="/bower_components/angular/angular.js"></script>
<script src="/bower_components/angular-filter/dist/angular-filter.js"></script>
<script src="/src/angular-multi-select.js"></script>

<script src="app.js"></script>
Expand Down Expand Up @@ -37,11 +37,11 @@
group-property="sub"
input-model="modernBrowsers"
output-model="outputBrowsers"
item-label="{{ icon }} {{ name }}"
item-label="<[ icon ]> <[ name ]>"
selection-mode="multi"
tick-property="checked"
helper-elements="all none reset filter"
button-label="{{ icon }} {{ name }}"
button-label="<[ icon ]> <[ name ]>"
button-label-separator='[", ","!?"]'
button-template="angular-multi-select-btn-data.htm"
>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"devDependencies": {
"bower": "latest",
"grunt": "^0.4.5",
"grunt-contrib-uglify": ">=0.5.0",
"grunt-contrib-jshint": "0.11.2",
"grunt-contrib-cssmin": ">=0.10.0",
Expand Down
56 changes: 36 additions & 20 deletions src/angular-multi-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,15 @@ angular_multi_select.directive('angularMultiSelect',
* @returns {*}
* @private
*/
$scope._walkedPath = [];
$scope._walk = function(obj, key, fn) {
var _idx;
var fnd = (function(d, o, n) { return fn(o, n, d); }).bind(this, {});
if(angular.isArray(obj)) {
var _objs = [];

for(_idx in obj) {
var _tmp_obj = $scope._walk(obj[_idx], key, fn);
var _tmp_obj = $scope._walk(obj[_idx], key, fnd);
if (_tmp_obj !== null) {
_objs.push(_tmp_obj);
}
Expand All @@ -310,17 +312,19 @@ angular_multi_select.directive('angularMultiSelect',
return _objs.length > 0 ? _objs : null;
} else if(angular.isObject(obj)) {
fn = fn || function(){ return true; };
var should_be_returned = fn(obj);
var should_be_returned = fn(obj, $scope._walkedPath.join('>'));

if (obj.hasOwnProperty(key) && angular.isArray(obj[key]) ) {
var sub = [];
$scope._walkedPath.push($scope._interpolatedItemLabel(obj));

for(_idx in obj[key]) {
var new_obj = $scope._walk(obj[key][_idx], key, fn);
var new_obj = $scope._walk(obj[key][_idx], key, fnd);
if (new_obj !== null) {
sub.push(new_obj);
}
}
$scope._walkedPath.pop();

if(sub.length !== obj[key].length){
obj[key] = sub;
Expand All @@ -339,22 +343,18 @@ angular_multi_select.directive('angularMultiSelect',
* @private
*/
$scope._syncModels = function(dst, src) {
/*
* We can't use $scope.merge() here as that will wipe
* the elements that are in the src but not in the dst model.
* We need to iterate over the src and dst items at the same
* time and apply changes only when both items exist and the
* tick property is different.
*/
$scope._walk(src, attrs.groupProperty, function(item) {
$scope._walk(dst, attrs.groupProperty, function(_item) {
if(_item[attrs.idProperty] === item[attrs.idProperty]) {
//Don't use extend here as it's really expensive and because
//the only thing that can change in an item is it's tick state.
_item[attrs.tickProperty] = item[attrs.tickProperty];
}
return true;
});
var tree = {};
$scope._walk(src, attrs.groupProperty, function (o, e) {
if (typeof tree[e] === "undefined") tree[e] = {};
var label = $scope._interpolatedItemLabel(o);
tree[e][label] = o[attrs.tickProperty];
return true;
});
$scope._walk(dst, attrs.groupProperty, function (o, e) {
var label = $scope._interpolatedItemLabel(o);
if (tree[e] && o[attrs.tickProperty] !== tree[e][label]) {
o[attrs.tickProperty] = tree[e][label];
}
return true;
});
};
Expand Down Expand Up @@ -768,6 +768,21 @@ angular_multi_select.directive('angularMultiSelect',
}, 0);
};

/**
* De-duplicates entries in a group. Returns true if this object is the first in this group.
* @param {Object} obj
* @param {Array} path
* @param {Object} dupes
* @returns {boolean}
* @private
*/
$scope._dedup = function(obj, path, dupes) {
if (typeof dupes === "undefined") return true;
var label = $scope._interpolatedItemLabel(obj);
if (dupes[label]) return false;
else return (dupes[label] = true);
};

/**
* Returns true if [attrs.searchProperty} matches the search input field (latinized, fuzzy match);
* @param {Object} obj
Expand Down Expand Up @@ -877,6 +892,7 @@ angular_multi_select.directive('angularMultiSelect',
*/
$scope.fillFilteredModel = function() {
$scope.filteredModel = angular.copy($scope._shadowModel);
$scope.filteredModel = $scope._walk($scope.filteredModel, attrs.groupProperty, $scope._dedup);
$scope.filteredModel = $scope._walk($scope.filteredModel, attrs.groupProperty, $scope._filter);
};

Expand Down Expand Up @@ -915,7 +931,7 @@ angular_multi_select.directive('angularMultiSelect',
});
_tmp = _tmp === null ? [] : _tmp;

var _new_shadowOutputModel = angular.copy(_tmp);
var _new_shadowOutputModel = $scope._walk(angular.copy(_tmp), attrs.groupProperty, $scope._dedup);
if(!$scope.deepCompare(angular.copy($scope._shadowOutputModel), _new_shadowOutputModel)) {
$scope._shadowOutputModel = _new_shadowOutputModel;
}
Expand Down