Commit ef31cd51 authored by Vivek's avatar Vivek

erp5_gadget_interface_validator: used simpler router logic.

parent 4d602a6a
...@@ -205,16 +205,12 @@ ...@@ -205,16 +205,12 @@
if(gadget_list.length > 0) {\n if(gadget_list.length > 0) {\n
var page_type = fetchPageType(gadget_list[0]);\n var page_type = fetchPageType(gadget_list[0]);\n
return gadget.redirect({\n return gadget.redirect({\n
command: \'change\',\n
options: {\n
page: page_type,\n page: page_type,\n
appcache_url: appcache_url\n appcache_url: appcache_url\n
}\n
});\n });\n
} else {\n } else {\n
return gadget.redirect({\n return gadget.redirect({\n
command: \'change\',\n found: false\n
options: {found: false}\n
});\n });\n
}\n }\n
});\n });\n
...@@ -365,7 +361,7 @@ ...@@ -365,7 +361,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>946.46361.42674.38161</string> </value> <value> <string>946.54877.22908.40635</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -383,7 +379,7 @@ ...@@ -383,7 +379,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1446476000.92</float> <float>1446717901.46</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -299,8 +299,7 @@ ...@@ -299,8 +299,7 @@
return validateAppGadgetList(gadget, gadget_list);\n return validateAppGadgetList(gadget, gadget_list);\n
}, function(error) {\n }, function(error) {\n
return gadget.redirect({\n return gadget.redirect({\n
command: \'change\',\n found: false\n
options: {found: false, page: \'form\'}\n
});\n });\n
});\n });\n
})\n })\n
...@@ -473,7 +472,7 @@ ...@@ -473,7 +472,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>946.49108.59205.8413</string> </value> <value> <string>946.54878.32293.23654</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -491,7 +490,7 @@ ...@@ -491,7 +490,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1446476069.46</float> <float>1446717974.43</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -101,437 +101,14 @@ ...@@ -101,437 +101,14 @@
<key> <string>text_content</string> </key> <key> <string>text_content</string> </key>
<value> <string encoding="cdata"><![CDATA[ <value> <string encoding="cdata"><![CDATA[
/*global window, rJS, RSVP, loopEventListener, document, jIO */\n /*global window, rJS */\n
/*jslint nomen: true, indent: 2 */\n /*jslint nomen: true, indent: 2 */\n
(function (window, rJS, RSVP, loopEventListener, document, jIO) {\n (function (window, rJS) {\n
"use strict";\n "use strict";\n
\n \n
// Keep reference of the latest allDocs params which reach to this view\n var MAIN_PAGE_PREFIX = "gadget_interface_validator_",\n
// var SELECTION_KEY = "s",\n DEFAULT_PAGE = "form",\n
// Keep reference in the global navigation pattern\n REDIRECT_TIMEOUT = 5000;\n
// HISTORY KEY = "h"\n
// Current display parameter\n
// DISPLAY KEY = "d"\n
var PREVIOUS_KEY = "p",\n
NEXT_KEY = "n",\n
PREFIX_DISPLAY = "/",\n
PREFIX_COMMAND = "!",\n
// PREFIX_ERROR = "?",\n
COMMAND_DISPLAY_STATE = "display",\n
COMMAND_CHANGE_STATE = "change",\n
COMMAND_INDEX_STATE = "index",\n
COMMAND_SELECTION_PREVIOUS = "selection_previous",\n
COMMAND_SELECTION_NEXT = "selection_next",\n
COMMAND_HISTORY_PREVIOUS = "history_previous",\n
COMMAND_PUSH_HISTORY = "push_history",\n
VALID_URL_COMMAND_DICT = {},\n
DEFAULT_PAGE = \'form\';\n
VALID_URL_COMMAND_DICT[COMMAND_DISPLAY_STATE] = null;\n
VALID_URL_COMMAND_DICT[COMMAND_CHANGE_STATE] = null;\n
VALID_URL_COMMAND_DICT[COMMAND_INDEX_STATE] = null;\n
VALID_URL_COMMAND_DICT[COMMAND_SELECTION_PREVIOUS] = null;\n
VALID_URL_COMMAND_DICT[COMMAND_SELECTION_NEXT] = null;\n
VALID_URL_COMMAND_DICT[COMMAND_HISTORY_PREVIOUS] = null;\n
VALID_URL_COMMAND_DICT[COMMAND_PUSH_HISTORY] = null;\n
\n
//////////////////////////////////////////////////////////////////\n
// Change URL functions\n
//////////////////////////////////////////////////////////////////\n
function changeState(hash) {\n
// window.location = hash;\n
return window.location.replace(hash);\n
}\n
\n
function synchronousChangeState(hash) {\n
changeState(hash);\n
// prevent returning unexpected response\n
// wait for the hash change to occur\n
// fail if nothing happens\n
return RSVP.timeout(\'5000\');\n
}\n
\n
//////////////////////////////////////////////////////////////////\n
// Selection functions\n
//////////////////////////////////////////////////////////////////\n
function getSelection(gadget, selection_id) {\n
return gadget.props.jio_gadget.get(selection_id)\n
.push(function (result) {\n
return result.data;\n
});\n
}\n
\n
function getHistory(gadget, history_id) {\n
var previous_id;\n
return gadget.props.jio_gadget.get(history_id)\n
.push(function (history) {\n
previous_id = history.previous_history_id;\n
return gadget.props.jio_gadget.get(history.options_id);\n
})\n
.push(function (result) {\n
return [result, previous_id];\n
}, function (error) {\n
// XXX Check if 404\n
if ((error instanceof jIO.util.jIOError) &&\n
(error.status_code === 404)) {\n
return [{data: {}}, undefined];\n
}\n
throw error;\n
});\n
}\n
\n
function addHistory(gadget, options, previous_selection_id) {\n
var options_blob = {\n
type: "options",\n
data: options\n
},\n
blob_id;\n
\n
return gadget.props.jio_gadget.post(options_blob)\n
.push(function (result) {\n
blob_id = result;\n
return gadget.props.jio_gadget.get(previous_selection_id)\n
.push(undefined, function () {\n
previous_selection_id = undefined;\n
});\n
})\n
.push(function () {\n
var data_history = {\n
type: "history",\n
options_id: blob_id,\n
previous_history_id: previous_selection_id\n
};\n
return gadget.props.jio_gadget.post(data_history);\n
})\n
.push(function (id) {\n
return id;\n
});\n
}\n
\n
function addSelection(gadget, options) {\n
var data_blob = {\n
type: "selection",\n
data: options\n
};\n
return gadget.props.jio_gadget.post(data_blob);\n
}\n
\n
//////////////////////////////////////////////////////////////////\n
// Build URL functions\n
//////////////////////////////////////////////////////////////////\n
function getCommandUrlFor(gadget, command, options) {\n
var result = "#" + PREFIX_COMMAND + (command || ""),\n
prefix = "?",\n
key,\n
tmp_dict;\n
tmp_dict = gadget.props.options;\n
for (key in tmp_dict) {\n
if (tmp_dict.hasOwnProperty(key)) {\n
if (tmp_dict[key] === undefined) {\n
tmp_dict[key] = \'\';\n
}\n
result += prefix + PREVIOUS_KEY + "." + encodeURIComponent(key) + "=" + encodeURIComponent(tmp_dict[key]);\n
prefix = "&";\n
}\n
}\n
for (key in options) {\n
if (options.hasOwnProperty(key)) {\n
if (options[key] === undefined) {\n
options[key] = \'\';\n
}\n
result += prefix + NEXT_KEY + "." + encodeURIComponent(key) + "=" + encodeURIComponent(options[key]);\n
prefix = "&";\n
}\n
}\n
return result;\n
}\n
\n
function getDisplayUrlFor(jio_key, options) {\n
var prefix = \'?\',\n
result,\n
key;\n
result = "#" + PREFIX_DISPLAY + (jio_key || "");\n
for (key in options) {\n
if (options.hasOwnProperty(key) && options[key] !== undefined) {\n
// Don\'t keep empty values\n
result += prefix + encodeURIComponent(key) + "=" + encodeURIComponent(options[key]);\n
prefix = \'&\';\n
}\n
}\n
return result;\n
}\n
\n
//////////////////////////////////////////////////////////////////\n
// exec command functions\n
//////////////////////////////////////////////////////////////////\n
function execDisplayCommand(next_options) {\n
// console.warn(command_options);\n
var jio_key = next_options.jio_key;\n
delete next_options.jio_key;\n
// XXX Implement history management\n
return synchronousChangeState(\n
getDisplayUrlFor(jio_key, next_options)\n
);\n
}\n
\n
function execChangeCommand(previous_options, next_options) {\n
var key,\n
jio_key;\n
for (key in previous_options) {\n
if (previous_options.hasOwnProperty(key)) {\n
if (!next_options.hasOwnProperty(key)) {\n
next_options[key] = previous_options[key];\n
}\n
}\n
}\n
for (key in next_options) {\n
if (next_options.hasOwnProperty(key)) {\n
if (!next_options[key]) {\n
delete next_options[key];\n
}\n
}\n
}\n
jio_key = next_options.jio_key;\n
delete next_options.jio_key;\n
return synchronousChangeState(\n
getDisplayUrlFor(jio_key, next_options)\n
);\n
}\n
\n
function execIndexCommand(gadget, previous_options, next_options) {\n
var jio_key = next_options.jio_key,\n
selection_options = {};\n
delete next_options.jio_key;\n
// selection_options.index = next_options.index;\n
selection_options.query = next_options.query;\n
selection_options.list_method_template = next_options.list_method_template;\n
// Store selection in local DB\n
return addSelection(gadget, selection_options)\n
.push(function (id) {\n
next_options.selection = id;\n
// XXX Implement history management\n
return addHistory(gadget, previous_options);\n
})\n
.push(function (id) {\n
next_options.history = id;\n
return synchronousChangeState(\n
getDisplayUrlFor(jio_key, next_options)\n
);\n
});\n
}\n
\n
function execPushHistoryCommand(gadget, previous_options, next_options) {\n
var jio_key = next_options.jio_key;\n
delete next_options.jio_key;\n
// XXX Hack to support create dialog\n
delete previous_options.view;\n
delete previous_options.page;\n
return addHistory(gadget, previous_options)\n
.push(function (id) {\n
next_options.history = id;\n
return synchronousChangeState(\n
getDisplayUrlFor(jio_key, next_options)\n
);\n
});\n
}\n
\n
function execSelectionNextCommand(gadget, previous_options) {\n
// Get the selection parameters\n
// Query all docs with those parameters + expected index\n
// Redirect to the result document\n
return getSelection(gadget, previous_options.selection)\n
.push(function (selection) {\n
return gadget.jio_allDocs({\n
"query": selection.query,\n
"list_method_template": selection.list_method_template,\n
"limit": [parseInt(previous_options.selection_index, 10) + 1, 1]\n
})\n
.push(function (result) {\n
if (result.data.rows.length === 0) {\n
return synchronousChangeState(\n
getCommandUrlFor(gadget, COMMAND_HISTORY_PREVIOUS, previous_options)\n
);\n
}\n
return synchronousChangeState(\n
getDisplayUrlFor(result.data.rows[0].id, {\n
selection: previous_options.selection,\n
selection_index: parseInt(previous_options.selection_index, 10) + 1,\n
history: previous_options.history\n
})\n
);\n
});\n
}, function (error) {\n
if ((error instanceof jIO.util.jIOError) &&\n
(error.status_code === 404)) {\n
return synchronousChangeState(\n
getCommandUrlFor(gadget, COMMAND_HISTORY_PREVIOUS, previous_options)\n
); \n
}\n
throw error;\n
});\n
}\n
\n
function execSelectionPreviousCommand(gadget, previous_options) {\n
// Get the selection parameters\n
// Query all docs with those parameters + expected index\n
// Redirect to the result document\n
return getSelection(gadget, previous_options.selection)\n
.push(function (selection) {\n
if (parseInt(previous_options.selection_index, 10) < 1) {\n
return synchronousChangeState(\n
getCommandUrlFor(gadget, COMMAND_HISTORY_PREVIOUS, previous_options)\n
);\n
}\n
return gadget.jio_allDocs({\n
"query": selection.query,\n
"list_method_template": selection.list_method_template,\n
"limit": [parseInt(previous_options.selection_index, 10) - 1, 1]\n
})\n
.push(function (result) {\n
if (result.data.rows.length === 0) {\n
return synchronousChangeState(\n
getCommandUrlFor(gadget, COMMAND_HISTORY_PREVIOUS, previous_options)\n
);\n
}\n
return synchronousChangeState(\n
getDisplayUrlFor(result.data.rows[0].id, {\n
selection: previous_options.selection,\n
selection_index: parseInt(previous_options.selection_index, 10) - 1,\n
history: previous_options.history\n
})\n
);\n
});\n
}, function (error) {\n
if ((error instanceof jIO.util.jIOError) &&\n
(error.status_code === 404)) {\n
return synchronousChangeState(\n
getCommandUrlFor(gadget, COMMAND_HISTORY_PREVIOUS, previous_options)\n
); \n
}\n
throw error;\n
});\n
}\n
\n
\n
function execHistoryPreviousCommand(gadget, previous_options) {\n
var history = previous_options.history;\n
if (history === undefined) {\n
return synchronousChangeState(\n
getDisplayUrlFor(undefined, {page: DEFAULT_PAGE})\n
);\n
}\n
return getHistory(gadget, previous_options.history)\n
.push(function (result_list) {\n
var options = result_list[0].data,\n
jio_key = options.jio_key;\n
delete options.jio_key;\n
return synchronousChangeState(\n
getDisplayUrlFor(jio_key, options)\n
);\n
});\n
}\n
\n
//////////////////////////////////////////////////////////////////\n
// Command URL functions\n
//////////////////////////////////////////////////////////////////\n
function routeMethodLess() {\n
// Nothing. Go to front page\n
return synchronousChangeState(\n
getDisplayUrlFor(undefined, {page: DEFAULT_PAGE})\n
);\n
}\n
\n
function routeDisplay(gadget, command_options) {\n
if (command_options.path) {\n
if (command_options.args.page === undefined) {\n
return synchronousChangeState(\n
getDisplayUrlFor(command_options.path, {\n
page: \'form\',\n
editable: command_options.args.editable,\n
view: command_options.args.view || \'view\',\n
selection: command_options.args.selection,\n
selection_index: command_options.args.selection_index,\n
history: command_options.args.history\n
})\n
);\n
}\n
}\n
\n
if (command_options.args.page === undefined) {\n
return synchronousChangeState(\n
getDisplayUrlFor(undefined, {\n
page: DEFAULT_PAGE\n
})\n
);\n
}\n
\n
command_options.args.jio_key = command_options.path;\n
\n
// Store current options to handle navigation\n
gadget.props.options = JSON.parse(JSON.stringify(command_options.args));\n
\n
return {\n
url: "gadget_interface_validator_page_" + command_options.args.page + ".html",\n
// XXX Drop this options thing.\n
// Implement a "getSelection" method \n
options: command_options.args\n
// options: {}\n
};\n
\n
}\n
\n
function routeCommand(gadget, command_options) {\n
var args = command_options.args,\n
key,\n
split_list,\n
previous_options = {},\n
next_options = {},\n
valid = true;\n
// Rebuild the previous and next parameter dict\n
for (key in args) {\n
if (args.hasOwnProperty(key)) {\n
split_list = key.split(\'.\', 2);\n
if (split_list.length !== 2) {\n
valid = false;\n
break;\n
}\n
if (split_list[0] === PREVIOUS_KEY) {\n
previous_options[split_list[1]] = args[key];\n
} else if (split_list[0] === NEXT_KEY) {\n
next_options[split_list[1]] = args[key];\n
} else {\n
valid = false;\n
break;\n
}\n
}\n
}\n
if (!valid) {\n
throw new Error(\'Unsupported parameters: \' + key);\n
}\n
\n
if (command_options.path === COMMAND_DISPLAY_STATE) {\n
return execDisplayCommand(next_options);\n
}\n
if (command_options.path === COMMAND_INDEX_STATE) {\n
return execIndexCommand(gadget, previous_options, next_options);\n
}\n
if (command_options.path === COMMAND_CHANGE_STATE) {\n
return execChangeCommand(previous_options, next_options);\n
}\n
if (command_options.path === COMMAND_SELECTION_NEXT) {\n
return execSelectionNextCommand(gadget, previous_options, next_options);\n
}\n
if (command_options.path === COMMAND_SELECTION_PREVIOUS) {\n
return execSelectionPreviousCommand(gadget, previous_options, next_options);\n
}\n
if (command_options.path === COMMAND_HISTORY_PREVIOUS) {\n
return execHistoryPreviousCommand(gadget, previous_options);\n
}\n
if (command_options.path === COMMAND_PUSH_HISTORY) {\n
return execPushHistoryCommand(gadget, previous_options, next_options);\n
}\n
throw new Error(\'Unsupported command \' + command_options.path);\n
\n
}\n
\n \n
function listenHashChange(gadget) {\n function listenHashChange(gadget) {\n
// Handle hash in this format: #$path1/path2?a=b&c=d\n // Handle hash in this format: #$path1/path2?a=b&c=d\n
...@@ -579,76 +156,56 @@ ...@@ -579,76 +156,56 @@
window.dispatchEvent(event);\n window.dispatchEvent(event);\n
return result;\n return result;\n
}\n }\n
\n
\n \n
rJS(window)\n rJS(window)\n
.ready(function (gadget) {\n .ready(function (gadget) {\n
gadget.props = {\n gadget.props = {\n
options: {},\n
start_deferred: RSVP.defer()\n start_deferred: RSVP.defer()\n
};\n };\n
})\n })\n
\n \n
.ready(function (gadget) {\n .declareMethod("getCommandUrlFor", function(options) {\n
return gadget.getDeclaredGadget("jio_selection")\n var prefix = \'?\',\n
.push(function (jio_gadget) {\n result,\n
gadget.props.jio_gadget = jio_gadget;\n
return jio_gadget.createJio({\n
type: "sha",\n
sub_storage: {\n
type: "indexeddb",\n
database: "selection"\n
}\n
});\n
});\n
})\n
\n
.declareMethod(\'getCommandUrlFor\', function (options) {\n
var command = options.command,\n
args = options.options,\n
valid = true,\n
key;\n key;\n
// Only authorize \'command\' and \'options\' keys\n result = "#";\n
// Drop all other kind of parameters, to detect issue more easily\n
for (key in options) {\n for (key in options) {\n
if (options.hasOwnProperty(key)) {\n if (options.hasOwnProperty(key) && options[key] !== undefined) {\n
if ((key !== \'command\') && (key !== \'options\')) {\n // Don\'t keep empty values\n
valid = false;\n result += prefix + encodeURIComponent(key) + "=" + encodeURIComponent(options[key]);\n
}\n prefix = \'&\';\n
}\n
}\n }\n
if (valid && (options.command) && (VALID_URL_COMMAND_DICT.hasOwnProperty(options.command))) {\n
return getCommandUrlFor(this, command, args);\n
}\n }\n
return getCommandUrlFor(this, \'error\', options);\n return result;\n
})\n })\n
\n \n
.declareMethod(\'getUrlParameter\', function (key) {\n .declareMethod(\'redirect\', function (options) {\n
return this.props.options[key];\n return this.getCommandUrlFor(options)\n
.push(function (hash) {\n
window.location.replace(hash);\n
// prevent returning unexpected response\n
// wait for the hash change to occur\n
// fail if nothing happens\n
return RSVP.timeout(REDIRECT_TIMEOUT);\n
});\n
})\n })\n
\n \n
.declareMethod(\'route\', function (command_options) {\n .declareMethod(\'route\', function (options) {\n
var gadget = this;\n var gadget = this,\n
args = options.args,\n
page;\n
\n \n
if (command_options.method === PREFIX_DISPLAY) {\n page = args.page || DEFAULT_PAGE;\n
return routeDisplay(gadget, command_options);\n return {\n
}\n url: MAIN_PAGE_PREFIX + "page_" + page + ".html",\n
if (command_options.method === PREFIX_COMMAND) {\n options: args\n
return routeCommand(gadget, command_options);\n };\n
}\n
if (command_options.method) {\n
throw new Error(\'Unsupported hash method: \' + command_options.method);\n
}\n
return routeMethodLess();\n
})\n })\n
\n \n
.declareAcquiredMethod(\'renderApplication\', \'renderApplication\')\n
.declareMethod(\'start\', function () {\n .declareMethod(\'start\', function () {\n
this.props.start_deferred.resolve();\n this.props.start_deferred.resolve();\n
})\n })\n
\n
.declareAcquiredMethod(\'renderApplication\', \'renderApplication\')\n
.declareAcquiredMethod(\'jio_allDocs\', \'jio_allDocs\')\n
\n
.declareService(function () {\n .declareService(function () {\n
var gadget = this;\n var gadget = this;\n
return new RSVP.Queue()\n return new RSVP.Queue()\n
...@@ -656,12 +213,11 @@ ...@@ -656,12 +213,11 @@
return gadget.props.start_deferred.promise;\n return gadget.props.start_deferred.promise;\n
})\n })\n
.push(function () {\n .push(function () {\n
// console.info(\'router service: listen to hash change\');\n
return listenHashChange(gadget);\n return listenHashChange(gadget);\n
});\n });\n
});\n });\n
\n \n
}(window, rJS, RSVP, loopEventListener, document, jIO)); }(window, rJS));
]]></string> </value> ]]></string> </value>
</item> </item>
...@@ -798,7 +354,7 @@ ...@@ -798,7 +354,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>946.44747.61190.22391</string> </value> <value> <string>946.54883.25521.16366</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -816,7 +372,7 @@ ...@@ -816,7 +372,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1446476363.31</float> <float>1446718128.25</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment