Skip to content

Commit 63bc0e4

Browse files
committed
added template support
1 parent 1f0f6cf commit 63bc0e4

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ And the Bootstrap Tour CSS (or create your own):
3232
Then add the module to your app:
3333

3434
angular.module('myApp', ['bm.bsTour']);
35-
35+
3636
## Examples
3737

38-
<div tour placement="top" on-end="onTourEnd(tour)" after-get-state="afterGetStateFunction">
38+
<div tour placement="top" on-end="onTourEnd(tour)" after-get-state="afterGetStateFunction" template-url="tour_template.html">
3939
<div id="mainMenu" tour-step title="Main Menu" content="{{mainMenuDescription}}" order="0" skip="pageName !== 'home'">
4040
...
4141
</div>
@@ -64,7 +64,6 @@ The title and contents options are watched, so an interpolated value can be pass
6464

6565
## TODO's
6666

67-
- Add template support (both string templates and template URLs)
6867
- Write some tests!! (Come on Ben, stop being lazy ;p)
6968

7069
## Build It Yourself
@@ -83,7 +82,9 @@ Assuming you have Node, grunt, and bower installed:
8382
I am using this in a personal project, but I haven't needed to use all the Bootstrap Tour options. This means that some of them might not be working
8483
due to the option values either not being passed correctly, or not being passed as interpolated values.
8584
If you run across any issues please report them with an example and I will fix them ASAP, or fork me and create a PR.
86-
I will have support for templates very soon, hopefully this week.
85+
You can now pass a template URL to either the tour or tour-step directives, and the template will be linked to whichever scope the template is specified on.
86+
(ie. if you add the template URL to the tour directive, it will always use the tour directive's scope, if you add it to a step, it will use the step's scope.)
87+
Alternatively, you can specify an expression that evaluates to a string that will be used as the template (using the "template" attribute.)
8788

8889
Thanks and enjoy!
8990

app/angular-bootstrap-tour.js

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,38 @@
8282

8383
}
8484

85+
app.factory('TourTemplateFactory', ['$templateCache', '$compile', function ($templateCache, $compile) {
86+
87+
var templateFactory = {};
88+
89+
templateFactory.wrap = function (template, scope) {
90+
return function (/*index, step*/) {
91+
var $template = angular.element(template); //requires jQuery
92+
safeApply(scope, function () {
93+
$compile($template)(scope);
94+
});
95+
return $template;
96+
}
97+
98+
};
99+
100+
templateFactory.get = function (templateUrl, scope) {
101+
102+
var template = $templateCache.get(templateUrl);
103+
104+
if (template) {
105+
return templateFactory.wrap(template, scope);
106+
}
107+
108+
return null;
109+
110+
};
111+
112+
113+
return templateFactory;
114+
115+
}]);
116+
85117
app.controller('TourController', ['$filter', function ($filter) {
86118

87119
var self = this,
@@ -137,7 +169,7 @@
137169

138170
}]);
139171

140-
app.directive('tour', [function () {
172+
app.directive('tour', ['TourTemplateFactory', function (TourTemplateFactory) {
141173

142174
return {
143175
restrict: 'EA',
@@ -157,7 +189,8 @@
157189
placement: attrs.placement || 'right',
158190
backdrop: attrs.backdrop || false,
159191
orphan: attrs.orphan || false
160-
};
192+
},
193+
template;
161194

162195
attachEventHandlers(scope, attrs, options);
163196

@@ -197,6 +230,18 @@
197230
};
198231
}
199232

233+
if (attrs.template) {
234+
template = TourTemplateFactory.wrap(scope.$eval(attrs.template), scope);
235+
}
236+
237+
if (attrs.templateUrl) {
238+
template = TourTemplateFactory.get(attrs.templateUrl, scope);
239+
}
240+
241+
if (template) {
242+
options.template = template;
243+
}
244+
200245
scope.$watchCollection(ctrl.getSteps, function (steps) {
201246
scope.stepCount = steps.length;
202247
});
@@ -212,7 +257,7 @@
212257

213258
}]);
214259

215-
app.directive('tourStep', [function () {
260+
app.directive('tourStep', ['TourTemplateFactory', function (TourTemplateFactory) {
216261

217262
return {
218263
restrict: 'EA',
@@ -223,7 +268,8 @@
223268
var step = {
224269
element: element,
225270
order: attrs.order || 0
226-
};
271+
},
272+
template;
227273

228274
if (attrs.path) { step.path = attrs.path; }
229275
if (attrs.animation) { step.animation = attrs.animation; }
@@ -244,6 +290,17 @@
244290
step.content = title;
245291
});
246292

293+
if (attrs.template) {
294+
template = TourTemplateFactory.wrap(scope.$eval(attrs.template), scope);
295+
}
296+
297+
if (attrs.templateUrl) {
298+
template = TourTemplateFactory.get(attrs.templateUrl, scope);
299+
}
300+
301+
if (template) {
302+
options.template = template;
303+
}
247304

248305
function stepIsSkipped() {
249306
if (attrs.skip) {

0 commit comments

Comments
 (0)