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 WizardApplicationRoute = requirejs('wizard/routes/application').default;
|
|
|
|
const findCustomWizard = requirejs('discourse/plugins/discourse-custom-wizard/wizard/models/custom').findCustomWizard;
|
|
|
|
const Router = requirejs('wizard/router').default;
|
|
|
|
const ajax = requirejs('wizard/lib/ajax').ajax;
|
|
|
|
const StepRoute = requirejs('wizard/routes/step').default;
|
|
|
|
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-09-25 16:47:40 +02:00
|
|
|
Router.map(function() {
|
2017-09-29 13:27:03 +02:00
|
|
|
this.route('custom', { path: '/custom/:id' }, function() {
|
2017-09-25 16:47:40 +02:00
|
|
|
this.route('step', { path: '/steps/:step_id' });
|
|
|
|
});
|
|
|
|
});
|
2017-09-29 13:27:03 +02:00
|
|
|
|
|
|
|
WizardApplicationRoute.reopen({
|
|
|
|
model() {
|
|
|
|
const customParams = this.paramsFor('custom');
|
|
|
|
return findCustomWizard(customParams.id);
|
|
|
|
},
|
|
|
|
|
|
|
|
afterModel(model) {
|
2017-10-09 07:52:09 +02:00
|
|
|
return Ember.RSVP.hash({
|
|
|
|
info: ajax({
|
|
|
|
url: `/site/basic-info`,
|
|
|
|
type: 'GET',
|
|
|
|
}).then((result) => {
|
|
|
|
return model.set('siteInfo', result);
|
|
|
|
}),
|
|
|
|
settings: ajax({
|
|
|
|
url: `/site/settings`,
|
|
|
|
type: 'GET',
|
|
|
|
}).then((result) => {
|
|
|
|
Object.assign(Wizard.SiteSettings, result);
|
|
|
|
})
|
2017-09-29 13:27:03 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setupController(controller, model) {
|
2017-10-07 04:27:38 +02:00
|
|
|
Ember.run.scheduleOnce('afterRender', this, function(){
|
|
|
|
$('body.custom-wizard').css('background', model.get('background'));
|
|
|
|
});
|
|
|
|
|
2017-09-29 13:27:03 +02:00
|
|
|
controller.setProperties({
|
|
|
|
customWizard: true,
|
|
|
|
siteInfo: model.get('siteInfo')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
StepModel.reopen({
|
|
|
|
save() {
|
|
|
|
const fields = {};
|
|
|
|
this.get('fields').forEach(f => fields[f.id] = f.value);
|
|
|
|
return ajax({
|
|
|
|
url: `/wizard/custom/${this.get('wizardId')}/steps/${this.get('id')}`,
|
|
|
|
type: 'PUT',
|
|
|
|
data: { fields }
|
|
|
|
}).catch(response => {
|
|
|
|
response.responseJSON.errors.forEach(err => this.fieldError(err.field, err.description));
|
|
|
|
throw response;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
StepRoute.reopen({
|
|
|
|
afterModel(model) {
|
2017-10-07 04:27:38 +02:00
|
|
|
if (!model) {
|
|
|
|
return document.location = getUrl("/");
|
|
|
|
}
|
|
|
|
|
2017-09-29 13:27:03 +02:00
|
|
|
const wizard = this.modelFor('application');
|
|
|
|
return model.set("wizardId", wizard.id);
|
|
|
|
}
|
|
|
|
});
|
2017-10-05 02:36:46 +02:00
|
|
|
|
|
|
|
WizardStep.reopen({
|
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-05 02:36:46 +02:00
|
|
|
advance() {
|
|
|
|
this.set('saving', true);
|
|
|
|
this.get('step').save()
|
|
|
|
.then(response => {
|
|
|
|
if (this.get('finalStep')) {
|
|
|
|
document.location = getUrl("/");
|
|
|
|
} else {
|
|
|
|
this.sendAction('goNext', response);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => this.animateInvalidFields())
|
|
|
|
.finally(() => this.set('saving', false));
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
quit() {
|
|
|
|
this.set('finalStep', true);
|
|
|
|
this.send('nextStep');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
};
|