Skip to content
This repository has been archived by the owner on Jun 19, 2018. It is now read-only.

Commit

Permalink
feat(customTemplates): allow a parent scope to be accessed in custom …
Browse files Browse the repository at this point in the history
…templates

Closes #415
  • Loading branch information
Matt Lewis committed Aug 10, 2016
1 parent 28354e6 commit c84e9ab
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ $templateCache.put('my-custom-template.html', 'Custom month view template here')
<mwl-calendar custom-template-urls="{calendarMonthView: 'my-custom-template.html'}"></mwl-calendar>
```

### template-scope
An object containing a set of variables that will be available in a custom template as `vm.templateScope`

## Configuring the calendar default config

You can easily customise the date formats and i18n strings used throughout the calendar by using the `calendarConfig` value. Please note that these example formats are those used by moment.js and these won't work if using angular as the date formatter. Example usage:
Expand Down
3 changes: 2 additions & 1 deletion src/directives/mwlCalendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ angular
dayViewStart: '@',
dayViewEnd: '@',
dayViewSplit: '@',
dayViewEventChunkSize: '@'
dayViewEventChunkSize: '@',
templateScope: '=?'
},
controller: 'MwlCalendarCtrl as vm',
bindToController: true
Expand Down
3 changes: 2 additions & 1 deletion src/directives/mwlCalendarDay.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ angular
editEventHtml: '=',
deleteEventHtml: '=',
customTemplateUrls: '=?',
cellModifier: '='
cellModifier: '=',
templateScope: '='
},
controller: 'MwlCalendarDayCtrl as vm',
bindToController: true
Expand Down
3 changes: 2 additions & 1 deletion src/directives/mwlCalendarHourList.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ angular
onDateRangeSelect: '=',
onEventTimesChanged: '=',
customTemplateUrls: '=?',
cellModifier: '='
cellModifier: '=',
templateScope: '='
},
bindToController: true
};
Expand Down
3 changes: 2 additions & 1 deletion src/directives/mwlCalendarMonth.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ angular
onTimespanClick: '=',
cellModifier: '=',
slideBoxDisabled: '=',
customTemplateUrls: '=?'
customTemplateUrls: '=?',
templateScope: '='
},
controller: 'MwlCalendarMonthCtrl as vm',
link: function(scope, element, attrs, calendarCtrl) {
Expand Down
3 changes: 2 additions & 1 deletion src/directives/mwlCalendarSlideBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ angular
deleteEventHtml: '=',
onDeleteEventClick: '=',
cell: '=',
customTemplateUrls: '=?'
customTemplateUrls: '=?',
templateScope: '='
},
bindToController: true
};
Expand Down
3 changes: 2 additions & 1 deletion src/directives/mwlCalendarWeek.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ angular
onTimespanClick: '=',
onDateRangeSelect: '=',
customTemplateUrls: '=?',
cellModifier: '='
cellModifier: '=',
templateScope: '='
},
controller: 'MwlCalendarWeekCtrl as vm',
link: function(scope, element, attrs, calendarCtrl) {
Expand Down
3 changes: 2 additions & 1 deletion src/directives/mwlCalendarYear.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ angular
onTimespanClick: '=',
cellModifier: '=',
slideBoxDisabled: '=',
customTemplateUrls: '=?'
customTemplateUrls: '=?',
templateScope: '='
},
controller: 'MwlCalendarYearCtrl as vm',
link: function(scope, element, attrs, calendarCtrl) {
Expand Down
4 changes: 4 additions & 0 deletions src/templates/calendar.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
cell-modifier="vm.cellModifier"
slide-box-disabled="vm.slideBoxDisabled"
custom-template-urls="vm.customTemplateUrls"
template-scope="vm.templateScope"
ng-switch-when="year">
</mwl-calendar-year>

Expand All @@ -39,6 +40,7 @@
cell-modifier="vm.cellModifier"
slide-box-disabled="vm.slideBoxDisabled"
custom-template-urls="vm.customTemplateUrls"
template-scope="vm.templateScope"
ng-switch-when="month">
</mwl-calendar-month>

Expand All @@ -55,6 +57,7 @@
on-date-range-select="vm.onDateRangeSelect"
custom-template-urls="vm.customTemplateUrls"
cell-modifier="vm.cellModifier"
template-scope="vm.templateScope"
ng-switch-when="week">
</mwl-calendar-week>

Expand All @@ -75,6 +78,7 @@
delete-event-html="vm.deleteEventHtml"
custom-template-urls="vm.customTemplateUrls"
cell-modifier="vm.cellModifier"
template-scope="vm.templateScope"
ng-switch-when="day">
</mwl-calendar-day>
</div>
1 change: 1 addition & 0 deletions src/templates/calendarDayView.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
on-event-times-changed="vm.onEventTimesChanged"
view-date="vm.viewDate"
custom-template-urls="vm.customTemplateUrls"
template-scope="vm.templateScope"
cell-modifier="vm.cellModifier">
</mwl-calendar-hour-list>

Expand Down
3 changes: 2 additions & 1 deletion src/templates/calendarMonthView.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
delete-event-html="vm.deleteEventHtml"
on-delete-event-click="vm.onDeleteEventClick"
cell="vm.view[vm.openDayIndex]"
custom-template-urls="vm.customTemplateUrls">
custom-template-urls="vm.customTemplateUrls"
template-scope="vm.templateScope">
</mwl-calendar-slide-box>

</div>
Expand Down
1 change: 1 addition & 0 deletions src/templates/calendarWeekView.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
on-date-range-select="vm.onDateRangeSelect"
custom-template-urls="vm.customTemplateUrls"
cell-modifier="vm.cellModifier"
template-scope="vm.templateScope"
ng-if="vm.showTimes">
</mwl-calendar-hour-list>

Expand Down
3 changes: 2 additions & 1 deletion src/templates/calendarYearView.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
delete-event-html="vm.deleteEventHtml"
on-delete-event-click="vm.onDeleteEventClick"
cell="vm.view[vm.openMonthIndex]"
custom-template-urls="vm.customTemplateUrls">
custom-template-urls="vm.customTemplateUrls"
template-scope="vm.templateScope">
</mwl-calendar-slide-box>

</div>
Expand Down
16 changes: 15 additions & 1 deletion test/unit/directives/mwlCalendarMonth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('mwlCalendarMonth directive', function() {
showModal,
calendarHelper,
calendarConfig,
$templateCache,
template =
'<mwl-calendar-month ' +
'events="events" ' +
Expand All @@ -25,6 +26,8 @@ describe('mwlCalendarMonth directive', function() {
'day-view-split="dayViewSplit || 30" ' +
'cell-template-url="{{ monthCellTemplateUrl }}" ' +
'cell-events-template-url="{{ monthCellEventsTemplateUrl }}" ' +
'template-scope="templateScope"' +
'custom-template-urls="customTemplateUrls"' +
'></mwl-calendar-month>';
var calendarDay = new Date(2015, 4, 1);

Expand Down Expand Up @@ -81,10 +84,11 @@ describe('mwlCalendarMonth directive', function() {

beforeEach(angular.mock.module('mwl.calendar'));

beforeEach(angular.mock.inject(function($compile, _$rootScope_, _calendarHelper_, _calendarConfig_) {
beforeEach(angular.mock.inject(function($compile, _$rootScope_, _calendarHelper_, _calendarConfig_, _$templateCache_) {
$rootScope = _$rootScope_;
calendarHelper = _calendarHelper_;
calendarConfig = _calendarConfig_;
$templateCache = _$templateCache_;
scope = $rootScope.$new();
prepareScope(scope);
element = angular.element(template);
Expand Down Expand Up @@ -282,4 +286,14 @@ describe('mwlCalendarMonth directive', function() {
expect(MwlCalendarCtrl.dateRangeSelect).to.be.undefined;
});

it('should pass in a scope object that is accessible from the custom template', function() {
scope.templateScope = {
foo: 'world'
};
$templateCache.put('customMonth.html', 'Hello {{ vm.templateScope.foo }}');
scope.customTemplateUrls = {calendarMonthView: 'customMonth.html'};
scope.$apply();
expect(element.text()).to.deep.equal('Hello world');
});

});

7 comments on commit c84e9ab

@h44z
Copy link

@h44z h44z commented on c84e9ab Aug 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, a short question to this enhancement:

should it be enough to specify the calendar as follows:

<body ng-app="timeKeeper" ng-controller="TimeController as vmTimeCtl">
<mwl-calendar
      events="vmTimeCtl.events"
      view="vmTimeCtl.calendarView"
      view-title="vmTimeCtl.calendarTitle"
      view-date="vmTimeCtl.viewDate"
      on-event-click="vmTimeCtl.eventClicked(calendarEvent)"
      on-event-times-changed="vmTimeCtl.eventTimesChanged(calendarEvent); calendarEvent.startsAt = calendarNewEventStart; calendarEvent.endsAt = calendarNewEventEnd"
      cell-is-open="vmTimeCtl.isCellOpen"
      day-view-start="06:00"
      day-view-end="22:59"
      day-view-split="30"
      cell-modifier="vmTimeCtl.modifyCell(calendarCell)"
      template-scope="{parentCtl: vmTimeCtl}">
</mwl-calendar>
</body>

so that I can access the parent controller via vm.templateScope.parentCtl ?

I have specified the template like this:

calendarConfig.templates.calendarSlideBox = 'tpl/calendarSlideBox.html';

Thanks for your great work!

@mattlewis92
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup that's correct! 😄

@h44z
Copy link

@h44z h44z commented on c84e9ab Aug 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, tested this, but neither vm.templateScope.parentCtl nor vm.templateScope are set in the template... using the latest master branch, implemented as stated above.

@mattlewis92
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you re-built the dist files?

@h44z
Copy link

@h44z h44z commented on c84e9ab Aug 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah! I forgot to do this...

Works fine after rebuilding the dist files!
Thanks a lot! :)

@mattlewis92
Copy link
Owner

@mattlewis92 mattlewis92 commented on c84e9ab Aug 11, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@albertomarley94
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for make this amazing library, i have a problem sending the parent controller to my custom template, two of them are in the same document, could you help me please?

 <div ng-app="mwl.calendar.docs" ng-controller="CalendarSlots as vm"  >
 <script id="customMonthCell.html" type="text/ng-template">
            <div ng-controller="vm">
                        Test
            </div>

</script>
<mwl-calendar
            events="vm.events"
            view="vm.calendarView"
            view-title="vm.calendarTitle"
            view-date="vm.viewDate"
            custom-template-urls="{calendarMonthCell: 'customMonthCell.html'}"
            template-scope="{parentCtl: vm}"
            cell-is-open="vm.cellIsOpen"
            day-view-start="06:00"
            day-view-end="22:59"
            day-view-split="30"
            cell-modifier="vm.modifyCell(calendarCell)"
            cell-auto-open-disabled="true"
            on-event-click="vm.toggleModal(calendarEvent)">
        </mwl-calendar>

</div>

Please sign in to comment.