Restructure callbacks

This commit is contained in:
YNH Webdev
2013-09-22 18:30:48 +02:00
parent f360e8a867
commit 3a9ede6e8c
10 changed files with 2023 additions and 722 deletions

View File

@@ -135,7 +135,7 @@ feature -- Execution
local
f: WSF_FILE_RESPONSE
do
create f.make_html (name)
create f.make (name)
response.send (f)
end

View File

@@ -20,10 +20,10 @@ feature
create container.make_multi_control ("container")
container.add_class ("container")
create navbar.make_navbar ("Sample Page")
navbar.add_element (create {WSF_BASIC_CONTROL}.make_with_body ("a", "href=%"/%"", "Home"))
navbar.add_element (create {WSF_BASIC_CONTROL}.make_with_body ("a", "href=%"/grid%"", "Grid"))
navbar.add_element (create {WSF_BASIC_CONTROL}.make_with_body ("a", "href=%"/repeater%"", "Repeater"))
navbar.add_element_right (create {WSF_BASIC_CONTROL}.make_with_body ("a", "href=%"#%"", "About"))
navbar.nav.add_control (create {WSF_BASIC_CONTROL}.make_with_body ("a", "href=%"/%"", "Home"))
navbar.nav.add_control (create {WSF_BASIC_CONTROL}.make_with_body ("a", "href=%"/grid%"", "Grid"))
navbar.nav.add_control (create {WSF_BASIC_CONTROL}.make_with_body ("a", "href=%"/repeater%"", "Repeater"))
navbar.nav_right.add_control (create {WSF_BASIC_CONTROL}.make_with_body ("a", "href=%"#%"", "About"))
container.add_control (navbar)
control := container
end

File diff suppressed because one or more lines are too long

View File

@@ -1,3 +1,4 @@
#IMPORTANT PLEASE COMPILE WITH:: coffee -cbw widget.coffee
cache = {}
template = tmpl = (str, data) ->
# Simple JavaScript Templating
@@ -10,20 +11,20 @@ Mini =
{
render:template(t)
}
trigger_callback = (control_name,event,event_parameter)->
$.ajax
data:
control_name: control_name
event: event
event_parameter: event_parameter
states: JSON.stringify(window.states)
cache: no
.done (new_states)->
#Update all classes
for name,state of new_states
controls[name]?.update(state)
return
build_control = (control_name, state, control)->
$el = control.$el.find('[data-name='+control_name+']')
#get control type
type = $el.data('type')
#create class
typeclass = null
try
typeclass = eval(type)
catch e
typeclass = WSF_CONTROL
if type? and typeclass?
return new typeclass(control, $el, control_name, state)
return null
class WSF_VALIDATOR
constructor: (@parent_control, @settings)->
@@ -55,21 +56,71 @@ class WSF_MAX_VALIDATOR extends WSF_VALIDATOR
val = @parent_control.value()
return (val.length<=@settings.max)
validatormap =
"WSF_REGEXP_VALIDATOR":WSF_REGEXP_VALIDATOR
"WSF_MIN_VALIDATOR":WSF_MIN_VALIDATOR
"WSF_MAX_VALIDATOR":WSF_MAX_VALIDATOR
class WSF_CONTROL
constructor: (@control_name, @$el)->
constructor: (@parent_control, @$el, @control_name, @fullstate)->
@state = @fullstate.state
@load_subcontrols()
return
load_subcontrols: ()->
if @fullstate.controls?
@controls=(build_control(control_name, state, @) for control_name, state of @fullstate.controls)
else
@controls = []
attach_events: ()->
console.log "Attached #{@control_name}"
for control in @controls
if control?
control.attach_events()
return
update: (state)->
return
get_state: ()->
@state
get_control_states:()->
result = {}
for control in @controls
if control?
result[control.control_name]=control.get_full_state()
result
get_full_state: ()->
{"state":@get_state(),"controls":@get_control_states()}
process_update: (new_states)->
if new_states[@control_name]?
@update(new_states[@control_name])
for control in @controls
if control?
control.process_update(new_states)
get_context_state : ()->
if @parent_control?
return @parent_control.get_context_state()
return @get_full_state()
trigger_callback: (control_name,event,event_parameter)->
if @parent_control?
return @parent_control.trigger_callback(control_name,event,event_parameter)
self = @
$.ajax
type: 'POST',
url: '?' + $.param
control_name: control_name
event: event
data:
JSON.stringify(@get_full_state())
processData: false,
contentType: 'application/json',
cache: no
.done (new_states)->
#Update all classes
self.process_update(new_states)
#Simple event listener
#subscribe to an event
@@ -89,36 +140,45 @@ class WSF_CONTROL
ev.callback.call(ev.context)
return @
class WSF_PAGE_CONTROL extends WSF_CONTROL
constructor: (@fullstate)->
@state = @fullstate.state
@parent_control=null
@$el = $('[data-name='+@state.id+']')
@control_name = @state.id
@load_subcontrols()
controls = {}
class WSF_BUTTON_CONTROL extends WSF_CONTROL
attach_events: ()->
super
self = @
@$el.click (e)->
e.preventDefault()
self.click()
click: ()->
if window.states[@control_name]['callback_click']
trigger_callback(@control_name, 'click')
if @state['callback_click']
@trigger_callback(@control_name, 'click')
update: (state) ->
if state.text?
window.states[@control_name]['text'] = state.text
@state['text'] = state.text
@$el.text(state.text)
class WSF_INPUT_CONTROL extends WSF_CONTROL
attach_events: ()->
super
self = @
@$el.change ()->
self.change()
change: ()->
#update local state
window.states[@control_name]['text'] = @$el.val()
if window.states[@control_name]['callback_change']
trigger_callback(@control_name, 'change')
@state['text'] = @$el.val()
if @state['callback_change']
@trigger_callback(@control_name, 'change')
@trigger('change')
value:()->
@@ -126,28 +186,31 @@ class WSF_INPUT_CONTROL extends WSF_CONTROL
update: (state) ->
if state.text?
window.states[@control_name]['text'] = state.text
@state['text'] = state.text
@$el.val(state.text)
class WSF_TEXTAREA_CONTROL extends WSF_INPUT_CONTROL
class WSF_AUTOCOMPLETE_CONTROL extends WSF_INPUT_CONTROL
attach_events: () ->
super
self = @
@$el.typeahead({
name: @control_name
template: window.states[@control_name]['template']
template: @state['template']
engine: Mini
remote:
url:""
replace: (url, uriEncodedQuery) ->
window.states[self.control_name]['text'] = self.$el.val()
self.state['text'] = self.$el.val()
'?' + $.param
control_name: self.control_name
event: 'autocomplete'
states: JSON.stringify(window.states)
states: JSON.stringify(self.get_context_state())
filter: (parsedResponse) ->
parsedResponse[self.control_name]['suggestions']
fn: ()->
self.trigger_callback(self.control_name, 'autocomplete')
})
@$el.on 'typeahead:closed',()->
self.change()
@@ -156,16 +219,17 @@ class WSF_AUTOCOMPLETE_CONTROL extends WSF_INPUT_CONTROL
class WSF_CHECKBOX_CONTROL extends WSF_CONTROL
attach_events: ()->
super
self = @
@checked_value = window.states[@control_name]['checked_value']
@checked_value = @state['checked_value']
@$el.change ()->
self.change()
change: ()->
#update local state
window.states[@control_name]['checked'] = @$el.is(':checked')
if window.states[@control_name]['callback_change']
trigger_callback(@control_name, 'change')
@state['checked'] = @$el.is(':checked')
if @state['callback_change']
@trigger_callback(@control_name, 'change')
@trigger('change')
value:()->
@@ -173,23 +237,25 @@ class WSF_CHECKBOX_CONTROL extends WSF_CONTROL
update: (state) ->
if state.text?
window.states[@control_name]['checked'] = state.checked
@state['checked'] = state.checked
@$el.prop('checked',state.checked)
class WSF_FORM_ELEMENT_CONTROL extends WSF_CONTROL
attach_events: ()->
super
self = @
@value_control = controls[window.states[@control_name]['value_control']]
@value_control = @controls[0]
if @value_control?
#subscribe to change event on value_control
@value_control.on('change',@change,@)
@serverside_validator = false
#Initialize validators
@validators = []
for validator in window.states[@control_name]['validators']
if validatormap[validator.name]?
@validators.push new validatormap[validator.name](@,validator)
else
for validator in @state['validators']
try
validatorclass = eval(validator.name)
@validators.push new validatorclass(@,validator)
catch e
#Use serverside validator if no js implementation
@serverside_validator = true
return
@@ -203,7 +269,7 @@ class WSF_FORM_ELEMENT_CONTROL extends WSF_CONTROL
@showerror("")
#If there is validator which is not implemented in js ask server to validate
if @serverside_validator
trigger_callback(@control_name, 'validate')
@trigger_callback(@control_name, 'validate')
return
showerror: (message)->
@@ -228,19 +294,16 @@ class WSF_HTML_CONTROL extends WSF_CONTROL
update: (state) ->
if state.html?
window.states[@control_name]['html'] = state.html
@state['html'] = state.html
@$el.html(state.html)
class WSF_CHECKBOX_LIST_CONTROL extends WSF_CONTROL
attach_events: ()->
self = @
@subcontrols = []
super
#Listen to events of subelements and forward them
for name,control of controls
if @$el.has(control.$el).length > 0
@subcontrols.push(control)
control.on('change',@change,@)
for control in @controls
control.on('change',@change,@)
return
change:()->
@@ -248,7 +311,7 @@ class WSF_CHECKBOX_LIST_CONTROL extends WSF_CONTROL
value:()->
result = []
for subc in @subcontrols
for subc in @controls
if subc.value()
result.push(subc.checked_value)
return result
@@ -256,20 +319,22 @@ class WSF_CHECKBOX_LIST_CONTROL extends WSF_CONTROL
class WSF_PROGRESS_CONTROL extends WSF_CONTROL
attach_events:() ->
super
self = @
runfetch= ()->
self.fetch()
setInterval(runfetch, 5000)
fetch: ()->
trigger_callback(@control_name, 'progress_fetch')
@trigger_callback(@control_name, 'progress_fetch')
update: (state)->
if state.progress?
window.states[@control_name]['progress'] = state.progress
@state['progress'] = state.progress
@$el.children('.progress-bar').attr('aria-valuenow', state.progress).width(state.progress + '%')
class WSF_PAGINATION_CONTROL extends WSF_CONTROL
attach_events: ()->
self = @
@$el.on 'click', 'a', (e)->
@@ -279,11 +344,11 @@ class WSF_PAGINATION_CONTROL extends WSF_CONTROL
click: (e)->
nr = $(e.target).data('nr')
if nr == "next"
trigger_callback(@control_name, "next")
@trigger_callback(@control_name, "next")
else if nr == "prev"
trigger_callback(@control_name, "prev")
@trigger_callback(@control_name, "prev")
else
trigger_callback(@control_name, "goto", nr)
@trigger_callback(@control_name, "goto", nr)
update: (state) ->
if state._html?
@@ -291,49 +356,25 @@ class WSF_PAGINATION_CONTROL extends WSF_CONTROL
class WSF_GRID_CONTROL extends WSF_CONTROL
attach_events: ()->
super
self = @
update: (state) ->
if state.datasource?
window.states[@control_name]['datasource'] = state.datasource
@state['datasource'] = state.datasource
if state._body?
@$el.find('tbody').html(state._body)
class WSF_REPEATER_CONTROL extends WSF_CONTROL
attach_events: ()->
super
self = @
update: (state) ->
if state.datasource?
window.states[@control_name]['datasource'] = state.datasource
@state['datasource'] = state.datasource
if state._body?
@$el.find('.repeater_content').html(state._body)
console.log state._body
#map class name to effective class
typemap =
"WSF_BUTTON_CONTROL": WSF_BUTTON_CONTROL
"WSF_INPUT_CONTROL": WSF_INPUT_CONTROL
"WSF_TEXTAREA_CONTROL": WSF_TEXTAREA_CONTROL
"WSF_AUTOCOMPLETE_CONTROL": WSF_AUTOCOMPLETE_CONTROL
"WSF_CHECKBOX_CONTROL": WSF_CHECKBOX_CONTROL
"WSF_FORM_ELEMENT_CONTROL": WSF_FORM_ELEMENT_CONTROL
"WSF_HTML_CONTROL": WSF_HTML_CONTROL
"WSF_CHECKBOX_LIST_CONTROL": WSF_CHECKBOX_LIST_CONTROL
"WSF_PROGRESS_CONTROL": WSF_PROGRESS_CONTROL
"WSF_PAGINATION_CONTROL": WSF_PAGINATION_CONTROL
"WSF_GRID_CONTROL": WSF_GRID_CONTROL
"WSF_REPEATER_CONTROL":WSF_REPEATER_CONTROL
#create a js class for each control
for name,state of window.states
#find control DOM element
$el = $('[data-name='+name+']')
#get control type
type = $el.data('type')
#create class
if type? and typemap[type]?
controls[name]=new typemap[type](name,$el)
for name,state of window.states
controls[name]?.attach_events()

File diff suppressed because it is too large Load Diff