Skip to content
Code-Schnipsel Gruppen Projekte
Snippets.js 7,77 KiB
Newer Older
Henning Leutz's avatar
Henning Leutz committed
/**
Henning Leutz's avatar
Henning Leutz committed
 * Snippet Verwaltungs Control
 * - Wird in einem Projekt angezeigt
 *
Henning Leutz's avatar
Henning Leutz committed
 * @module package/quiqqer/html-snippets/bin/backend/Snippets
 * @author www.pcsg.de (Henning Leutz)
 */
define('package/quiqqer/html-snippets/bin/backend/Snippets', [

    'qui/QUI',
    'qui/controls/Control',
    'qui/controls/loader/Loader',
    'package/quiqqer/html-snippets/bin/backend/Utils',
Henning Leutz's avatar
Henning Leutz committed
    'controls/grid/Grid',
    'Ajax',
    'Locale'

], function(QUI, QUIControl, QUILoader, SnippetUtils, Grid, QUIAjax, QUILocale) {
Henning Leutz's avatar
Henning Leutz committed
    'use strict';

    const lg = 'quiqqer/html-snippets';

    return new Class({

        Extends: QUIControl,
        Type: 'package/quiqqer/html-snippets/bin/backend/Snippets',

        Binds: [
Henning Leutz's avatar
Henning Leutz committed
            'add',
            'edit',
            'remove',
            'refresh',
Henning Leutz's avatar
Henning Leutz committed
            '$onImport'
        ],

        initialize: function(options) {
            this.parent(options);

            this.Loader = new QUILoader();
            this.$loaded = false;

            this.$Project = null;
            this.$Grid = null;

            this.addEvents({
                onImport: this.$onImport
            });
        },

        setProject: function(Project) {
            this.$Project = Project;
Henning Leutz's avatar
Henning Leutz committed

            if (this.$Grid) {
                this.$Grid.enable();
                this.$Grid.refresh();
            }
Henning Leutz's avatar
Henning Leutz committed
        },

        $onImport: function() {
            const Label = this.getElm().getParent('label');
            Label.getElements('.field-container-item').destroy();

            const Container = new Element('div', {
                styles: {
                    width: '100%'
                }
            }).inject(Label);

            SnippetUtils.isGDPRInstalled().then((isInstalled) => {

                const columnModel = [
Henning Leutz's avatar
Henning Leutz committed
                    {
                        header: QUILocale.get(lg, 'grid.name'),
                        dataIndex: 'name',
                        dataType: 'string',
                        width: 200
                    }, {
                        header: QUILocale.get(lg, 'grid.event'),
Henning Leutz's avatar
Henning Leutz committed
                        dataIndex: 'event',
Henning Leutz's avatar
Henning Leutz committed
                        dataType: 'string',
Henning Leutz's avatar
Henning Leutz committed
                        width: 400
                    }
                ];

                if (isInstalled) {
                    columnModel.push({
                        header: QUILocale.get(lg, 'grid.gdpr'),
                        dataIndex: 'gdpr',
                        dataType: 'string',
                        width: 200
                    });
                }

                this.$Grid = new Grid(Container, {
                    height: Label.getParent('.qui-panel-content').getSize().y - 100,
                    multiple: true,
                    columnModel: columnModel,
                    buttons: [
                        {
                            name: 'add',
                            text: QUILocale.get('quiqqer/quiqqer', 'add'),
                            events: {
                                click: this.add
                            }
                        }, {
                            name: 'remove',
                            icon: 'fa fa-trash',
                            title: QUILocale.get('quiqqer/quiqqer', 'remove'),
                            disabled: true,
                            styles: {
                                'float': 'right'
                            },
                            events: {
                                click: this.remove
                            }
                    ]
                });

                this.$Grid.disable();
                this.$Grid.addEvents({
                    refresh: this.refresh,
                    onDblClick: this.edit,
                    click: () => {
                        const selected = this.$Grid.getSelectedData();

                        if (selected.length) {
                            this.$Grid.getButton('remove').enable();
                if (this.$Project) {
                    this.$Grid.enable();
                    this.$Grid.refresh();
Henning Leutz's avatar
Henning Leutz committed
                }
            });
        },

        refresh: function() {
            if (!this.$Project) {
                return;
            }

            this.Loader.show();

            QUIAjax.get('package_quiqqer_html-snippets_ajax_backend_getSnippets', (result) => {
                this.$Grid.setData({
                    data: result
                });

                this.$Grid.getButton('remove').disable();
Henning Leutz's avatar
Henning Leutz committed
                this.Loader.hide();
            }, {
                'package': 'quiqqer/html-snippets',
                projectName: this.$Project.getName()
            });
        },

        add: function() {
            if (!this.$Project) {
                return;
            }

            require([
                'package/quiqqer/html-snippets/bin/backend/controls/windows/AddSnippet'
            ], (AddSnippet) => {
                new AddSnippet({
                    Project: this.$Project,
                    events: {
                        onClose: () => {
Henning Leutz's avatar
Henning Leutz committed
                            this.refresh();
                        }
                    }
                }).open();
            });
        },

        edit: function() {
            if (!this.$Project) {
                return;
            }
            require([
                'package/quiqqer/html-snippets/bin/backend/controls/windows/EditSnippet'
            ], (EditSnippet) => {
                new EditSnippet({
                    Project: this.$Project,
                    name: this.$Grid.getSelectedData()[0].name,
                    events: {
                        onClose: () => {
                            this.refresh();
                        }
                    }
                }).open();
            });
Henning Leutz's avatar
Henning Leutz committed
        },

        remove: function() {
            const selected = this.$Grid.getSelectedData();
Henning Leutz's avatar
Henning Leutz committed

            if (!selected.length) {
                return;
            }

            const snippetNames = selected.map((entry) => {
                return entry.name;
            });

            require(['qui/controls/windows/Confirm'], (QUIConfirm) => {
                const listElements = (n) => {
                    return '<li>' + n + '</li>';
                };

Henning Leutz's avatar
Henning Leutz committed
                new QUIConfirm({
                    icon: 'fa fa-trash',
                    texticon: 'fa fa-trash',
Henning Leutz's avatar
Henning Leutz committed
                    title: QUILocale.get(lg, 'window.remove.title'),
                    information: QUILocale.get(lg, 'window.remove.information', {
                        snippetList: '<ul>' + snippetNames.map(listElements).join('') + '</ul>'
                    }),
                    text: QUILocale.get(lg, 'window.remove.text', {
                        snippetList: snippetNames.join(', ')
                    }),
Henning Leutz's avatar
Henning Leutz committed
                    autoclose: false,
                    maxWidth: 600,
                    maxHeight: 400,
Henning Leutz's avatar
Henning Leutz committed
                    events: {
                        onSubmit: (Win) => {
                            Win.Loader.show();

                            QUIAjax.post('package_quiqqer_html-snippets_ajax_backend_removeSnippets', () => {
                                Win.close();
                                this.refresh();
                            }, {
                                'package': 'quiqqer/html-snippets',
                                projectName: this.$Project.getName(),
                                snippetNames: JSON.encode(snippetNames),
                                onError: () => {
                                    Win.Loader.hide();
                                    this.refresh();
                                }
                            });
                        }
                    }
                }).open();
            });