Skip to content

Commit 9568f50

Browse files
committed
added interpolation helper to minimize code
1 parent f38db29 commit 9568f50

File tree

1 file changed

+51
-45
lines changed

1 file changed

+51
-45
lines changed

app/angular-bootstrap-tour.js

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
* @param {$rootScope.Scope} scope
5858
* @returns {Function}
5959
*/
60-
function lookupTemplate(templateUrl, scope) {
60+
function lookupTemplate(templateUrl, scope) {
6161

6262
var template = $templateCache.get(templateUrl);
6363

@@ -74,7 +74,7 @@
7474
*
7575
* @param {$rootScope.Scope} scope
7676
* @param {Attributes} attrs
77-
* @param {Object} options
77+
* @param {Object} options represents the tour or step object
7878
*/
7979
helpers.attachTemplate = function (scope, attrs, options) {
8080

@@ -99,7 +99,7 @@
9999
*
100100
* @param {$rootScope.Scope} scope
101101
* @param {Attributes} attrs
102-
* @param {Object} options
102+
* @param {Object} options represents the tour or step object
103103
* @param {Array} events
104104
*/
105105
helpers.attachEventHandlers = function (scope, attrs, options, events) {
@@ -116,6 +116,25 @@
116116

117117
};
118118

119+
/**
120+
* Helper function that attaches observers to option attributes
121+
*
122+
* @param {Attributes} attrs
123+
* @param {Object} options represents the tour or step object
124+
* @param {Array} keys attribute names
125+
*/
126+
helpers.attachInterpolatedValues = function (attrs, options, keys) {
127+
128+
angular.forEach(keys, function (key) {
129+
if (attrs[key]) {
130+
attrs.$observe(key, function (newValue) {
131+
options[key] = newValue;
132+
});
133+
}
134+
});
135+
136+
};
137+
119138
return helpers;
120139

121140
}]);
@@ -146,12 +165,12 @@
146165
/**
147166
* As steps are linked, add them to the tour options
148167
*/
149-
function refreshTour() {
168+
self.refreshTour = function () {
150169
if (tour) {
151170
tour._options.steps = [];
152171
tour.addSteps(orderSteps(steps));
153172
}
154-
}
173+
};
155174

156175
/**
157176
* Adds a step to the tour
@@ -164,7 +183,7 @@
164183
}
165184

166185
steps.push(step);
167-
refreshTour();
186+
self.refreshTour();
168187
};
169188

170189
/**
@@ -178,7 +197,7 @@
178197
}
179198

180199
steps.splice(steps.indexOf(step), 1);
181-
refreshTour();
200+
self.refreshTour();
182201
};
183202

184203
/**
@@ -214,26 +233,18 @@
214233
link: function (scope, element, attrs, ctrl) {
215234

216235
//Pass static options through or use defaults
217-
var options = {
218-
name: attrs.name || 'tour',
219-
container: attrs.container || 'body',
220-
keyboard: attrs.keyboard || true,
221-
storage: attrs.storage ? scope.$eval(attrs.storage) : window.localStorage,
222-
debug: attrs.debug || false,
223-
redirect: attrs.redirect || true,
224-
duration: attrs.duration || false,
225-
basePath: attrs.basePath || '',
226-
placement: attrs.placement || 'right',
227-
backdrop: attrs.backdrop || false,
228-
orphan: attrs.orphan || false
229-
},
230-
events = 'onStart onEnd afterGetState afterSetState afterRemoveState onShow onShown onHide onHidden onNext onPrev onPause onResume'.split(' ');
236+
var tour = {},
237+
events = 'onStart onEnd afterGetState afterSetState afterRemoveState onShow onShown onHide onHidden onNext onPrev onPause onResume'.split(' '),
238+
options = 'name container keyboard storage debug redirect duration basePath placement backdrop orphan'.split(' ');
239+
240+
//Pass interpolated values through
241+
TourHelpers.attachInterpolatedValues(attrs, tour, options);
231242

232243
//Attach event handlers
233-
TourHelpers.attachEventHandlers(scope, attrs, options, events);
244+
TourHelpers.attachEventHandlers(scope, attrs, tour, events);
234245

235246
//Compile template
236-
TourHelpers.attachTemplate(scope, attrs, options);
247+
TourHelpers.attachTemplate(scope, attrs, tour);
237248

238249
//Monitor number of steps
239250
scope.$watchCollection(ctrl.getSteps, function (steps) {
@@ -242,11 +253,11 @@
242253

243254
//If there is an options argument passed, just use that instead
244255
if (attrs.tourOptions) {
245-
angular.extend(options, scope.$eval(attrs.tourOptions));
256+
angular.extend(tour, scope.$eval(attrs.tourOptions));
246257
}
247258

248259
//Initialize tour
249-
scope.tour = ctrl.init(options);
260+
scope.tour = ctrl.init(tour);
250261

251262
}
252263
};
@@ -263,27 +274,16 @@
263274

264275
//Assign required options
265276
var step = {
266-
element: element,
267-
order: attrs.order || 0
277+
element: element
268278
},
269-
events = 'onShow onShown onHide onHidden onNext onPrev onPause onResume'.split(' ');
270-
271-
//Pass static values through
272-
if (attrs.path) { step.path = attrs.path; }
273-
if (attrs.animation) { step.animation = attrs.animation; }
274-
if (attrs.container) { step.container = attrs.container; }
275-
if (attrs.placement) { step.placement = attrs.placement; }
276-
if (attrs.backdrop) { step.backdrop = attrs.backdrop; }
277-
if (attrs.redirect) { step.redirect = attrs.redirect; }
278-
if (attrs.orphan) { step.orphan = attrs.orphan; }
279-
if (attrs.reflex) { step.reflex = attrs.reflex; }
279+
events = 'onShow onShown onHide onHidden onNext onPrev onPause onResume'.split(' '),
280+
options = 'content title path animation container placement backdrop redirect orphan reflex'.split(' ');
280281

281282
//Pass interpolated values through
282-
attrs.$observe('content', function (content) {
283-
step.content = content;
284-
});
285-
attrs.$observe('title', function (title) {
286-
step.content = title;
283+
TourHelpers.attachInterpolatedValues(attrs, step, options);
284+
attrs.$observe('order', function (order) {
285+
step.order = !isNaN(order*1) ? order*1 : 0;
286+
ctrl.refreshTour();
287287
});
288288

289289
//Attach event handlers
@@ -294,10 +294,14 @@
294294

295295
//Check whether or not the step should be skipped
296296
function stepIsSkipped() {
297+
var skipped;
297298
if (attrs.skip) {
298-
return scope.$eval(attrs.skip);
299+
skipped = scope.$eval(attrs.skip);
300+
}
301+
if (!skipped) {
302+
skipped = element.is(':hidden');
299303
}
300-
return false;
304+
return skipped;
301305
}
302306
scope.$watch(stepIsSkipped, function (skip) {
303307
if (skip) {
@@ -321,3 +325,5 @@
321325
}]);
322326

323327
}(angular.module('bm.bsTour', [])));
328+
329+

0 commit comments

Comments
 (0)