webmcp
annotate framework/env/ui/_partial_load_js.lua @ 31:1cd9e69b85cb
add execute.load_chunk(args)
load_chunk loads the content of a lua file and returns the code.
It can construct the path name easily so you can put helper code in
seperate files for DRYness.
load_chunk loads the content of a lua file and returns the code.
It can construct the path name easily so you can put helper code in
seperate files for DRYness.
| author | Daniel Poelzleithner <poelzi@poelzi.org> | 
|---|---|
| date | Tue Oct 05 02:34:04 2010 +0200 (2010-10-05) | 
| parents | f3d3203cd2e4 | 
| children | 
| rev | line source | 
|---|---|
| jbe/bsw@11 | 1 --[[-- | 
| jbe@12 | 2 ui._partial_load_js( | 
| jbe@12 | 3 { | 
| jbe@12 | 4 module = module, | 
| jbe@12 | 5 view = view, | 
| jbe@12 | 6 id = id, | 
| jbe@12 | 7 params = params, | 
| jbe@12 | 8 target = target | 
| jbe@12 | 9 }, | 
| jbe@12 | 10 mode = mode | 
| jbe/bsw@11 | 11 } | 
| jbe/bsw@11 | 12 | 
| jbe@12 | 13 This function is not supposed to be called directly, but only to be used by | 
| jbe@12 | 14 ui.link{...} and ui.form{...}. | 
| jbe@12 | 15 | 
| jbe@12 | 16 It returns a JavaScript which can be used for onclick or onsubmit | 
| jbe@12 | 17 attributes in HTML to cause a partial load of contents. Behaviour differs | 
| jbe@12 | 18 for the following cases: | 
| jbe@12 | 19 a) module, view and target (and optionally id) are given as parameters | 
| jbe@12 | 20 b) neither module, view, id, nor target are given as parameters | 
| jbe@12 | 21 | 
| jbe@12 | 22 In case of a) the function will create a JavaScript which requests the | 
| jbe@12 | 23 given view (optionally with the given id and params) as JSON data. | 
| jbe@12 | 24 | 
| jbe@12 | 25 In case of b) the function will create a JavaScript requesting the view | 
| jbe@12 | 26 specified by the next outer ui.partial{...} call as JSON data. Request | 
| jbe@12 | 27 parameters specified by previous calls of add_partial_param_names({...}) | 
| jbe@12 | 28 are copied from the GET/POST parameters of the current request, while they | 
| jbe@12 | 29 can be overwritten using the "params" argument to this function. | 
| jbe/bsw@11 | 30 | 
| jbe@12 | 31 If there is no outer ui.partial{...} call in case b), then this function | 
| jbe@12 | 32 returns nil. | 
| jbe@12 | 33 | 
| jbe@12 | 34 The called URL contains "_webmcp_json_slots[]" as GET parameters to | 
| jbe@12 | 35 indicate that slot contents should be returned as a JSON object, instead of | 
| jbe@12 | 36 being inserted to a page layout. | 
| jbe@12 | 37 | 
| jbe@12 | 38 TODO: Currently the slots requested are "default", "trace" and | 
| jbe@12 | 39 "system_error". The target for the slot "default" is passed as argument to | 
| jbe@12 | 40 this function or to ui.partial{...}. The targets for the slots "trace" and | 
| jbe@12 | 41 "system_error" are "trace" and "system_error". This is hardcoded and should | 
| jbe@12 | 42 be possible to change in future. The JavaScript will fail, if there are no | 
| jbe@12 | 43 HTML elements with id's "trace" and "system_error". | 
| jbe@12 | 44 | 
| jbe@12 | 45 A mode can be passed as second parameter to this function. When this mode | 
| jbe@12 | 46 is "nil" or non existent, the function creates JavaScript code to be used | 
| jbe@12 | 47 as onclick event for normal (GET) links. When mode is set to "form_normal" | 
| jbe@12 | 48 or "form_action", the returned code can be used as onsubmit event of web | 
| jbe@12 | 49 forms. "form_normal" is used when the form calls a view, "form_action" is | 
| jbe@12 | 50 used when the form calls an action. | 
| jbe/bsw@11 | 51 | 
| jbe/bsw@11 | 52 --]]-- | 
| jbe/bsw@11 | 53 | 
| jbe/bsw@11 | 54 function ui._partial_load_js(args, mode) | 
| jbe/bsw@11 | 55 local args = args or {} | 
| jbe/bsw@11 | 56 local module | 
| jbe/bsw@11 | 57 local view | 
| jbe/bsw@11 | 58 local id | 
| jbe/bsw@11 | 59 local params = {} | 
| jbe/bsw@11 | 60 local target | 
| jbe/bsw@11 | 61 if args.view and args.target then | 
| jbe/bsw@11 | 62 module = args.module | 
| jbe/bsw@11 | 63 view = args.view | 
| jbe/bsw@11 | 64 id = args.id | 
| jbe/bsw@11 | 65 target = args.target | 
| jbe/bsw@11 | 66 elseif not args.view and not args.target then | 
| jbe/bsw@11 | 67 if not ui._partial_state then | 
| jbe/bsw@11 | 68 return nil | 
| jbe/bsw@11 | 69 end | 
| jbe/bsw@11 | 70 module = ui._partial_state.module | 
| jbe/bsw@11 | 71 view = ui._partial_state.view | 
| jbe/bsw@11 | 72 id = ui._partial_state.id | 
| jbe/bsw@11 | 73 target = ui._partial_state.target | 
| jbe/bsw@11 | 74 else | 
| jbe/bsw@11 | 75 error("Unexpected arguments passed to ui._partial_load_js{...}") | 
| jbe/bsw@11 | 76 end | 
| jbe/bsw@11 | 77 | 
| jbe/bsw@11 | 78 if ui._partial_state then | 
| jbe@12 | 79 -- TODO: do this only if args.view and args.target are unset!? | 
| jbe/bsw@11 | 80 if ui._partial_state.params then | 
| jbe/bsw@11 | 81 for key, value in pairs(ui._partial_state.params) do | 
| jbe/bsw@11 | 82 params[key] = value | 
| jbe/bsw@11 | 83 end | 
| jbe/bsw@11 | 84 end | 
| jbe/bsw@11 | 85 for param_name, dummy in pairs(ui._partial_state.param_name_hash) do | 
| jbe/bsw@11 | 86 params[param_name] = cgi.params[param_name] | 
| jbe/bsw@11 | 87 end | 
| jbe/bsw@11 | 88 end | 
| jbe/bsw@11 | 89 if args.params then | 
| jbe/bsw@11 | 90 for key, value in pairs(args.params) do | 
| jbe/bsw@11 | 91 params[key] = value | 
| jbe/bsw@11 | 92 end | 
| jbe/bsw@11 | 93 end | 
| jbe/bsw@11 | 94 local encoded_url = encode.json( | 
| jbe/bsw@11 | 95 encode.url{ | 
| jbe/bsw@11 | 96 module = module, | 
| jbe/bsw@11 | 97 view = view, | 
| jbe/bsw@11 | 98 id = id, | 
| jbe/bsw@11 | 99 params = params | 
| jbe/bsw@11 | 100 } | 
| jbe/bsw@11 | 101 ) | 
| jbe/bsw@11 | 102 | 
| jbe/bsw@11 | 103 if mode == "form_normal" then | 
| jbe/bsw@11 | 104 -- NOTE: action in "action_mode" refers to WebMCP actions, while action | 
| jbe/bsw@11 | 105 -- in "this.action" refers to the action attribute of HTML forms | 
| jbe/bsw@11 | 106 slot.put('this.action = ', encoded_url, '; ') | 
| jbe/bsw@11 | 107 end | 
| jbe/bsw@11 | 108 | 
| jbe/bsw@11 | 109 return slot.use_temporary(function() | 
| jbe/bsw@11 | 110 slot.put( | 
| jbe/bsw@11 | 111 'partialMultiLoad({', | 
| jbe/bsw@11 | 112 -- mapping: | 
| jbe/bsw@11 | 113 '"trace": "trace", "system_error": "system_error", ', | 
| jbe/bsw@11 | 114 encode.json(target), ': "default" }, ', | 
| jbe/bsw@11 | 115 -- tempLoadingContents: | 
| jbe/bsw@11 | 116 '{}, ', | 
| jbe/bsw@11 | 117 -- failureContents: | 
| jbe/bsw@11 | 118 '"error", ', | 
| jbe/bsw@11 | 119 -- url: | 
| jbe/bsw@11 | 120 (mode == "form_normal" or mode == "form_action") and ( | 
| jbe/bsw@11 | 121 'this' | 
| jbe/bsw@11 | 122 ) or ( | 
| jbe/bsw@11 | 123 encoded_url | 
| jbe/bsw@11 | 124 ), ', ', | 
| jbe/bsw@11 | 125 -- urlParams: | 
| jbe/bsw@11 | 126 '"_webmcp_json_slots[]=default&_webmcp_json_slots[]=trace&_webmcp_json_slots[]=system_error", ', | 
| jbe/bsw@11 | 127 -- postParams: | 
| jbe/bsw@11 | 128 '{}, ', | 
| jbe/bsw@11 | 129 -- successHandler: | 
| jbe/bsw@11 | 130 'function() {}, ', | 
| jbe/bsw@11 | 131 -- failureHandler: | 
| jbe/bsw@11 | 132 'function() {} ', | 
| jbe/bsw@11 | 133 '); ', | 
| jbe/bsw@11 | 134 'return false;' | 
| jbe/bsw@11 | 135 ) | 
| jbe/bsw@11 | 136 end) | 
| jbe/bsw@11 | 137 end |