| 1 |
(function($, w) { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
const makeHelpWizardRequest = (data) => { |
| 5 |
return new Promise((resolve, reject) => { |
| 6 |
VBOCore.doAjax( |
| 7 |
w.VBO_HELP_WIZARD_AJAX_URL, |
| 8 |
data, |
| 9 |
(resp) => { |
| 10 |
resolve(resp); |
| 11 |
}, (err) => { |
| 12 |
reject(err.responseText || err.statusText || 'Error'); |
| 13 |
} |
| 14 |
); |
| 15 |
}); |
| 16 |
} |
| 17 |
|
| 18 |
const normalizePostData = (data) => { |
| 19 |
|
| 20 |
if (data === undefined) { |
| 21 |
data = {}; |
| 22 |
} else if (Array.isArray(data)) { |
| 23 |
// the form data is serialized @see jQuery.serializeArray() |
| 24 |
var form = data; |
| 25 |
|
| 26 |
data = {}; |
| 27 |
|
| 28 |
for (var i = 0; i < form.length; i++) { |
| 29 |
// if the field ends with [] it should be an array |
| 30 |
if (form[i].name.endsWith("[]")) { |
| 31 |
// if the field doesn't exist yet, create a new list |
| 32 |
if (!data.hasOwnProperty(form[i].name)) { |
| 33 |
data[form[i].name] = new Array(); |
| 34 |
} |
| 35 |
|
| 36 |
// append the value to the array |
| 37 |
data[form[i].name].push(form[i].value); |
| 38 |
} else { |
| 39 |
// otherwise overwrite the value (if any) |
| 40 |
data[form[i].name] = form[i].value; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
return data; |
| 46 |
} |
| 47 |
|
| 48 |
w.VBOHelpWizard = class VBOHelpWizard { |
| 49 |
static getNextInstruction() { |
| 50 |
return makeHelpWizardRequest({ |
| 51 |
task: 'helpwizard.show', |
| 52 |
}); |
| 53 |
} |
| 54 |
|
| 55 |
static dismissInstruction(id, datetime) { |
| 56 |
return makeHelpWizardRequest({ |
| 57 |
task: 'helpwizard.dismiss', |
| 58 |
instruction: id, |
| 59 |
datetime: datetime, |
| 60 |
}); |
| 61 |
} |
| 62 |
|
| 63 |
static processInstruction(id, args) { |
| 64 |
return makeHelpWizardRequest({ |
| 65 |
task: 'helpwizard.process', |
| 66 |
instruction: id, |
| 67 |
args: args, |
| 68 |
}); |
| 69 |
} |
| 70 |
|
| 71 |
static setupEligibleInstruction() { |
| 72 |
VBOHelpWizard.getNextInstruction().then((instruction) => { |
| 73 |
const currentInstruction = $('.vbo-menu-help-wizard-container[data-instruction]'); |
| 74 |
|
| 75 |
if (currentInstruction.length) { |
| 76 |
currentInstruction.remove(); |
| 77 |
} |
| 78 |
|
| 79 |
if (instruction.has) { |
| 80 |
VBOHelpWizard.displayBadge(instruction); |
| 81 |
} |
| 82 |
}).catch((error) => { |
| 83 |
console.warn('Help wizard failure!\n' + error); |
| 84 |
}); |
| 85 |
} |
| 86 |
|
| 87 |
static displayBadge(instruction) { |
| 88 |
const menuContainer = $('<div class="vbo-menu-help-wizard-container"></div>') |
| 89 |
.attr('data-instruction', instruction.id); |
| 90 |
|
| 91 |
const menuButton = $('<button type="button" class="vbo-help-wizard-handler"></button>') |
| 92 |
.attr('title', instruction.title) |
| 93 |
.html($('<i></i>').addClass(instruction.icon)); |
| 94 |
|
| 95 |
menuButton.on('click', () => { |
| 96 |
const modalOptions = { |
| 97 |
suffix: 'help-wizard-modal', |
| 98 |
title: instruction.title, |
| 99 |
extra_class: 'vbo-modal-rounded vbo-modal-dialog', |
| 100 |
escape_dismiss: false, |
| 101 |
body_prepend: false, |
| 102 |
lock_scroll: true, |
| 103 |
dismiss_event: 'vbo-help-wizard-instruction-modal-dismiss', |
| 104 |
}; |
| 105 |
|
| 106 |
if (instruction.dismissible) { |
| 107 |
modalOptions.footer_left = $('<button type="button" class="btn"></button>') |
| 108 |
.text(Joomla.JText._('VBDISMISS')) |
| 109 |
.on('click', async function() { |
| 110 |
$(this).prop('disabled', true); |
| 111 |
|
| 112 |
try { |
| 113 |
// process instruction |
| 114 |
await VBOHelpWizard.dismissInstruction(instruction.id); |
| 115 |
|
| 116 |
// dismiss succeeded, remove "help" button from menu |
| 117 |
menuContainer.remove(); |
| 118 |
|
| 119 |
VBOCore.emitEvent('vbo-help-wizard-instruction-modal-dismiss'); |
| 120 |
|
| 121 |
// move to the next instruction, if any |
| 122 |
setTimeout(() => { |
| 123 |
VBOHelpWizard.setupEligibleInstruction(); |
| 124 |
}, 1000); |
| 125 |
} catch (err) { |
| 126 |
alert(err); |
| 127 |
} |
| 128 |
|
| 129 |
$(this).prop('disabled', false); |
| 130 |
}); |
| 131 |
} |
| 132 |
|
| 133 |
if (instruction.processable) { |
| 134 |
modalOptions.footer_right = $('<button type="button" class="btn btn-success"></button>') |
| 135 |
.text(instruction.processtext || Joomla.JText._('VBSAVE')) |
| 136 |
.on('click', async function() { |
| 137 |
$(this).prop('disabled', true); |
| 138 |
|
| 139 |
// converts the form inputs into a key-value pairs object |
| 140 |
const data = normalizePostData($('#help-wizard-instruction-form').serializeArray()); |
| 141 |
|
| 142 |
try { |
| 143 |
// process instruction |
| 144 |
const response = await VBOHelpWizard.processInstruction(instruction.id, data); |
| 145 |
|
| 146 |
// process succeeded, remove "help" button from menu |
| 147 |
menuContainer.remove(); |
| 148 |
|
| 149 |
VBOCore.emitEvent('vbo-help-wizard-instruction-modal-dismiss'); |
| 150 |
|
| 151 |
setTimeout(() => { |
| 152 |
if (response.redirect) { |
| 153 |
// follow the returned URL |
| 154 |
document.location.href = response.redirect; |
| 155 |
} else { |
| 156 |
VBOHelpWizard.setupEligibleInstruction(); |
| 157 |
} |
| 158 |
}, 1000); |
| 159 |
} catch (err) { |
| 160 |
alert(err); |
| 161 |
} |
| 162 |
|
| 163 |
$(this).prop('disabled', false); |
| 164 |
}); |
| 165 |
} |
| 166 |
|
| 167 |
// display modal |
| 168 |
const modalBody = VBOCore.displayModal(modalOptions); |
| 169 |
modalBody.append(instruction.html); |
| 170 |
}); |
| 171 |
|
| 172 |
menuContainer.append(menuButton); |
| 173 |
|
| 174 |
$('.vbo-menu-container .vbo-menu-updates').append(menuContainer); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
$(function() { |
| 179 |
VBOHelpWizard.setupEligibleInstruction(); |
| 180 |
}); |
| 181 |
|
| 182 |
/** |
| 183 |
* For configurable reports only. |
| 184 |
*/ |
| 185 |
document.addEventListener('vbo-report-settings-saved', (event) => { |
| 186 |
VBOHelpWizard.setupEligibleInstruction(); |
| 187 |
}); |
| 188 |
})(jQuery, window); |