Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 49c96c1

Browse files
KarenParkerErinCoughlan
authored andcommitted
feat(panel): Inject mdPanelRef when instantiating the panel controller.
1 parent cfb3da1 commit 49c96c1

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

src/components/panel/demoBasicUsage/panel.tmpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ <h2>Surprise!</h2>
1919
<md-button flex class="md-primary">
2020
Nothing
2121
</md-button>
22-
<md-button md-autofocus flex class="md-primary" ng-click="ctrl.closeFn()">
22+
<md-button md-autofocus flex class="md-primary" ng-click="ctrl.closeDialog()">
2323
Close
2424
</md-button>
2525
</div>

src/components/panel/demoBasicUsage/script.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
angular.module('panelDemo', ['ngMaterial'])
22
.controller('BasicDemoCtrl', BasicDemoCtrl)
3-
.controller('DialogCtrl', DialogCtrl);
3+
.controller('PanelDialogCtrl', PanelDialogCtrl);
44

55

66
function BasicDemoCtrl($mdPanel) {
@@ -16,12 +16,9 @@ BasicDemoCtrl.prototype.showDialog = function() {
1616

1717
var config = {
1818
attachTo: angular.element(document.querySelector('.demo-md-panel')),
19-
controller: DialogCtrl,
19+
controller: PanelDialogCtrl,
2020
controllerAs: 'ctrl',
2121
templateUrl: 'panel.tmpl.html',
22-
locals: {
23-
closeFn: angular.bind(this, this.closeDialog)
24-
},
2522
panelClass: 'demo-dialog-example',
2623
position: position,
2724
trapFocus: true,
@@ -32,10 +29,12 @@ BasicDemoCtrl.prototype.showDialog = function() {
3229
};
3330

3431

35-
BasicDemoCtrl.prototype.closeDialog = function() {
36-
this._panelRef && this._panelRef.close();
37-
};
32+
// Necessary to pass locals to the dialog template.
33+
function PanelDialogCtrl(mdPanelRef) {
34+
this._mdPanelRef = mdPanelRef;
35+
}
3836

3937

40-
// Necessary to pass locals to the dialog template.
41-
function DialogCtrl() { }
38+
PanelDialogCtrl.prototype.closeDialog = function() {
39+
this._mdPanelRef && this._mdPanelRef.close();
40+
};

src/components/panel/panel.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,10 @@ MdPanelRef.prototype.focusOnOpen = function() {
998998
MdPanelRef.prototype._createPanel = function() {
999999
var self = this;
10001000
return this._$q(function(resolve, reject) {
1001+
if (!self._config.locals) {
1002+
self._config.locals = {};
1003+
}
1004+
self._config.locals.mdPanelRef = self;
10011005
self._$mdCompiler.compile(self._config)
10021006
.then(function(compileData) {
10031007
self._panelContainer = compileData.link(self._config['scope']);

src/components/panel/panel.spec.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('$mdPanel', function() {
99
var FULLSCREEN_CLASS = '_md-panel-fullscreen';
1010
var DEFAULT_TEMPLATE = '<div>Hello World!</div>';
1111
var DEFAULT_CONFIG = { template: DEFAULT_TEMPLATE };
12+
var PANEL_ID_PREFIX = 'panel_';
1213

1314
/**
1415
* @param {!angular.$injector} $injector
@@ -597,12 +598,13 @@ describe('$mdPanel', function() {
597598

598599
it('should allow locals to be injected in the controller', function() {
599600
var htmlContent = 'Tiramisu';
600-
var template = '<div>{{ ctrl.content }}</div>';
601+
var template = '<div>{{ ctrl.content }} {{ ctrl.myPanel.id }}</div>';
601602

602603
var config = {
603604
template: template,
604-
controller: function Ctrl(content) {
605+
controller: function Ctrl(content, mdPanelRef) {
605606
this.content = content;
607+
this.myPanel = mdPanelRef;
606608
},
607609
controllerAs: 'ctrl',
608610
locals: { content: htmlContent },
@@ -612,16 +614,18 @@ describe('$mdPanel', function() {
612614
openPanel(config);
613615

614616
expect(PANEL_EL).toContainHtml(htmlContent);
617+
expect(PANEL_EL).toContainHtml(PANEL_ID_PREFIX);
615618
});
616619

617620
it('should bind locals to the controller', function() {
618621
var htmlContent = 'Apple Pie';
619-
var template = '<div>{{ ctrl.content }}</div>';
622+
var template = '<div>{{ ctrl.content }} {{ ctrl.mdPanelRef.id }}</div>';
620623

621624
var config = {
622625
template: template,
623626
controller: function Ctrl() {
624627
this.content; // Populated via bindToController.
628+
this.mdPanelRef;
625629
},
626630
controllerAs: 'ctrl',
627631
locals: { content: htmlContent }
@@ -630,6 +634,36 @@ describe('$mdPanel', function() {
630634
openPanel(config);
631635

632636
expect(PANEL_EL).toContainHtml(htmlContent);
637+
expect(PANEL_EL).toContainHtml(PANEL_ID_PREFIX);
638+
});
639+
640+
it('should inject mdPanelRef to the controller', function() {
641+
var htmlContent = 'Cupcake';
642+
var template =
643+
'<button ng-click="ctrl.closeSelf()">{{ ctrl.content }}</button>';
644+
645+
function Ctrl() {
646+
this.content; // Populated via bindToController.
647+
this.mdPanelRef;
648+
}
649+
650+
Ctrl.prototype.closeSelf = function() {
651+
this.mdPanelRef.close();
652+
};
653+
654+
var config = {
655+
template: template,
656+
controller: Ctrl,
657+
controllerAs: 'ctrl',
658+
locals: { content: htmlContent }
659+
};
660+
661+
openPanel(config);
662+
663+
expect(PANEL_EL).toContainHtml(htmlContent);
664+
665+
document.querySelector(PANEL_EL + ' button').click();
666+
expect(PANEL_EL).not.toExist();
633667
});
634668
});
635669

0 commit comments

Comments
 (0)