mod_adhoc_dataforms_demo/mod_adhoc_dataforms_demo.lua
changeset 3562 22587eb2d87c
parent 3561 54b4b020de4c
child 3918 f5caacd475c4
equal deleted inserted replaced
3561:54b4b020de4c 3562:22587eb2d87c
   119 
   119 
   120 module:provides("adhoc",
   120 module:provides("adhoc",
   121 	adhoc_new("Dataforms Demo",
   121 	adhoc_new("Dataforms Demo",
   122 		"xmpp:zash.se/mod_adhoc_dataforms_demo#form",
   122 		"xmpp:zash.se/mod_adhoc_dataforms_demo#form",
   123 		adhoc_util.new_simple_form(form, handler)));
   123 		adhoc_util.new_simple_form(form, handler)));
       
   124 
       
   125 
       
   126 local function multi_step_command(_, data, state)
       
   127 
       
   128 	if data.action == "cancel" then
       
   129 		return { status = "canceled" };
       
   130 	elseif data.action == "complete" then
       
   131 		return {
       
   132 			status = "completed",
       
   133 			info = "State was:\n"
       
   134 				.. serialization.serialize(state, { fatal = false }),
       
   135 		};
       
   136 	end
       
   137 	state = state or { step = 1, forms = { } };
       
   138 
       
   139 	if data.action == "next" then
       
   140 		state.step = state.step + 1;
       
   141 	elseif data.action == "prev" then
       
   142 		state.step = math.max(state.step - 1, 1);
       
   143 	end
       
   144 
       
   145 	local current_form = state.forms[state.step]
       
   146 	if not current_form then
       
   147 		current_form = {
       
   148 			title = string.format("Step %d", state.step);
       
   149 			instructions = state.step == 1 and "Here's a form." or "Here's another form.";
       
   150 		};
       
   151 		local already_selected = {};
       
   152 		for _ = 1, math.random(1, 5) do
       
   153 			local random
       
   154 			repeat
       
   155 				random = math.random(2, #form);
       
   156 			until not already_selected[random]
       
   157 			table.insert(current_form, form[random]);
       
   158 		end
       
   159 		state.forms[state.step] = dataforms.new(current_form);
       
   160 	end
       
   161 
       
   162 	local next_step = {
       
   163 		status = "executing",
       
   164 		form = current_form,
       
   165 		actions = {
       
   166 			"next", "complete"
       
   167 		},
       
   168 	};
       
   169 	if state.step > 1 then
       
   170 		table.insert(next_step.actions, 1, "prev");
       
   171 	end
       
   172 	return next_step, state;
       
   173 end
       
   174 
       
   175 module:provides("adhoc",
       
   176 	adhoc_new("Multi-step command demo",
       
   177 		"xmpp:zash.se/mod_adhoc_dataforms_demo#multi",
       
   178 		multi_step_command));
       
   179