jbe/bsw@11: --[[-- jbe@12: ui._partial_load_js( jbe@12: { jbe@12: module = module, jbe@12: view = view, jbe@12: id = id, jbe@12: params = params, jbe@12: target = target jbe@12: }, jbe@12: mode = mode jbe/bsw@11: } jbe/bsw@11: jbe@12: This function is not supposed to be called directly, but only to be used by jbe@12: ui.link{...} and ui.form{...}. jbe@12: jbe@12: It returns a JavaScript which can be used for onclick or onsubmit jbe@12: attributes in HTML to cause a partial load of contents. Behaviour differs jbe@12: for the following cases: jbe@12: a) module, view and target (and optionally id) are given as parameters jbe@12: b) neither module, view, id, nor target are given as parameters jbe@12: jbe@12: In case of a) the function will create a JavaScript which requests the jbe@12: given view (optionally with the given id and params) as JSON data. jbe@12: jbe@12: In case of b) the function will create a JavaScript requesting the view jbe@12: specified by the next outer ui.partial{...} call as JSON data. Request jbe@12: parameters specified by previous calls of add_partial_param_names({...}) jbe@12: are copied from the GET/POST parameters of the current request, while they jbe@12: can be overwritten using the "params" argument to this function. jbe/bsw@11: jbe@12: If there is no outer ui.partial{...} call in case b), then this function jbe@12: returns nil. jbe@12: jbe@12: The called URL contains "_webmcp_json_slots[]" as GET parameters to jbe@12: indicate that slot contents should be returned as a JSON object, instead of jbe@12: being inserted to a page layout. jbe@12: jbe@12: TODO: Currently the slots requested are "default", "trace" and jbe@12: "system_error". The target for the slot "default" is passed as argument to jbe@12: this function or to ui.partial{...}. The targets for the slots "trace" and jbe@12: "system_error" are "trace" and "system_error". This is hardcoded and should jbe@12: be possible to change in future. The JavaScript will fail, if there are no jbe@12: HTML elements with id's "trace" and "system_error". jbe@12: jbe@12: A mode can be passed as second parameter to this function. When this mode jbe@12: is "nil" or non existent, the function creates JavaScript code to be used jbe@12: as onclick event for normal (GET) links. When mode is set to "form_normal" jbe@12: or "form_action", the returned code can be used as onsubmit event of web jbe@12: forms. "form_normal" is used when the form calls a view, "form_action" is jbe@12: used when the form calls an action. jbe/bsw@11: jbe/bsw@11: --]]-- jbe/bsw@11: jbe/bsw@11: function ui._partial_load_js(args, mode) jbe/bsw@11: local args = args or {} jbe/bsw@11: local module jbe/bsw@11: local view jbe/bsw@11: local id jbe/bsw@11: local params = {} jbe/bsw@11: local target jbe/bsw@11: if args.view and args.target then jbe/bsw@11: module = args.module jbe/bsw@11: view = args.view jbe/bsw@11: id = args.id jbe/bsw@11: target = args.target jbe/bsw@11: elseif not args.view and not args.target then jbe/bsw@11: if not ui._partial_state then jbe/bsw@11: return nil jbe/bsw@11: end jbe/bsw@11: module = ui._partial_state.module jbe/bsw@11: view = ui._partial_state.view jbe/bsw@11: id = ui._partial_state.id jbe/bsw@11: target = ui._partial_state.target jbe/bsw@11: else jbe/bsw@11: error("Unexpected arguments passed to ui._partial_load_js{...}") jbe/bsw@11: end jbe/bsw@11: jbe/bsw@11: if ui._partial_state then jbe@12: -- TODO: do this only if args.view and args.target are unset!? jbe/bsw@11: if ui._partial_state.params then jbe/bsw@11: for key, value in pairs(ui._partial_state.params) do jbe/bsw@11: params[key] = value jbe/bsw@11: end jbe/bsw@11: end jbe/bsw@11: for param_name, dummy in pairs(ui._partial_state.param_name_hash) do jbe/bsw@11: params[param_name] = cgi.params[param_name] jbe/bsw@11: end jbe/bsw@11: end jbe/bsw@11: if args.params then jbe/bsw@11: for key, value in pairs(args.params) do jbe/bsw@11: params[key] = value jbe/bsw@11: end jbe/bsw@11: end jbe/bsw@11: local encoded_url = encode.json( jbe/bsw@11: encode.url{ jbe/bsw@11: module = module, jbe/bsw@11: view = view, jbe/bsw@11: id = id, jbe/bsw@11: params = params jbe/bsw@11: } jbe/bsw@11: ) jbe/bsw@11: jbe/bsw@11: if mode == "form_normal" then jbe/bsw@11: -- NOTE: action in "action_mode" refers to WebMCP actions, while action jbe/bsw@11: -- in "this.action" refers to the action attribute of HTML forms jbe/bsw@11: slot.put('this.action = ', encoded_url, '; ') jbe/bsw@11: end jbe/bsw@11: jbe/bsw@11: return slot.use_temporary(function() jbe/bsw@11: slot.put( jbe/bsw@11: 'partialMultiLoad({', jbe/bsw@11: -- mapping: jbe/bsw@11: '"trace": "trace", "system_error": "system_error", ', jbe/bsw@11: encode.json(target), ': "default" }, ', jbe/bsw@11: -- tempLoadingContents: jbe/bsw@11: '{}, ', jbe/bsw@11: -- failureContents: jbe/bsw@11: '"error", ', jbe/bsw@11: -- url: jbe/bsw@11: (mode == "form_normal" or mode == "form_action") and ( jbe/bsw@11: 'this' jbe/bsw@11: ) or ( jbe/bsw@11: encoded_url jbe/bsw@11: ), ', ', jbe/bsw@11: -- urlParams: jbe/bsw@11: '"_webmcp_json_slots[]=default&_webmcp_json_slots[]=trace&_webmcp_json_slots[]=system_error", ', jbe/bsw@11: -- postParams: jbe/bsw@11: '{}, ', jbe/bsw@11: -- successHandler: jbe/bsw@11: 'function() {}, ', jbe/bsw@11: -- failureHandler: jbe/bsw@11: 'function() {} ', jbe/bsw@11: '); ', jbe/bsw@11: 'return false;' jbe/bsw@11: ) jbe/bsw@11: end) jbe/bsw@11: end