2017-09-25 16:47:40 +02:00
|
|
|
export default {
|
|
|
|
name: 'custom-routes',
|
|
|
|
|
2017-10-05 02:36:46 +02:00
|
|
|
initialize(app) {
|
|
|
|
if (app.constructor.name !== 'Class' || app.get('rootElement') !== '#custom-wizard-main') return;
|
2017-09-29 13:27:03 +02:00
|
|
|
|
|
|
|
const Router = requirejs('wizard/router').default;
|
2017-10-13 15:02:34 +02:00
|
|
|
const ApplicationRoute = requirejs('wizard/routes/application').default;
|
2017-09-29 13:27:03 +02:00
|
|
|
const ajax = requirejs('wizard/lib/ajax').ajax;
|
|
|
|
const StepModel = requirejs('wizard/models/step').default;
|
2017-10-05 02:36:46 +02:00
|
|
|
const WizardStep = requirejs('wizard/components/wizard-step').default;
|
|
|
|
const getUrl = requirejs('discourse-common/lib/get-url').default;
|
2017-10-09 07:52:09 +02:00
|
|
|
const FieldModel = requirejs('wizard/models/wizard-field').default;
|
2017-09-29 13:27:03 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
Router.reopen({
|
|
|
|
rootURL: getUrl('/w/')
|
|
|
|
});
|
|
|
|
|
2017-09-25 16:47:40 +02:00
|
|
|
Router.map(function() {
|
2017-10-13 15:02:34 +02:00
|
|
|
this.route('custom', { path: '/:wizard_id' }, function() {
|
2017-10-22 05:37:58 +02:00
|
|
|
this.route('steps');
|
2017-09-25 16:47:40 +02:00
|
|
|
this.route('step', { path: '/steps/:step_id' });
|
|
|
|
});
|
|
|
|
});
|
2017-09-29 13:27:03 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
ApplicationRoute.reopen({
|
|
|
|
redirect() {
|
|
|
|
this.transitionTo('custom');
|
2017-10-15 05:58:22 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
model() {}
|
2017-09-29 13:27:03 +02:00
|
|
|
});
|
2017-10-05 02:36:46 +02:00
|
|
|
|
|
|
|
WizardStep.reopen({
|
2017-11-01 05:21:14 +01:00
|
|
|
showQuitButton: function() {
|
|
|
|
const index = this.get('step.index');
|
|
|
|
const required = this.get('wizard.required');
|
|
|
|
return index === 0 && !required;
|
|
|
|
}.property('step.index', 'wizard.required'),
|
|
|
|
|
2017-10-06 04:59:02 +02:00
|
|
|
bannerImage: function() {
|
|
|
|
const src = this.get('step.banner');
|
|
|
|
if (!src) return;
|
|
|
|
|
2017-10-07 04:27:38 +02:00
|
|
|
if (src.indexOf('/uploads/') > -1 || src.indexOf('/images/') > -1) {
|
2017-10-06 04:59:02 +02:00
|
|
|
return getUrl(src);
|
|
|
|
} else {
|
|
|
|
return getUrl(`/images/wizard/${src}`);
|
|
|
|
};
|
|
|
|
}.property('step.banner'),
|
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
handleMessage: function() {
|
|
|
|
const message = this.get('step.message');
|
|
|
|
this.sendAction('showMessage', message);
|
|
|
|
}.observes('step.message'),
|
|
|
|
|
2017-10-05 02:36:46 +02:00
|
|
|
advance() {
|
|
|
|
this.set('saving', true);
|
|
|
|
this.get('step').save()
|
|
|
|
.then(response => {
|
|
|
|
if (this.get('finalStep')) {
|
2017-11-01 05:21:14 +01:00
|
|
|
this.get('wizard').finished(response);
|
2017-10-05 02:36:46 +02:00
|
|
|
} else {
|
|
|
|
this.sendAction('goNext', response);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => this.animateInvalidFields())
|
|
|
|
.finally(() => this.set('saving', false));
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
quit() {
|
2017-11-01 05:21:14 +01:00
|
|
|
if ($(event.target).hasClass('quit')) {
|
|
|
|
this.get('wizard').skip();
|
|
|
|
} else {
|
|
|
|
this.set('finalStep', true);
|
|
|
|
this.send('nextStep');
|
|
|
|
};
|
2017-10-13 15:02:34 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
showMessage(message) {
|
|
|
|
this.sendAction('showMessage', message);
|
2017-10-05 02:36:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-10-09 07:52:09 +02:00
|
|
|
|
2017-10-13 15:02:34 +02:00
|
|
|
StepModel.reopen({
|
|
|
|
save() {
|
|
|
|
const wizardId = this.get('wizardId');
|
|
|
|
const fields = {};
|
|
|
|
this.get('fields').forEach(f => fields[f.id] = f.value);
|
|
|
|
return ajax({
|
|
|
|
url: `/w/${wizardId}/steps/${this.get('id')}`,
|
|
|
|
type: 'PUT',
|
|
|
|
data: { fields }
|
|
|
|
}).catch(response => {
|
|
|
|
if (response && response.responseJSON && response.responseJSON.errors) {
|
|
|
|
let wizardErrors = [];
|
|
|
|
response.responseJSON.errors.forEach(err => {
|
|
|
|
if (err.field === wizardId) {
|
|
|
|
wizardErrors.push(err.description);
|
|
|
|
} else if (err.field) {
|
|
|
|
this.fieldError(err.field, err.description);
|
|
|
|
} else if (err) {
|
|
|
|
wizardErrors.push(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (wizardErrors.length) {
|
|
|
|
this.handleWizardError(wizardErrors.join('\n'));
|
|
|
|
}
|
|
|
|
throw response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response && response.responseText) {
|
|
|
|
const responseText = response.responseText;
|
|
|
|
const start = responseText.indexOf('>') + 1;
|
|
|
|
const end = responseText.indexOf('plugins');
|
|
|
|
const message = responseText.substring(start, end);
|
|
|
|
this.handleWizardError(message);
|
|
|
|
throw message;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
handleWizardError(message) {
|
|
|
|
this.set('message', {
|
|
|
|
state: 'error',
|
|
|
|
text: message
|
|
|
|
});
|
|
|
|
Ember.run.later(() => this.set('message', null), 6000);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-10-09 07:52:09 +02:00
|
|
|
FieldModel.reopen({
|
|
|
|
check() {
|
|
|
|
let valid = this.get('valid');
|
|
|
|
|
|
|
|
if (!this.get('required')) {
|
|
|
|
this.setValid(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.get('customValidation')) {
|
|
|
|
const val = this.get('value');
|
|
|
|
valid = val && val.length > 0;
|
|
|
|
|
|
|
|
this.setValid(valid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
});
|
2017-09-25 16:47:40 +02:00
|
|
|
}
|
|
|
|
};
|