CallBackery

 view release on metacpan or  search on metacpan

lib/CallBackery/qooxdoo/callbackery/source/class/callbackery/ui/plugin/Action.js  view on Meta::CPAN

                    let btnId = btCfg.key
                        + (btCfg.testingIdPostfix ? btCfg.testingIdPostfix : '')
                        + 'Button';
                    this.addOwnedQxObject(button, btnId);
                }
                if (mmButton && btCfg.key) {
                    let mmBtnId = btCfg.key
                        + (btCfg.testingIdPostfix ? btCfg.testingIdPostfix : '')
                        + 'mmButton';
                    this.addOwnedQxObject(mmButton, mmBtnId);
                }
                var action = function () {
                    var that = this;
                    if (!button.isEnabled()) {
                        return;
                    }
                    switch (btCfg.action) {
                        case 'save':
                            var formData = getFormData();
                            var key = btCfg.key;
                            callbackery.data.Server.getInstance().callAsync(function (ret) {
                                that.fireDataEvent('actionResponse', ret || {});
                            }, 'processPluginData', cfg.name, { "key": key, "formData": formData });
                            break;
                        case 'submitVerify':
                        case 'submit':
                            var formData = getFormData();
                            if (formData === false) {
                                callbackery.ui.MsgBox.getInstance().error(
                                    this.tr("Validation Error"),
                                    this.tr("The form can only be submitted when all data fields have valid content.")
                                );
                                return;
                            }
                            var key = btCfg.key;
                            var asyncCall = function () {
                                callbackery.data.Server.getInstance().callAsyncSmartBusy(function (ret) {
                                    that.fireDataEvent('actionResponse', ret || {});
                                }, 'processPluginData', cfg.name, { "key": key, "formData": formData });
                            };

                            if (btCfg.action == 'submitVerify') {
                                var title = btCfg.label != null ? btCfg.label : btCfg.key;
                                callbackery.ui.MsgBox.getInstance().yesno(
                                    this.xtr(title),
                                    this.xtr(btCfg.question)
                                )
                                    .addListenerOnce('choice', function (e) {
                                        if (e.getData() == 'yes') {
                                            asyncCall();
                                        }
                                    }, this);
                            }
                            else {
                                asyncCall();
                            }
                            break;
                        case 'download':
                        case 'display':
                            var formData = getFormData();
                            let busy = callbackery.ui.Busy.getInstance();
                            if (formData === false) {
                                callbackery.ui.MsgBox.getInstance().error(
                                    this.tr("Validation Error"),
                                    this.tr("The form can only be submitted when all data fields have valid content.")
                                );
                                return;
                            }
                            var key = btCfg.key;
                            if (btCfg.busyMessage) {
                                busy.manifest(this.xtr(btCfg.busyMessage));
                            } else {
                                busy.manifest(this.tr('Preparing Download ...'));
                            }
                            setTimeout(() => { busy.vanish(); }, 3 * 1000); // hide the activity indicator after 3 seconds anyway
                            callbackery.data.Server.getInstance().callAsyncSmart(function (cookie) {
                                let url = 'download'
                                    + '?name=' + cfg.name
                                    + '&key=' + key
                                    + '&xsc=' + encodeURIComponent(cookie)
                                    + '&formData=' + encodeURIComponent(qx.lang.Json.stringify(formData));
                                if (btCfg.action == 'display') {
                                    window.open(url + '&display=1', '_blank');
                                    return;
                                }
                                var iframe = new qx.ui.embed.Iframe().set({
                                    width: 100,
                                    height: 100
                                });
                                iframe.addListener('load', function (e) {
                                    busy.vanish();
                                    var response = {
                                        exception: {
                                            message: String(that.tr("No Data")),
                                            code: 9999
                                        }
                                    };
                                    try {
                                        // innerHTML is wrapped in `<pre>` tags, which we remove.
                                        let innerHTML = iframe.getBody().innerHTML;
                                        if (innerHTML) {
                                            innerHTML = innerHTML.replace(/^<.*?>/, '');
                                            innerHTML = innerHTML.replace(/<.*?>$/, '');
                                        }
                                        // If there is text left, it should be the json from the server.
                                        // JSON parsing an empty string is an error.
                                        if (innerHTML) {
                                            response = qx.lang.Json.parse(innerHTML);
                                        }
                                        // otherwise remove standard exception.
                                        else {
                                            response = {};
                                        }
                                    } catch (e) { };
                                    if (response.exception) {
                                        callbackery.ui.MsgBox.getInstance().error(
                                            that.tr("Download Exception"),
                                            that.xtr(response.exception.message) + " (" + response.exception.code + ")"
                                        );
                                    }
                                    that.getApplicationRoot().remove(iframe);
                                    if (btCfg.closeAfterDownload) {
                                        that.fireDataEvent('actionResponse', { action: 'cancel' });
                                    }
                                });
                                iframe.setSource(url);
                                that.getApplicationRoot().add(iframe, { top: -1000, left: -1000 });
                            }, 'getSessionCookie');
                            break;
                        case 'cancel':
                            this.fireDataEvent('actionResponse', { action: 'cancel' });
                            break;
                        case 'wizzard':
                            var parent = that.getLayoutParent();
                            while (!parent.classname.match(/Page|Popup/)) {
                                parent = parent.getLayoutParent();
                            }
                            // This could in principal work for Page although.
                            if (parent.classname.match(/Popup/)) { // parent already exists, replace content
                                parent.replaceContent(btCfg, getFormData);
                                break;
                            }
                        // fall through intended to create first popup content
                        case 'popup':
                            if (!btCfg.noValidation) { // backward incompatibility work around
                                var formData = getFormData();
                                if (formData === false) {
                                    callbackery.ui.MsgBox.getInstance().error(
                                        this.tr("Validation Error"),
                                        this.tr("The form can only be submitted when all data fields have valid content.")
                                    );

lib/CallBackery/qooxdoo/callbackery/source/class/callbackery/ui/plugin/Action.js  view on Meta::CPAN

                        if (btCfg.addToToolBar !== false) {
                            this.add(button);
                        }
                    }
                }
                if (mmButton) {
                    mmButton.addListener('execute', action, this);
                    if (btCfg.addToMenu) {
                        mmMenues[btCfg.addToMenu].add(mmButton);
                    }
                    else {
                        if (btCfg.addToToolBar !== false) {
                            mm.add(mmButton);
                        }
                    }
                }
                if (menuButton) {
                    menuButton.addListener('execute', action, this);
                    tm.add(menuButton);
                }
                if (button && menuButton) {
                    this._bindButtonProperties(button, mmButton);
                }
            }, this);
        },

        _makeUploadButton(cfg, btCfg, getFormData, buttonClass) {
            var button;
            var label = btCfg.label ? this.xtr(btCfg.label) : null;
            if (buttonClass) {
                button = new buttonClass(label);
            }
            else {
                button = new qx.ui.form.FileSelectorButton(label);
            }
            if (btCfg.buttonSet) {
                var bs = btCfg.buttonSet;
                if (bs.label) {
                    bs.label = this.xtr(bs.label);
                }
                button.set(bs);
                if (btCfg.key) {
                    this._buttonSetMap[btCfg.key] = bs;
                }
            }
            var serverCall = callbackery.data.Server.getInstance();
            var key = btCfg.key;
            var name = cfg.name;
            button.addListener('changeFileSelection', function (e) {
                var fileList = e.getData();
                var formData = getFormData();
                if (formData && fileList) {
                    var form = new FormData();
                    form.append('name', name);
                    form.append('key', key);
                    form.append('file', fileList[0]);
                    form.append('formData', qx.lang.Json.stringify(formData));
                    var that = this;
                    serverCall.callAsyncSmart(function (cookie) {
                        form.append('xsc', cookie);
                        that._uploadForm(form,btCfg.busyMessage);
                    }, 'getSessionCookie');
                } else {
                    callbackery.ui.MsgBox.getInstance().error(
                        this.tr("Upload Exception"),
                        this.tr("Make sure to select a file and properly fill the form")
                    );
                }
            }, this);

            return button;
        },

        _uploadForm(form,busyMessage) {
            var req = new qx.io.request.Xhr("upload", 'POST').set({
                requestData: form
            });
            let busy = callbackery.ui.Busy.getInstance();
            req.addListener('success', function (e) {
                busy.vanish();                
                var response = req.getResponse();
                if (response.exception) {
                    callbackery.ui.MsgBox.getInstance().error(
                        this.tr("Upload Exception"),
                        this.xtr(response.exception.message)
                        + " (" + response.exception.code + ")"
                    );
                } else {
                    this.fireDataEvent('actionResponse', response);
                }
                req.dispose();
            }, this);
            req.addListener('fail', function (e) {
                var response = {};
                busy.vanish();
                try {
                    response = req.getResponse();
                }
                catch (e) {
                    response = {
                        exception: {
                            message: e.message,
                            code: 99999
                        }
                    };
                }
                callbackery.ui.MsgBox.getInstance().error(
                    this.tr("Upload Exception"),
                    this.xtr(response.exception.message)
                    + " (" + response.exception.code + ")"
                );
                req.dispose();
            });
            if (busyMessage) {
                busy.manifest(this.xtr(busyMessage));
            } else {
                busy.manifest(this.tr('Uploading File, please wait ...'));
            }
            req.send();
        },

        getTableContextMenu() {
            return this._tableMenu;
        },
        getMobileMenu() {
            return this._mobileMenu;
        },
        getDefaultAction() {
            return this._defaultAction;
        },
        getButtonMap() {
            return this._buttonMap;
        },
        getButtonSetMap() {
            return this._buttonSetMap;
        },
        getMenuButtonSetMap() {
            return this._menuButtonSetMap;
        },
        _bindButtonProperties(button, mmButton) {
            ['visibility', 'enabled', 'label', 'icon'].forEach((prop) => {
                button.bind(prop, mmButton, prop);
            });
        }
    },

    destruct() {
        if (!this._buttonMap) {
            return;
        }
        for (const [key, btn] of Object.entries(this._buttonMap)) {
            btn.destroy();
        }
    },

});



( run in 0.662 second using v1.01-cache-2.11-cpan-39bf76dae61 )