2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00
books/setup/index.js

136 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-03-28 10:26:24 +00:00
const frappe = require('frappejs');
const utils = require('frappejs/client/ui/utils');
const slideConfigs = require('./config');
const Tree = require('frappejs/client/ui/tree');
const FormLayout = require('frappejs/client/view/formLayout');
module.exports = class SetupWizard {
constructor({postSetup = () => {}}) {
this.slideList = [];
this.indicatorList = [];
this.footerLinks = {};
2018-03-28 11:15:43 +00:00
this.currentIndex = 0;
2018-03-28 10:26:24 +00:00
this.data = {};
this.postSetup = postSetup;
2018-03-28 11:15:43 +00:00
this.make();
2018-03-28 10:26:24 +00:00
2018-03-28 11:15:43 +00:00
this.showSlide(this.currentIndex);
}
2018-03-28 10:26:24 +00:00
2018-03-28 11:15:43 +00:00
make() {
let body = document.querySelector('body');
this.container = frappe.ui.add('form', 'setup-container container', body);
2018-03-28 10:26:24 +00:00
this.$indicators = frappe.ui.add('div', 'indicators vertical-margin align-center', this.container);
2018-03-28 11:15:43 +00:00
this.makeSlides();
this.makeLinks();
}
makeSlides() {
2018-03-28 10:26:24 +00:00
slideConfigs.forEach(config => {
this.formLayout = new FormLayout(config);
this.slideList.push(this.formLayout);
let form = this.formLayout.form;
2018-03-28 11:15:43 +00:00
this.container.appendChild(form);
2018-03-28 10:26:24 +00:00
2018-03-28 11:15:43 +00:00
let title = frappe.ui.create('h3', {
className: 'text-extra-muted',
innerHTML: config.title
})
2018-03-28 10:26:24 +00:00
form.insertBefore(title, form.firstChild);
2018-03-28 11:15:43 +00:00
let indicator = frappe.ui.create('span', {
inside: this.$indicators,
className: 'indicator gray'
})
2018-03-28 10:26:24 +00:00
this.indicatorList.push(indicator);
});
2018-03-28 11:15:43 +00:00
}
2018-03-28 10:26:24 +00:00
2018-03-28 11:15:43 +00:00
makeLinks() {
2018-03-28 10:26:24 +00:00
this.linkArea = frappe.ui.add('div', 'setup-link-area align-right', this.container);
// this.formLayout.on('change', () => {
// const show = this.doc._dirty && !this.doc.submitted;
// this.saveButton.classList.toggle('hide', !show);
// });
2018-03-28 11:15:43 +00:00
this.getFooterLinks().map(link => {
2018-03-28 10:26:24 +00:00
let $link = utils.addLink(link.label, this.linkArea, () => {
2018-03-28 11:15:43 +00:00
this.buildData();
2018-03-28 10:26:24 +00:00
link.action(this.data);
});
this.footerLinks[link.name] = $link;
})
}
2018-03-28 11:15:43 +00:00
buildData() {
this.data = {};
this.slideList.forEach(slide => {
Object.assign(this.data, slide.doc);
});
2018-03-28 10:26:24 +00:00
}
showSlide(index) {
utils.activate(this.container, this.slideList[index].form, 'form-body', 'active');
this.slideList[index].controlList[0].input.blur();
this.activateIndicator(index);
2018-03-28 11:15:43 +00:00
this.showFooterLinks(index);
2018-03-28 10:26:24 +00:00
this.currentIndex = index;
}
2018-03-28 11:15:43 +00:00
prevSlide() {
this.showSlide(this.currentIndex - 1);
}
nextSlide() {
this.showSlide(this.currentIndex + 1);
}
2018-03-28 10:26:24 +00:00
activateIndicator(index) {
this.indicatorList.forEach(indicator => {indicator.classList.add('gray')});
let indicator = this.indicatorList[index];
utils.activate(this.$indicators, indicator, 'gray', 'blue', index);
frappe.ui.removeClass(indicator, 'gray');
indicator.classList.remove('gray');
}
2018-03-28 11:15:43 +00:00
showFooterLinks(index) {
let mat = [1, 1, 0]
2018-03-28 10:26:24 +00:00
if(index === 0) {
2018-03-28 11:15:43 +00:00
mat = [0, 1, 0];
2018-03-28 10:26:24 +00:00
} else if (index === this.slideList.length - 1) {
2018-03-28 11:15:43 +00:00
mat = [1, 0, 1];
2018-03-28 10:26:24 +00:00
}
2018-03-28 11:15:43 +00:00
this.showHideLinks(mat);
}
2018-03-28 10:26:24 +00:00
2018-03-28 11:15:43 +00:00
showHideLinks(matrix = [1, 1, 0]) {
let linkNames = this.getFooterLinks().map(link => link.name);
matrix.forEach((value, i) => {
const fn = value ? 'remove' : 'add';
this.footerLinks[linkNames[i]].classList[fn]('hide');
});
}
getFooterLinks() {
return [
{
label: 'Prev', name: 'prev',
action: this.prevSlide.bind(this)
},
{
label: 'Next', name: 'next',
action: this.nextSlide.bind(this)
},
{
label: 'Complete', name: 'complete',
action: this.postSetup.bind(this)
}
];
2018-03-28 10:26:24 +00:00
}
}