webmcp

view framework/env/ui/_partial_load_js.lua @ 12:f3d3203cd2e4

Documentation for partial loading added

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

Impressum / About Us