pyc-website
main website for pyc inc.
git clone https://9o.is/git/pyc-website.git
angular-wizard.js
(7004B)
1 /**
2 * Easy to use Wizard library for AngularJS
3 * @version v0.4.0 - 2014-05-17 * @link https://github.com/mgonto/angular-wizard
4 * @author Martin Gontovnikas <martin@gon.to>
5 * @license MIT License, http://www.opensource.org/licenses/MIT
6 */
7 angular.module('templates-angularwizard', ['step.html', 'wizard.html']);
8
9 angular.module("step.html", []).run(["$templateCache", function($templateCache) {
10 $templateCache.put("step.html",
11 "<section ng-show=\"selected\" ng-class=\"{current: selected, done: completed}\" class=\"step\" ng-transclude>\n" +
12 "</section>");
13 }]);
14
15 angular.module("wizard.html", []).run(["$templateCache", function($templateCache) {
16 $templateCache.put("wizard.html",
17 "<div>\n" +
18 " <div class=\"steps\" ng-transclude></div>\n" +
19 " <ul class=\"steps-indicator steps-{{steps.length}}\" ng-if=\"!hideIndicators\">\n" +
20 " <li ng-class=\"{default: !step.completed && !step.selected, current: step.selected && !step.completed, done: step.completed && !step.selected, editing: step.selected && step.completed}\" ng-repeat=\"step in steps\">\n" +
21 " <a ng-click=\"goTo(step)\">{{step.title || step.wzTitle}}</a>\n" +
22 " </li>\n" +
23 " </ul>\n" +
24 "</div>\n" +
25 "");
26 }]);
27
28 angular.module('mgo-angular-wizard', ['templates-angularwizard']);
29
30 angular.module('mgo-angular-wizard').directive('wzStep', function() {
31 return {
32 restrict: 'EA',
33 replace: true,
34 transclude: true,
35 scope: {
36 wzTitle: '@',
37 title: '@'
38 },
39 require: '^wizard',
40 templateUrl: function(element, attributes) {
41 return attributes.template || "step.html";
42 },
43 link: function($scope, $element, $attrs, wizard) {
44 $scope.title = $scope.title || $scope.wzTitle;
45 wizard.addStep($scope);
46 }
47 };
48 });
49
50 angular.module('mgo-angular-wizard').directive('wizard', function() {
51 return {
52 restrict: 'EA',
53 replace: true,
54 transclude: true,
55 scope: {
56 currentStep: '=',
57 onFinish: '&',
58 hideIndicators: '=',
59 editMode: '=',
60 name: '@'
61 },
62 templateUrl: function(element, attributes) {
63 return attributes.template || "wizard.html";
64 },
65 controller: ['$scope', '$element', 'WizardHandler', function($scope, $element, WizardHandler) {
66
67 WizardHandler.addWizard($scope.name || WizardHandler.defaultName, this);
68 $scope.$on('$destroy', function() {
69 WizardHandler.removeWizard($scope.name || WizardHandler.defaultName);
70 });
71
72 $scope.steps = [];
73
74 $scope.$watch('currentStep', function(step) {
75 if (!step) return;
76 var stepTitle = $scope.selectedStep.title || $scope.selectedStep.wzTitle;
77 if ($scope.selectedStep && stepTitle !== $scope.currentStep) {
78 $scope.goTo(_.findWhere($scope.steps, {title: $scope.currentStep}));
79 }
80
81 });
82
83 $scope.$watch('[editMode, steps.length]', function() {
84 var editMode = $scope.editMode;
85 if (_.isUndefined(editMode) || _.isNull(editMode)) return;
86
87 if (editMode) {
88 _.each($scope.steps, function(step) {
89 step.completed = true;
90 });
91 }
92 }, true);
93
94 this.addStep = function(step) {
95 $scope.steps.push(step);
96 if ($scope.steps.length === 1) {
97 $scope.goTo($scope.steps[0]);
98 }
99 };
100
101 $scope.goTo = function(step) {
102 unselectAll();
103 $scope.selectedStep = step;
104 if (!_.isUndefined($scope.currentStep)) {
105 $scope.currentStep = step.title || step.wzTitle;
106 }
107 step.selected = true;
108 $scope.$emit('wizard:stepChanged', {step: step, index: _.indexOf($scope.steps , step)});
109 };
110
111 function unselectAll() {
112 _.each($scope.steps, function (step) {
113 step.selected = false;
114 });
115 $scope.selectedStep = null;
116 }
117
118 this.next = function(draft) {
119 var index = _.indexOf($scope.steps , $scope.selectedStep);
120 if (!draft) {
121 $scope.selectedStep.completed = true;
122 }
123 if (index === $scope.steps.length - 1) {
124 this.finish();
125 } else {
126 $scope.goTo($scope.steps[index + 1]);
127 }
128 };
129
130 this.goTo = function(step) {
131 var stepTo;
132 if (_.isNumber(step)) {
133 stepTo = $scope.steps[step];
134 } else {
135 stepTo = _.findWhere($scope.steps, {title: step});
136 }
137 $scope.goTo(stepTo);
138 };
139
140 this.finish = function() {
141 if ($scope.onFinish) {
142 $scope.onFinish();
143 }
144 };
145
146 this.cancel = this.previous = function() {
147 var index = _.indexOf($scope.steps , $scope.selectedStep);
148 if (index === 0) {
149 throw new Error("Can't go back. It's already in step 0");
150 } else {
151 $scope.goTo($scope.steps[index - 1]);
152 }
153 };
154 }]
155 };
156 });
157
158 function wizardButtonDirective(action) {
159 angular.module('mgo-angular-wizard')
160 .directive(action, function() {
161 return {
162 restrict: 'A',
163 replace: false,
164 require: '^wizard',
165 link: function($scope, $element, $attrs, wizard) {
166
167 $element.on("click", function(e) {
168 e.preventDefault();
169 $scope.$apply(function() {
170 $scope.$eval($attrs[action]);
171 wizard[action.replace("wz", "").toLowerCase()]();
172 });
173 });
174 }
175 };
176 });
177 }
178
179 wizardButtonDirective('wzNext');
180 wizardButtonDirective('wzPrevious');
181 wizardButtonDirective('wzFinish');
182 wizardButtonDirective('wzCancel');
183
184 angular.module('mgo-angular-wizard').factory('WizardHandler', function() {
185 var service = {};
186
187 var wizards = {};
188
189 service.defaultName = "defaultWizard";
190
191 service.addWizard = function(name, wizard) {
192 wizards[name] = wizard;
193 };
194
195 service.removeWizard = function(name) {
196 delete wizards[name];
197 };
198
199 service.wizard = function(name) {
200 var nameToUse = name;
201 if (!name) {
202 nameToUse = service.defaultName;
203 }
204
205 return wizards[nameToUse];
206 };
207
208 return service;
209 });