This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
/
Copy pathngTransclude.js
243 lines (237 loc) · 9.56 KB
/
ngTransclude.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
'use strict';
/**
* @ngdoc directive
* @name ngTransclude
* @restrict EAC
*
* @description
* Directive that marks the insertion point for the transcluded DOM of the nearest parent directive that uses transclusion.
*
* You can specify that you want to insert a named transclusion slot, instead of the default slot, by providing the slot name
* as the value of the `ng-transclude` or `ng-transclude-slot` attribute.
*
* If the transcluded content is not empty (i.e. contains one or more DOM nodes, including whitespace text nodes), any existing
* content of this element will be removed before the transcluded content is inserted.
* If the transcluded content is empty, the existing content is left intact. This lets you provide fallback content in the case
* that no transcluded content is provided.
*
* @element ANY
*
* @param {string} ngTransclude|ngTranscludeSlot the name of the slot to insert at this point. If this is not provided, is empty
* or its value is the same as the name of the attribute then the default slot is used.
*
* @example
* ### Basic transclusion
* This example demonstrates basic transclusion of content into a component directive.
* <example name="simpleTranscludeExample" module="transcludeExample">
* <file name="index.html">
* <script>
* angular.module('transcludeExample', [])
* .directive('pane', function(){
* return {
* restrict: 'E',
* transclude: true,
* scope: { title:'@' },
* template: '<div style="border: 1px solid black;">' +
* '<div style="background-color: gray">{{title}}</div>' +
* '<ng-transclude></ng-transclude>' +
* '</div>'
* };
* })
* .controller('ExampleController', ['$scope', function($scope) {
* $scope.title = 'Lorem Ipsum';
* $scope.text = 'Neque porro quisquam est qui dolorem ipsum quia dolor...';
* }]);
* </script>
* <div ng-controller="ExampleController">
* <input ng-model="title" aria-label="title"> <br/>
* <textarea ng-model="text" aria-label="text"></textarea> <br/>
* <pane title="{{title}}"><span>{{text}}</span></pane>
* </div>
* </file>
* <file name="protractor.js" type="protractor">
* it('should have transcluded', function() {
* var titleElement = element(by.model('title'));
* titleElement.clear();
* titleElement.sendKeys('TITLE');
* var textElement = element(by.model('text'));
* textElement.clear();
* textElement.sendKeys('TEXT');
* expect(element(by.binding('title')).getText()).toEqual('TITLE');
* expect(element(by.binding('text')).getText()).toEqual('TEXT');
* });
* </file>
* </example>
*
* @example
* ### Transclude fallback content
* This example shows how to use `NgTransclude` with fallback content, that
* is displayed if no transcluded content is provided.
*
* <example module="transcludeFallbackContentExample">
* <file name="index.html">
* <script>
* angular.module('transcludeFallbackContentExample', [])
* .directive('myButton', function(){
* return {
* restrict: 'E',
* transclude: true,
* scope: true,
* template: '<button style="cursor: pointer;">' +
* '<ng-transclude>' +
* '<b style="color: red;">Button1</b>' +
* '</ng-transclude>' +
* '</button>'
* };
* });
* </script>
* <!-- fallback button content -->
* <my-button id="fallback"></my-button>
* <!-- modified button content -->
* <my-button id="modified">
* <i style="color: green;">Button2</i>
* </my-button>
* </file>
* <file name="protractor.js" type="protractor">
* it('should have different transclude element content', function() {
* expect(element(by.id('fallback')).getText()).toBe('Button1');
* expect(element(by.id('modified')).getText()).toBe('Button2');
* });
* </file>
* </example>
*
* @example
* ### Multi-slot transclusion
* This example demonstrates using multi-slot transclusion in a component directive.
* <example name="multiSlotTranscludeExample" module="multiSlotTranscludeExample">
* <file name="index.html">
* <style>
* .title, .footer {
* background-color: gray
* }
* </style>
* <div ng-controller="ExampleController">
* <input ng-model="title" aria-label="title"> <br/>
* <textarea ng-model="text" aria-label="text"></textarea> <br/>
* <pane>
* <pane-title><a ng-href="{{link}}">{{title}}</a></pane-title>
* <pane-body><p>{{text}}</p></pane-body>
* </pane>
* </div>
* </file>
* <file name="app.js">
* angular.module('multiSlotTranscludeExample', [])
* .directive('pane', function(){
* return {
* restrict: 'E',
* transclude: {
* 'title': '?paneTitle',
* 'body': 'paneBody',
* 'footer': '?paneFooter'
* },
* template: '<div style="border: 1px solid black;">' +
* '<div class="title" ng-transclude="title">Fallback Title</div>' +
* '<div ng-transclude="body"></div>' +
* '<div class="footer" ng-transclude="footer">Fallback Footer</div>' +
* '</div>'
* };
* })
* .controller('ExampleController', ['$scope', function($scope) {
* $scope.title = 'Lorem Ipsum';
* $scope.link = "https://google.com";
* $scope.text = 'Neque porro quisquam est qui dolorem ipsum quia dolor...';
* }]);
* </file>
* <file name="protractor.js" type="protractor">
* it('should have transcluded the title and the body', function() {
* var titleElement = element(by.model('title'));
* titleElement.clear();
* titleElement.sendKeys('TITLE');
* var textElement = element(by.model('text'));
* textElement.clear();
* textElement.sendKeys('TEXT');
* expect(element(by.css('.title')).getText()).toEqual('TITLE');
* expect(element(by.binding('text')).getText()).toEqual('TEXT');
* expect(element(by.css('.footer')).getText()).toEqual('Fallback Footer');
* });
* </file>
* </example>
*
* @example
* ### Dynamic multi-slot transclusion
* This example demonstrates using dynamic approach with multi-slot transclusion in a component directive.
* In this example, we use this mode to define a custom table component, while allowing the consumers to cleanly but
* optionally define their own cell templates:
* <example name="dynamicMultiSlotTranscludeExample" module="dynamicMultiSlotTranscludeExample">
* <file name="index.html">
* <style>
* .title, .footer {
* background-color: gray
* }
* </style>
* <div>
* <my-table transclude-slots="{ firstName: 'myFn', lastName: 'myLn' }"
* headers="[{name: 'firstName', caption: 'First Name'}, {name: 'middleName', caption: 'Middle Name'}, {name: 'lastName', caption: 'Last Name'}]"
* data="[{firstName: 'Ann', middleName: 'Margaret', lastName: 'Smith'}, {firstName: 'Edward', middleName: 'John', lastName: 'Williams'}]">
* <my-fn><strong>{{$parent.row['firstName']}}</strong></my-fn>
* <my-ln>{{$parent.row['lastName'].toUpperCase()}}</my-ln>
* </my-table>
* </div>
* </file>
* <file name="app.js">
* angular.module('dynamicMultiSlotTranscludeExample', [])
* .directive('myTable', function() {
* return {
* restrict: 'E',
* transclude: 'dynamic',
* template: '<table>' +
* '<thead>' +
* '<th ng-repeat="header in headers">{{header.caption}}</th>' +
* '</thead>' +
* '<tbody>' +
* '<tr ng-repeat="row in data">' +
* '<td ng-repeat="header in headers">' +
* '<div ng-if="transcludeSlots[header.name]"><div ng-transclude="{{header.name}}"></div></div>' +
* '<div ng-if="!transcludeSlots[header.name]">{{row[header.name]}}</div>' +
* '</td>' +
* '</tr>' +
* '</tbody>' +
* '</table>',
* scope: {
* transcludeSlots: '=',
* headers: '=',
* data: '='
* }
* };
* });
* </file>
* </example>
*/
var ngTranscludeMinErr = minErr('ngTransclude');
var ngTranscludeDirective = ngDirective({
restrict: 'EAC',
link: function($scope, $element, $attrs, controller, $transclude) {
if ($attrs.ngTransclude === $attrs.$attr.ngTransclude) {
// If the attribute is of the form: `ng-transclude="ng-transclude"`
// then treat it like the default
$attrs.ngTransclude = '';
}
function ngTranscludeCloneAttachFn(clone) {
if (clone.length) {
$element.empty();
$element.append(clone);
}
}
if (!$transclude) {
throw ngTranscludeMinErr('orphan',
'Illegal use of ngTransclude directive in the template! ' +
'No parent directive that requires a transclusion found. ' +
'Element: {0}',
startingTag($element));
}
// If there is no slot name defined or the slot name is not optional
// then transclude the slot
var slotName = $attrs.ngTransclude || $attrs.ngTranscludeSlot;
$transclude(ngTranscludeCloneAttachFn, null, slotName);
}
});