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

Commit a42e364

Browse files
committed
feat(datepicker): let button group can be customise
1 parent 2784058 commit a42e364

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ lib-cov
88
*.gz
99
*.swp
1010
*.swo
11+
.idea
1112
.DS_Store
1213

1314
pids

src/datepicker/datepicker.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
335335
'ng-model': 'date',
336336
'ng-change': 'dateSelection()'
337337
});
338+
339+
if (angular.isDefined(attrs.buttonGroupTemplateUrl)) {
340+
popupEl.attr('button-group-template-url', attrs.buttonGroupTemplateUrl);
341+
}
338342
var datepickerEl = popupEl.find('datepicker');
339343
if (attrs.datepickerOptions) {
340344
datepickerEl.attr(angular.extend({}, originalScope.$eval(attrs.datepickerOptions)));
@@ -446,6 +450,9 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
446450
scope.today = function() {
447451
$setModelValue(originalScope, new Date());
448452
};
453+
scope.selectDate = function(date) {
454+
$setModelValue(originalScope, date ? new Date(date) : new Date());
455+
};
449456
scope.clear = function() {
450457
$setModelValue(originalScope, null);
451458
};
@@ -455,7 +462,7 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
455462
};
456463
}])
457464

458-
.directive('datepickerPopupWrap', function() {
465+
.directive('datepickerPopupWrap',["$compile", function($compile) {
459466
return {
460467
restrict:'E',
461468
replace: true,
@@ -466,6 +473,24 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
466473
event.preventDefault();
467474
event.stopPropagation();
468475
});
476+
var popupEl = angular.element('<datepicker-popup-button-group></datepicker-popup-button-group>');
477+
if (angular.isDefined(attrs.buttonGroupTemplateUrl)) {
478+
popupEl.attr('button-group-template-url', attrs.buttonGroupTemplateUrl);
479+
}
480+
element.find("li").eq(2).prepend($compile(popupEl)(scope));
481+
}
482+
};
483+
}])
484+
485+
.directive('datepickerPopupButtonGroup',["$parse", "$templateCache", "$http", "$compile",
486+
function($parse, $templateCache, $http, $compile) {
487+
return {
488+
restrict:'AE',
489+
link:function (scope, element, attrs) {
490+
var tplUrl = attrs.buttonGroupTemplateUrl || 'template/datepicker/popup-button-group.html';
491+
$http.get(tplUrl, {cache: $templateCache}).success(function(tplContent){
492+
element.replaceWith($compile(tplContent.trim())(scope));
493+
});
469494
}
470495
};
471-
});
496+
}]);

src/datepicker/test/datepicker.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ describe('datepicker directive', function () {
33
beforeEach(module('ui.bootstrap.datepicker'));
44
beforeEach(module('template/datepicker/datepicker.html'));
55
beforeEach(module('template/datepicker/popup.html'));
6+
beforeEach(module('template/datepicker/popup-button-group.html'));
67
beforeEach(inject(function(_$compile_, _$rootScope_) {
78
$compile = _$compile_;
89
$rootScope = _$rootScope_;
@@ -1236,6 +1237,8 @@ describe('datepicker directive', function () {
12361237

12371238
});
12381239

1240+
1241+
12391242
});
12401243
});
12411244

@@ -1259,4 +1262,28 @@ describe('datepicker directive with empty initial state', function () {
12591262
it('is shows rows with days', function() {
12601263
expect(element.find('tbody').find('tr').length).toBeGreaterThan(3);
12611264
});
1265+
});
1266+
1267+
describe('',function(){
1268+
var $rootScope, element, $httpBackend;
1269+
1270+
beforeEach(module('ui.bootstrap.datepicker'));
1271+
beforeEach(module('template/datepicker/popup.html'));
1272+
beforeEach(inject(function(_$compile_, _$rootScope_,_$httpBackend_) {
1273+
$compile = _$compile_;
1274+
$rootScope = _$rootScope_;
1275+
$httpBackend = _$httpBackend_;
1276+
}));
1277+
1278+
it("should send request to get customer template", function(){
1279+
var templateUrl = "test.html",
1280+
html = '<div><button type="button" ng-click="selectDate(\'2012-01-01\')">Next financial day</button></div>';
1281+
$httpBackend.whenGET(templateUrl).respond(200, html);
1282+
element = $compile('<datepicker-popup-wrap button-group-template-url="' + templateUrl + '"></datepicker-popup-wrap>')($rootScope);
1283+
$rootScope.$digest();
1284+
$httpBackend.flush();
1285+
1286+
expect(element.find('button').text()).toEqual("Next financial day");
1287+
});
1288+
12621289
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<span class="btn-group">
2+
<button type="button" class="btn btn-small btn-inverse" ng-click="today()">{{currentText}}</button>
3+
<button type="button" class="btn btn-small btn-info" ng-click="showWeeks = ! showWeeks"
4+
ng-class="{active: showWeeks}">{{toggleWeeksText}}</button>
5+
<button type="button" class="btn btn-small btn-danger" ng-click="clear()">{{clearText}}</button>
6+
</span>

template/datepicker/popup.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
<li ng-transclude></li>
33
<li class="divider"></li>
44
<li style="padding: 9px;">
5-
<span class="btn-group">
6-
<button type="button" class="btn btn-small btn-inverse" ng-click="today()">{{currentText}}</button>
7-
<button type="button" class="btn btn-small btn-info" ng-click="showWeeks = ! showWeeks" ng-class="{active: showWeeks}">{{toggleWeeksText}}</button>
8-
<button type="button" class="btn btn-small btn-danger" ng-click="clear()">{{clearText}}</button>
9-
</span>
105
<button type="button" class="btn btn-small btn-success pull-right" ng-click="isOpen = false">{{closeText}}</button>
116
</li>
127
</ul>

0 commit comments

Comments
 (0)