Skip to content

Commit 0c107c1

Browse files
author
0xAX
committed
dropdown added
1 parent 2211299 commit 0c107c1

File tree

4 files changed

+110
-9
lines changed

4 files changed

+110
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Directives
99
* [accordion](https://github.com/angularify/angular-semantic-ui/tree/master/src/accordion)
1010
* [checkbox](https://github.com/angularify/angular-semantic-ui/tree/master/src/checkbox)
1111
* [dimmer](https://github.com/angularify/angular-semantic-ui/tree/master/src/dimmer)
12+
* [dropdown](https://github.com/angularify/angular-semantic-ui/tree/master/src/dropdown)
1213

1314
Building
1415
----------------------

src/dropdown/docs/controllers.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
var dropdownApp = angular.module('dropdownApp', ['angularify.semantic.dropdown']);
22

33
function RootCtrl ($scope) {
4-
$scope.hello = function(){
5-
alert('hello');
6-
}
4+
$scope.dropdown_model = 'item3';
75
}
86

src/dropdown/dropdown.js

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@
33
angular.module('angularify.semantic.dropdown', [])
44

55
.controller('DropDownController', ['$scope', function($scope){
6+
$scope.items = [];
7+
8+
this.add_item = function(scope) {
9+
$scope.items.push(scope);
10+
11+
scope.$on('$destroy', function (event) {
12+
this.remove_accordion(scope);
13+
});
14+
15+
return $scope.items;
16+
}
17+
18+
this.remove_item = function(scope) {
19+
var index = $scope.items.indexOf(scope);
20+
if ( index !== -1 )
21+
$scope.items.splice(index, 1);
22+
}
23+
24+
this.update_title = function(title){
25+
var i = 0;
26+
for (i in $scope.items){
27+
$scope.items[i].title = title;
28+
}
29+
}
630

731
}])
832

@@ -14,24 +38,48 @@ angular.module('angularify.semantic.dropdown', [])
1438
controller: 'DropDownController',
1539
scope: {
1640
title : "@",
17-
open : "@"
41+
open : "@",
42+
model : '=ngModel'
1843
},
1944
template: "<div class=\"{{dropdown_class}}\">" +
2045
"<div class=\"default text\">{{title}}</div>" +
2146
"<i class=\"dropdown icon\"></i>" +
2247
"<div class=\"menu\" ng-transclude>" +
2348
"</div>" +
2449
"</div>",
25-
link: function(scope, element, attrs){
50+
link: function(scope, element, attrs, DropDownController){
2651
scope.dropdown_class = 'ui selection dropdown';
27-
52+
2853
if (scope.open == "true"){
2954
scope.open = true;
3055
scope.dropdown_class = scope.dropdown_class + ' active visible';
3156
}
3257
else
3358
scope.open = false;
3459

60+
DropDownController.add_item(scope);
61+
62+
//
63+
// Watch for title changing
64+
//
65+
scope.$watch('title', function (val) {
66+
if (val == undefined)
67+
return;
68+
69+
if (val == scope.title)
70+
return;
71+
72+
scope.model = val;
73+
});
74+
75+
//
76+
// Watch for ng-model changing
77+
//
78+
scope.$watch('model', function (val) {
79+
// update title
80+
DropDownController.update_title(val)
81+
});
82+
3583
//
3684
// Click handler
3785
//
@@ -54,9 +102,17 @@ angular.module('angularify.semantic.dropdown', [])
54102
replace: true,
55103
transclude: true,
56104
require: '^dropdown',
57-
template: '<div class="item" ng-transclude></div>',
58-
link: function(scope, element, attrs){
59-
105+
scope : {
106+
},
107+
template: '<div class="item" ng-transclude>{{title}}</div>',
108+
link: function(scope, element, attrs, DropDownController){
109+
var title = element.children()[0].innerHTML;
110+
//
111+
// Menu item click handler
112+
//
113+
element.bind('click', function(){
114+
DropDownController.update_title(title)
115+
});
60116
}
61117
}
62118
});

src/dropdown/test/dropdown.spec.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
describe('dropdown', function () {
2+
var $scope;
3+
4+
beforeEach(module('angularify.semantic.dropdown'));
5+
6+
beforeEach(inject(function ($rootScope) {
7+
$scope = $rootScope;
8+
}));
9+
10+
describe('controller', function () {
11+
var ctrl, $element, $attrs;
12+
13+
beforeEach(inject(function($controller) {
14+
$attrs = {}; $element = {};
15+
ctrl = $controller('DropDownController', { $scope: $scope });
16+
}));
17+
18+
describe('add_item', function() {
19+
it("DropDownController add_item test", function(){
20+
var acc1, acc2;
21+
ctrl.add_item(acc1 = $scope.$new());
22+
ctrl.add_item(acc2 = $scope.$new());
23+
expect($scope.items.length).toBe(2);
24+
});
25+
});
26+
27+
describe('remove_item', function(){
28+
it("DropDownController remove_item test", function(){
29+
var acc1, acc2;
30+
ctrl.add_item(acc1 = $scope.$new());
31+
ctrl.add_item(acc2 = $scope.$new());
32+
ctrl.remove_item(acc2);
33+
expect($scope.items.length).toBe(1);
34+
})
35+
});
36+
37+
describe('update_title', function () {
38+
it("DropDownController update_title test", function () {
39+
var scope = $scope.$new();
40+
ctrl.add_item(scope);
41+
ctrl.update_title('title');
42+
expect(scope.title).toBe('title');
43+
})
44+
});
45+
});
46+
});

0 commit comments

Comments
 (0)