Skip to content

Commit

Permalink
Scope in step's methods. Call step methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Karen committed Jul 20, 2017
1 parent 9040e53 commit 221d6a2
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 25 deletions.
11 changes: 11 additions & 0 deletions classes/Step.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,15 @@ export class Step {
get data () {
return this._data
}

get container () {
return this.parent.container.find(this.parent._step_container)
}

call (method, ...args) {
let fnc = this.methods[method] || this.interceptors[method]
if (fnc) {
return fnc.apply(this, args)
}
}
}
35 changes: 24 additions & 11 deletions classes/StepSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export class StepSystem {
this.onFinish = function () {}
this.onProgress = function () {}
this.onStepRender = function () {}

this._global_interceptors = {
isSkipGlobal: function (step) {
return step.interceptors.isSkip(step)
}
}
}

/**
Expand All @@ -35,11 +41,20 @@ export class StepSystem {
return this
}

setGlobalInterceptors (interceptors) {
this._global_interceptors = Object.assign(this._global_interceptors, interceptors)
return this
}

setHandlers (cb) {
this.commonHandlers = cb
return this
}

get global_interceptors () {
return this._global_interceptors
}

get current_step () {
return this.step(this._current_step) || null
}
Expand All @@ -61,17 +76,15 @@ export class StepSystem {
}

render (step) {
let _br = step.interceptors.beforeRender(step)
let _br = step.call('beforeRender', step)
if (!_br.status) {
if (_br.onError) _br.onError()
if (_br.onError) _br.onError.apply(step)
return this
}
this.container.find(this._step_container).html(step.template || this._container.find(`#${step.name}`).html())
this.container.find(this._step_container).attr('data-name', step.name)
this.onStepRender(step)
if (step.methods.onRender) {
step.methods.onRender(step)
}
step.call('onRender', step)
}

updateProgress () {
Expand Down Expand Up @@ -99,12 +112,12 @@ export class StepSystem {

goNext () {
let curr_step = this.current_step || {}
let next_step = curr_step.next || null
let _bn = curr_step.interceptors.beforeNext(curr_step)
let _bn = curr_step.call('beforeNext', curr_step)
if (!_bn.status) {
if (_bn.onError) _bn.onError()
if (_bn.onError) _bn.onError.apply(curr_step)
return this
}
let next_step = curr_step.next || null
if (next_step) {
this.goToStep(this.step(next_step), { from: curr_step.name })
} else {
Expand All @@ -118,9 +131,9 @@ export class StepSystem {
goBack () {
let curr_step = this.current_step || {}
let prev_step = curr_step.from || null
let _bb = curr_step.interceptors.beforeBack(curr_step) || { status: false }
let _bb = curr_step.call('beforeBack', curr_step) || { status: false }
if (!_bb.status) {
if (_bb.onError) _bb.onError()
if (_bb.onError) _bb.onError.apply(curr_step)
return this
}
if (prev_step) {
Expand All @@ -133,7 +146,7 @@ export class StepSystem {
}

goToStep (step, params = {}) {
let is_skip = step.interceptors.isSkip(step)
let is_skip = this.global_interceptors.isSkipGlobal(step)
if (is_skip) {
step = this.step(step.next)
}
Expand Down
7 changes: 6 additions & 1 deletion example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ window.app = new StepSystem({
step.data.kek = 'kek'
return { status: true }
},
onRender: (step) => { }
onRender: (step) => {
step.call('anyOfInternalMethods')
},
anyOfInternalMethods: () => {
alert ('Hey there')
}
}
}))

Expand Down
61 changes: 49 additions & 12 deletions example/dist/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/dist/app.js.map

Large diffs are not rendered by default.

0 comments on commit 221d6a2

Please sign in to comment.