forked from patternfly/angular-patternfly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.js
More file actions
106 lines (89 loc) · 3.51 KB
/
Copy pathselect.js
File metadata and controls
106 lines (89 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* @ngdoc directive
* @name patternfly.select:pfSelect
* @element select
*
* @param {string} ngModel Model binding using the {@link https://docs.angularjs.org/api/ng/type/ngModel.NgModelController/ NgModelController} is mandatory.
* @param {string=} ngOptions The `{@link https://docs.angularjs.org/api/ng/directive/select/ ngOptions}` attribute can be used to dynamically generate a list of `<option>`
* elements for the `<select>` element.
*
* @description
* An AngularJS wrapper for the {@link http://silviomoreto.github.io/bootstrap-select/ Bootstrap-select} jQuery plugin which is used
* as a default select decorator in {@link https://www.patternfly.org/widgets/#bootstrap-select Patternfly}.
*
* @example
<example module="patternfly.select">
<file name="index.html">
<div ng-controller="SelectDemoCtrl">
<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="pet">Preferred pet:</label>
<div class="col-sm-10">
<select pf-select ng-model="pet" id="pet" ng-options="o as o for o in pets"></select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="fruit">Preferred fruit:</label>
<div class="col-sm-10">
<select pf-select ng-model="fruit" id="fruit">
<option value="orange">Orange</option>
<option value="apple" ng-selected="true" selected>Apple</option>
<option value="banana">Banana</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="drink">Preferred drink:</label>
<div class="col-sm-10">
<select pf-select="{ noneSelectedText: 'None' }" ng-model="drink" id="drink" ng-options="o as o for o in drinks">
<option value="">No drink selected</option>
</select>
</div>
</div>
</form>
<p>Your preferred pet is {{pet}}.</p>
</div>
</file>
<file name="script.js">
angular.module( 'patternfly.select' ).controller( 'SelectDemoCtrl', function( $scope ) {
$scope.drinks = ['tea', 'coffee', 'water'];
$scope.pets = ['Dog', 'Cat', 'Chicken'];
$scope.pet = $scope.pets[0];
$scope.fruit = 'orange';
});
</file>
</example>
*/
angular.module('patternfly.select', []).directive('pfSelect', function ($timeout) {
'use strict';
return {
restrict: 'A',
require: '?ngModel',
scope: {
selectPickerOptions: '=pfSelect'
},
link: function (scope, element, attrs, ngModel) {
var optionCollectionList, optionCollectionExpr, optionCollection, $render = ngModel.$render;
var selectpickerRefresh = function (argument) {
scope.$applyAsync(function () {
element.selectpicker('refresh');
});
};
element.selectpicker(scope.selectPickerOptions);
ngModel.$render = function () {
$render.apply(this, arguments);
selectpickerRefresh();
};
if (attrs.ngOptions) {
optionCollectionList = attrs.ngOptions.split('in ');
optionCollectionExpr = optionCollectionList[optionCollectionList.length - 1].split(/track by|\|/);
optionCollection = optionCollectionExpr[0];
scope.$parent.$watchCollection(optionCollection, selectpickerRefresh);
}
if (attrs.ngModel) {
scope.$parent.$watch(attrs.ngModel, selectpickerRefresh);
}
attrs.$observe('disabled', selectpickerRefresh);
}
};
});