Skip to content

Commit 500112b

Browse files
committed
V 0.3.0
- Bug Fix: Checking the existence of array of dropdown list. - Added “multiSelect” on or off feature. Also, auto CSS fix based on “multiSelect” property.
1 parent 2dbc74e commit 500112b

File tree

3 files changed

+75
-27
lines changed

3 files changed

+75
-27
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ $scope.options = [ {
5757
```
5858

5959
<h4>dropdown-trackby</h4>
60-
<p>Initially, dropdown items are tracked by <code>Id</code> automagically, if the <code>dropdown-trackby</code> attribute is not set.</p>
60+
<p>Initially, dropdown items are tracked by <code>Id</code> automagically (considering "Id" property is present in all objects in an array), if the <code>dropdown-trackby</code> attribute is not set.</p>
6161
<p>If the option objects does not have <code>Id</code> property, then custom tracking could be set by providing any of the property of an object from the options data.</p>
6262
Controller:
6363
```javascript
@@ -76,10 +76,10 @@ var options = [ {
7676
```html
7777
<dropdown-multiselect dropdown-options="options" dropdown-trackby="Name"></dropdown-multiselect>
7878
```
79-
<p>It is always better to provide <code>dropdown-trackby</code> attribute for correct tracking.</p>
79+
<p><em>NOTE: </em> It is always better to provide <code>dropdown-trackby</code> attribute for correct tracking, when <code>dropdown-options</code> is being used.</p>
8080

8181
<h4>dropdown-disable</h4>
82-
<p>Dropdown could be disabled by providing boolean value to <code>dropdown-disable</code> attribute.</p>
82+
<p>Dropdown could be disabled by directly providing boolean value to <code>dropdown-disable</code> attribute.</p>
8383
HTML:
8484
```html
8585
<dropdown-multiselect dropdown-options="options" dropdown-disable="true"></dropdown-multiselect>
@@ -112,8 +112,8 @@ Controller:
112112
```
113113

114114
<h3>Set through config in Controller:</h3>
115-
Configure the options from the controller to set <code>dropdown-config</code>.
116-
<p><em>NOTE: </em> When <code>dropdown-config</code> is being used, it will overwrite <code>dropdown-options</code> and <code>dropdown-trackby</code> attribute, if in use.</p>
115+
Configure the options from the controller to set <code>dropdown-config</code> attribute.
116+
<p><em>NOTE: </em> When <code>dropdown-config</code> is being used, it will overwrite <code>dropdown-options</code> and <code>dropdown-trackby</code> attribute, if in use. Therefore, it's better to use just one at a time.</p>
117117
<p>Available <code>config</code> options:
118118
<pre>
119119
options,
@@ -134,6 +134,7 @@ Configure the options from the controller to set <code>dropdown-config</code>.
134134
<h6>displayBadge:</h6> <p>Badge on the dropdown button that displays the total number of selected items from the dropdown list. Default visibility is <code>true</code>, but could be set to <code>false</code>.</p>
135135
<h6>height:</h6> <p>Height of the scrollable item list in a dropdown-box, in pixel. Default height is set to <code>200px</code>.</p>
136136
<h6>filter:</h6> <p>Filter/search items from the dropdown list. Default visibility is <code>false</code>, but could be set to <code>true</code>.</p>
137+
<h6>multiSelect:</h6> <p>Turn multi-select list items "on" or "off". Default is <code>true</code>, but could be turned "off" by setting <code>false</code>.</p>
137138

138139
Controller:
139140
```javascript
@@ -159,7 +160,8 @@ $scope.config = {
159160
icon: 'glyphicon glyphicon-heart',
160161
displayBadge: true,
161162
height: '200px',
162-
filter: true
163+
filter: true,
164+
multiSelect: false
163165
};
164166
```
165167

@@ -172,5 +174,3 @@ HTML:
172174
![Alt text](https://cdn1.iconfinder.com/data/icons/google_jfk_icons_by_carlosjj/32/chrome.png "Chrome")
173175
![Alt text](https://cdn1.iconfinder.com/data/icons/humano2/32x32/apps/firefox-icon.png "Firefox")
174176
![Alt text](https://cdn1.iconfinder.com/data/icons/fatcow/32x32/safari_browser.png "Safari")
175-
176-

src/angular-dropdownMultiselect.css

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
margin-right: -6px;
1717
}
1818
.dropdown-menu div {
19-
width: 93%;
19+
width: 100%;
2020
z-index: 1;
2121
}
2222
.dropdown-static {
@@ -72,11 +72,13 @@
7272
position: relative;
7373
font-size: 18px;
7474
padding-top: 0px;
75+
padding-left: 20px;
76+
padding-right: 20px;
7577
margin: 3px auto 0px;
7678
}
7779
.filter-parent label {
7880
position: absolute;
79-
left: 7px;
81+
left: 27px;
8082
top: 5px;
8183
color: #C3BFBF;
8284
}
@@ -87,4 +89,4 @@
8789
padding: 6px 26px;
8890
border-radius: 23px;
8991
height: 30px;
90-
}
92+
}

src/angular-dropdownMultiselect.js

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ angular.module( 'dropdown-multiselect', [] )
2525
'<li>' +
2626
'<div>' +
2727
'<ul class="dropdown-static">' +
28-
'<li><a ng-click="selectAll()"><i class="glyphicon glyphicon-ok"></i> Select All</a></li>' +
29-
'<li><a ng-click="unSelectAll()"><i class="glyphicon glyphicon-remove"></i> Unselect All</a></li>' +
28+
'<li ng-if="multiSelect"><a ng-click="selectAll()"><i class="glyphicon glyphicon-ok"></i> Select All</a></li>' +
29+
'<li ng-if="multiSelect"><a ng-click="unSelectAll()"><i class="glyphicon glyphicon-remove"></i> Unselect All</a></li>' +
3030
'<div class="filter-parent" id="search" ng-if="isFilterVisible">' +
3131
'<label for="filter-by"><i class="glyphicon glyphicon-search"></i></label>' +
3232
'<div>' +
@@ -53,12 +53,13 @@ angular.module( 'dropdown-multiselect', [] )
5353
disableDropdown: '=dropdownDisable', // boolean
5454
dropdownType: '=', // view type
5555
model: '=?', // model to bind with view & bind data back to the controller
56-
options: '=dropdownOptions', // object for repeater
56+
dropdownOptions: '=dropdownOptions', // object for repeater
5757
notifyParent: '&dropdownSelected' // notifier
5858
},
5959
controller: function ( $scope ) {
6060
var model = [],
6161
badgeVisibility = true, // default badge visibility
62+
multiSelect = true, // default multiSelect is "ON"
6263
filterVisibility = false, // drfault filter visibility
6364
divider = '-', // default divider sign
6465
icon = 'glyphicon glyphicon-ok', // default icon
@@ -74,11 +75,30 @@ angular.module( 'dropdown-multiselect', [] )
7475

7576
$scope.divider = divider;
7677

78+
$scope.multiSelect = multiSelect;
79+
7780
$scope.model = model;
7881

7982
try {
83+
if ( angular.isUndefined( $scope.dropdownOptions ) && angular.isUndefined( $scope.config ) ) {
84+
throw 'Error: Missing array for dropdown list. Consider providing array through [dropdown-options] or [dropdown-config] attribute in the directive tag.';
85+
}
86+
87+
// check if dropdownOptions is defined or not
88+
if ( angular.isDefined( $scope.dropdownOptions ) ) {
89+
// setting dropdown list items
90+
$scope.options = $scope.dropdownOptions;
91+
92+
// auto assigning left display value and right display value
93+
autoLeftKeyRightKey();
94+
}
95+
96+
/*===================================================================*/
97+
/* NOTE: WHEN config AND dropdownOptions ARE BOTH USED TOGETHER, */
98+
/* config WILL OVERWRITE dropdownOptions. */
99+
/*===================================================================*/
80100

81-
//check if config is defined or not
101+
// check if config is defined or not
82102
if ( angular.isDefined( $scope.config ) ) {
83103
// if 'options' property exists, set the dropdown list items
84104
if ( $scope.config.hasOwnProperty( 'options' ) ) {
@@ -90,14 +110,8 @@ angular.module( 'dropdown-multiselect', [] )
90110
$scope.leftKey = $scope.config.displayBy[ 0 ];
91111
$scope.rightKey = $scope.config.displayBy[ 1 ];
92112
} else {
93-
// grab the first two property and set it automatically as leftKey and rightKey
94-
var optionsProperties = [];
95-
for ( var prop in $scope.options[ 0 ] ) {
96-
optionsProperties.push( prop );
97-
}
98-
99-
$scope.leftKey = optionsProperties[ 0 ];
100-
$scope.rightKey = optionsProperties[ 1 ];
113+
// auto assigning left display value and right display value
114+
autoLeftKeyRightKey();
101115
}
102116

103117
if ( $scope.config.hasOwnProperty( 'divider' ) ) {
@@ -120,6 +134,13 @@ angular.module( 'dropdown-multiselect', [] )
120134
$scope.isBadgeVisible = badgeVisibility;
121135
}
122136

137+
if ( $scope.config.hasOwnProperty( 'multiSelect' ) ) {
138+
multiSelect = ( angular.equals( true, $scope.config.multiSelect ) ) ? true : false;
139+
140+
$scope.multiSelect = multiSelect;
141+
}
142+
143+
123144
// if 'trackBy' property exists, set the main key to track the dropdown list
124145
//(config file overrites the dropdown-trackby attributes)
125146
if ( $scope.config.hasOwnProperty( 'trackBy' ) ) {
@@ -217,11 +238,19 @@ angular.module( 'dropdown-multiselect', [] )
217238
chainId = this.option.ChainId,
218239
obj = {};
219240

241+
// when multi select feature turned off through "config"
242+
if ( multiSelect === false ) {
243+
if ( isDuplicate( id, model ) ) return false;
244+
model[ 0 ] = this.option;
245+
return false;
246+
}
247+
220248
var duplicate = isDuplicate( id, model );
221249

222250
if ( !duplicate ) {
223251
obj[ $scope.trackByKey ] = id;
224252

253+
// add to array
225254
model.push( this.option );
226255
}
227256

@@ -248,6 +277,16 @@ angular.module( 'dropdown-multiselect', [] )
248277
console.error( e );
249278
}
250279

280+
function autoLeftKeyRightKey() {
281+
// grab the first two property and set it automatically as leftKey and rightKey
282+
var optionsProperties = [];
283+
for ( var prop in $scope.options[ 0 ] ) {
284+
optionsProperties.push( prop );
285+
}
286+
$scope.leftKey = optionsProperties[ 0 ]; // setthing by 1st property
287+
$scope.rightKey = optionsProperties[ 1 ]; // setting by 2nd property
288+
}
289+
251290
function keyIsUndefined( key ) {
252291
// if dropdownTrackby is not defined
253292
if ( angular.isUndefined( key ) ) {
@@ -322,16 +361,23 @@ angular.module( 'dropdown-multiselect', [] )
322361

323362
}, true );
324363

325-
if ( angular.isDefined( scope.config.height ) ) {
326-
var custom_height = scope.config.height,
327-
dropdownListBox = angular.element( document.querySelector( '.dropdown-scrollable' ) );
364+
var dropdownListBox = angular.element( document.querySelector( '.dropdown-scrollable' ) );
365+
366+
if ( angular.isDefined( scope.config ) && angular.isDefined( scope.config.height ) ) {
367+
var custom_height = scope.config.height;
328368

329-
dropdownListBox.css( 'max-height', custom_height )
369+
dropdownListBox.css( 'max-height', custom_height );
330370

331371
} else {
332372
scope.defaultHeight = true;
333373
}
334374

375+
if ( scope.multiSelect === false && scope.isFilterVisible === true ) {
376+
dropdownListBox.css( 'margin-top', '50px' );
377+
} else if ( scope.multiSelect === false && scope.isFilterVisible === false ) {
378+
dropdownListBox.css( 'margin-top', '10px' );
379+
}
380+
335381
var transcluded = element.find( 'span' ).contents();
336382

337383
scope.hasText = ( transcluded.length > 0 );

0 commit comments

Comments
 (0)