Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit d807466

Browse files
committed
More exceptions
1 parent 08f3ec5 commit d807466

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/select.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ angular.module('ui.select', [])
9797
* put as much logic in the controller (instead of the link functions) as possible so it can be easily tested.
9898
*/
9999
.controller('uiSelectCtrl',
100-
['$scope', '$element', '$timeout', 'RepeatParser', '$parse', '$q',
101-
function($scope, $element, $timeout, RepeatParser, $parse, $q) {
100+
['$scope', '$element', '$timeout', 'RepeatParser', '$parse', '$q', 'uiSelectMinErr',
101+
function($scope, $element, $timeout, RepeatParser, $parse, $q, uiSelectMinErr) {
102102

103103
var ctrl = this;
104104

@@ -113,6 +113,9 @@ angular.module('ui.select', [])
113113
ctrl.disabled = false;
114114

115115
ctrl.searchInput = $element.querySelectorAll('input.ui-select-search');
116+
if (ctrl.searchInput.length !== 1) {
117+
throw uiSelectMinErr('searchInput', "Expected 1 input.ui-select-search but got '{0}'.", ctrl.searchInput.length);
118+
}
116119

117120
// When the user clicks on ui-select, displays the dropdown list
118121
ctrl.activate = function() {
@@ -218,7 +221,10 @@ angular.module('ui.select', [])
218221
};
219222
}])
220223

221-
.directive('uiSelect', ['$document', 'uiSelectConfig', function($document, uiSelectConfig) {
224+
.directive('uiSelect',
225+
['$document', 'uiSelectConfig', 'uiSelectMinErr',
226+
function($document, uiSelectConfig, uiSelectMinErr) {
227+
222228
return {
223229
restrict: 'EA',
224230
templateUrl: function(tElement, tAttrs) {
@@ -254,6 +260,9 @@ angular.module('ui.select', [])
254260
function ensureHighlightVisible() {
255261
var container = element.querySelectorAll('.ui-select-choices-content');
256262
var rows = container.querySelectorAll('.ui-select-choices-row');
263+
if (rows.length < 1) {
264+
throw uiSelectMinErr('rows', "Expected multiple .ui-select-choices-row but got '{0}'.", rows.length);
265+
}
257266

258267
var highlighted = rows[$select.activeIndex];
259268
var posY = highlighted.offsetTop + highlighted.clientHeight - container[0].scrollTop;
@@ -312,16 +321,25 @@ angular.module('ui.select', [])
312321
var transcluded = angular.element('<div>').append(clone);
313322

314323
var transcludedMatch = transcluded.querySelectorAll('.ui-select-match');
324+
if (transcludedMatch.length !== 1) {
325+
throw uiSelectMinErr('transcluded', "Expected 1 .ui-select-match but got '{0}'.", transcludedMatch.length);
326+
}
315327
element.querySelectorAll('.ui-select-match').replaceWith(transcludedMatch);
316328

317329
var transcludedChoices = transcluded.querySelectorAll('.ui-select-choices');
330+
if (transcludedChoices.length !== 1) {
331+
throw uiSelectMinErr('transcluded', "Expected 1 .ui-select-choices but got '{0}'.", transcludedChoices.length);
332+
}
318333
element.querySelectorAll('.ui-select-choices').replaceWith(transcludedChoices);
319334
});
320335
}
321336
};
322337
}])
323338

324-
.directive('choices', ['uiSelectConfig', 'RepeatParser', function(uiSelectConfig, RepeatParser) {
339+
.directive('choices',
340+
['uiSelectConfig', 'RepeatParser', 'uiSelectMinErr',
341+
function(uiSelectConfig, RepeatParser, uiSelectMinErr) {
342+
325343
return {
326344
restrict: 'EA',
327345
require: '^uiSelect',
@@ -336,10 +354,14 @@ angular.module('ui.select', [])
336354
compile: function(tElement, tAttrs) {
337355
var repeat = RepeatParser.parse(tAttrs.repeat);
338356

339-
tElement.querySelectorAll('.ui-select-choices-row')
340-
.attr('ng-repeat', RepeatParser.getNgRepeatExpression(repeat.lhs, '$select.items', repeat.trackByExp))
341-
.attr('ng-mouseenter', '$select.activeIndex = $index')
342-
.attr('ng-click', '$select.select(' + repeat.lhs + ')');
357+
var rows = tElement.querySelectorAll('.ui-select-choices-row');
358+
if (rows.length !== 1) {
359+
throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row but got '{0}'.", rows.length);
360+
}
361+
362+
rows.attr('ng-repeat', RepeatParser.getNgRepeatExpression(repeat.lhs, '$select.items', repeat.trackByExp))
363+
.attr('ng-mouseenter', '$select.activeIndex = $index')
364+
.attr('ng-click', '$select.select(' + repeat.lhs + ')');
343365

344366
return function link(scope, element, attrs, $select) {
345367
$select.parseRepeatAttr(attrs.repeat);

0 commit comments

Comments
 (0)