aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/extract_callback.js
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+github@gmail.com>2021-07-22 16:07:19 +0200
committerJan-Hendrik Willms <tleilax+github@gmail.com>2021-07-22 16:19:12 +0200
commita3da1483a9e689846179159355badfec8073dbec (patch)
tree770dcca6bdf5f6f2a11b0e7fcbbeda6919a3fc52 /resources/assets/javascripts/lib/extract_callback.js
current code from svn, revision 62608
Diffstat (limited to 'resources/assets/javascripts/lib/extract_callback.js')
-rw-r--r--resources/assets/javascripts/lib/extract_callback.js80
1 files changed, 80 insertions, 0 deletions
diff --git a/resources/assets/javascripts/lib/extract_callback.js b/resources/assets/javascripts/lib/extract_callback.js
new file mode 100644
index 0000000..5b88607
--- /dev/null
+++ b/resources/assets/javascripts/lib/extract_callback.js
@@ -0,0 +1,80 @@
+export default function extractCallback(cmd, payload) {
+ var command = cmd,
+ chunks,
+ last_chunk = null,
+ callback = window,
+ previous = null;
+
+ // Try to decode URI component in case it is encoded
+ try {
+ command = window.decodeURIComponent(command);
+ } catch (ignore) {}
+
+ // Try to parse value as JSON (value might be {func: 'foo', payload: {}})
+ try {
+ command = $.parseJSON(command);
+ } catch (e) {
+ command = { func: command };
+ }
+
+ // Check for invalid call
+ if (!command.hasOwnProperty('func')) {
+ throw 'Dialog: Invalid value for X-Dialog-Execute';
+ }
+
+ // Populate payload if not set
+ if (!command.hasOwnProperty('payload')) {
+ command.payload = payload;
+ }
+
+ // Find callback
+ chunks = command.func.trim().split(/\./);
+ $.each(chunks, function(index, chunk) {
+ // Check if last chunk was unfinished
+ if (last_chunk !== null) {
+ chunk = last_chunk + '.' + chunk;
+ last_chunk = null;
+ }
+
+ // Check for not finished/closed chunk
+ if (chunk.match(/\([^\)]*$/)) {
+ last_chunk = chunk;
+ return;
+ }
+
+ previous = callback;
+
+ var match = chunk.match(/\((.*)\);?$/),
+ parameters = null;
+
+ if (match !== null) {
+ chunk = chunk.replace(match[0], '');
+ try {
+ parameters = $.parseJSON('[' + match[1].replace(/'/g, '"') + ']');
+ } catch (e) {
+ console.log('error parsing json', match);
+ }
+ }
+
+ if (callback[chunk] === undefined) {
+ throw 'Error: Undefined callback ' + cmd;
+ }
+
+ if ($.isFunction(callback[chunk]) && parameters !== null) {
+ callback = callback[chunk].apply(callback, parameters);
+ } else {
+ callback = callback[chunk];
+ }
+ });
+
+ // Check callback
+ if (!$.isFunction(callback)) {
+ return function() {
+ return callback;
+ };
+ }
+
+ return function(p) {
+ return callback.apply(previous, [p || payload]);
+ };
+}