Skip to content
Code-Schnipsel Gruppen Projekte
Manager.js 13,7 KiB
Newer Older
Henning Leutz's avatar
Henning Leutz committed
/**
 * Cron Manager
 *
 * @module package/quiqqer/cron/bin/Manager
Henning Leutz's avatar
Henning Leutz committed
 * @author www.pcsg.de (Henning Leutz)
 *
 * @require qui/QUI
 * @require qui/controls/desktop/Panel
 * @require qui/controls/windows/Confirm
 * @require qui/controls/buttons/Button
 * @require qui/controls/buttons/Seperator
 * @require controls/grid/Grid
 * @require Ajax
 * @require Locale
define('package/quiqqer/cron/bin/Manager', [
Henning Leutz's avatar
Henning Leutz committed
    'qui/controls/desktop/Panel',
Henning Leutz's avatar
Henning Leutz committed
    'qui/controls/buttons/Button',
Henning Leutz's avatar
Henning Leutz committed
    'qui/controls/buttons/Seperator',
Henning Leutz's avatar
Henning Leutz committed
    'controls/grid/Grid',
], function (QUI, QUIPanel, QUIConfirm, QUIButton, QUIButtonSeperator, Grid, Ajax, QUILocale) {
Henning Leutz's avatar
Henning Leutz committed
    "use strict";
Henning Leutz's avatar
Henning Leutz committed
    var lg = 'quiqqer/cron';

Henning Leutz's avatar
Henning Leutz committed
    return new Class({
        Extends: QUIPanel,
        Type   : 'package/quiqqer/cron/bin/Manager',
Henning Leutz's avatar
Henning Leutz committed
            '$onCreate',
            '$onResize'
        options: {
            title: 'Cron-Manager',
Henning Leutz's avatar
Henning Leutz committed
            icon : 'fa fa-clock-o'
        initialize: function (options) {
            this.parent(options);
Henning Leutz's avatar
Henning Leutz committed

            this.addEvents({
                onCreate: this.$onCreate,
                onResize: this.$onResize
Henning Leutz's avatar
Henning Leutz committed
            });
        },

        /**
         * Load the crons into the grid
         *
         * @return {self}
         */
        loadCrons: function () {
Henning Leutz's avatar
Henning Leutz committed
            var self = this;

            Ajax.get('package_quiqqer_cron_ajax_getList', function (result) {
                if (!self.$Grid) {
                var execCron = function (Btn) {
                        Btn.getAttribute('cronId')
                var toggleCron = function (Btn) {
                        Btn.getAttribute('cronId')
                for (var i = 0, len = result.length; i < len; i++) {
                    result[i].status = {
                        title : QUILocale.get(lg, 'cron.panel.manager.btn.toggle'),
Henning Leutz's avatar
Henning Leutz committed
                        icon  : result[i].active == 1 ? 'fa fa-check' : 'fa fa-remove',
                        cronId: result[i].id,
                        events: {
                            onClick: toggleCron
                    result[i].play = {
                        name  : 'cron-play-button-' + result[i].id,
                        title : QUILocale.get(lg, 'cron.panel.manager.btn.execute'),
Henning Leutz's avatar
Henning Leutz committed
                        icon  : 'fa fa-play',
                        cronId: result[i].id,
                        events: {
                            onClick: execCron
                self.$Grid.setData({
                'package': 'quiqqer/cron'
Henning Leutz's avatar
Henning Leutz committed
            });
Henning Leutz's avatar
Henning Leutz committed

            return this;
Henning Leutz's avatar
Henning Leutz committed
        },

        /**
         * event : on Create
         */
        $onCreate: function () {
            var self = this;

Henning Leutz's avatar
Henning Leutz committed
            this.addButton(
                new QUIButton({
                    name     : 'add',
                    text     : QUILocale.get(lg, 'cron.panel.manager.btn.add'),
Henning Leutz's avatar
Henning Leutz committed
                    textimage: 'fa fa-plus',
                    events   : {
                        onClick: function () {
                            self.openAddCronWindow();
                        }
                    }
                })
            );
            this.addButton(new QUIButtonSeperator());

            this.addButton(
                new QUIButton({
                    name     : 'edit',
                    text     : QUILocale.get(lg, 'cron.panel.manager.btn.edit'),
Henning Leutz's avatar
Henning Leutz committed
                    textimage: 'fa fa-edit',
                    events   : {
                        onClick: function () {
                            self.editMarkedCron();
                        }
                    }
                })
            );

            this.addButton(
                new QUIButton({
                    name     : 'delete',
                    text     : QUILocale.get(lg, 'cron.panel.manager.btn.delete'),
Henning Leutz's avatar
Henning Leutz committed
                    textimage: 'fa fa-trash',
                    events   : {
                        onClick: function () {
                            self.deleteMarkedCrons();
            this.addButton(new QUIButtonSeperator());
Henning Leutz's avatar
Henning Leutz committed

            this.addButton(
                new QUIButton({
                    name     : 'history',
                    text     : QUILocale.get(lg, 'cron.panel.manager.btn.history'),
Henning Leutz's avatar
Henning Leutz committed
                    textimage: 'fa fa-long-arrow-right',
                    events   : {
                        onClick: function () {
Henning Leutz's avatar
Henning Leutz committed
                            self.showHistory();
                        }
                    }
                })
            );

            this.getButtons('edit').disable();
            this.getButtons('delete').disable();
            var Content   = this.getContent(),
Henning Leutz's avatar
Henning Leutz committed

                Container = new Element('div', {
Henning Leutz's avatar
Henning Leutz committed
                    styles : {
                        width : '100%',
Henning Leutz's avatar
Henning Leutz committed
                    }
                }).inject(Content);
Henning Leutz's avatar
Henning Leutz committed


            this.$Grid = new Grid(Container, {
                columnModel      : [{
                    header   : QUILocale.get('quiqqer/system', 'status'),
                    dataIndex: 'status',
                    dataType : 'button',
                    width    : 60
                    header   : '&nbsp;',
                    dataIndex: 'play',
                    dataType : 'button',
                    width    : 60
                    header   : QUILocale.get('quiqqer/system', 'id'),
                    dataIndex: 'id',
                    dataType : 'string',
                    width    : 50
Henning Leutz's avatar
Henning Leutz committed
                }, {
                    header   : QUILocale.get(lg, 'cron.title'),
                    dataIndex: 'title',
                    dataType : 'string',
                    width    : 150
Henning Leutz's avatar
Henning Leutz committed
                }, {
                    header   : QUILocale.get(lg, 'cron.min'),
                    dataIndex: 'min',
                    dataType : 'string',
                    width    : 50
Henning Leutz's avatar
Henning Leutz committed
                }, {
                    header   : QUILocale.get(lg, 'cron.hour'),
                    dataIndex: 'hour',
                    dataType : 'string',
                    width    : 50
Henning Leutz's avatar
Henning Leutz committed
                }, {
                    header   : QUILocale.get(lg, 'cron.day'),
                    dataIndex: 'day',
                    dataType : 'string',
                    width    : 50
Henning Leutz's avatar
Henning Leutz committed
                }, {
                    header   : QUILocale.get(lg, 'cron.month'),
                    dataIndex: 'month',
                    dataType : 'string',
                    width    : 50
                    header   : QUILocale.get(lg, 'cron.dayOfWeek'),
                    dataIndex: 'dayOfWeek',
                    dataType : 'string',
                    width    : 50
Henning Leutz's avatar
Henning Leutz committed
                }, {
                    header   : QUILocale.get(lg, 'cron.execute'),
                    dataIndex: 'exec',
                    dataType : 'string',
                    width    : 150
                    header   : QUILocale.get(lg, 'cron.params'),
                    dataIndex: 'params',
                    dataType : 'string',
                    width    : 150
                }, {
                    header   : QUILocale.get(lg, 'cron.desc'),
                    dataIndex: 'desc',
                    dataType : 'string',
                    width    : 200
                multipleSelection: true,
                pagination       : true
                onRefresh: function () {
                onClick  : function () {
                    var delButton  = self.getButtons('delete'),
                        editButton = self.getButtons('edit'),
                        selected   = self.$Grid.getSelectedIndices().length;

                    if (selected == 1) {
                        editButton.enable();
                        editButton.disable();
                    }
                onDblClick: function (data) {
                    var rowData = self.$Grid.getDataByRow(data.row);
                    self.editCron(rowData.id);
Henning Leutz's avatar
Henning Leutz committed
            });

            this.loadCrons();
        },

        /**
         * event : on resize
         */
        $onResize: function () {
            if (!this.$Grid) {
Henning Leutz's avatar
Henning Leutz committed
                return;
            }
Henning Leutz's avatar
Henning Leutz committed
            var Content = this.getContent(),
                size    = Content.getSize();
            this.$Grid.setHeight(size.y - 40);
            this.$Grid.setWidth(size.x - 40);
        },

        /**
         * Open the delete marked cron windows and delete all marked crons
         *
         * @return {self}
         */
        deleteMarkedCrons: function () {
            if (!this.$Grid) {
                return this;
            }

            var self = this,
                data = this.$Grid.getSelectedData();

            if (!data.length) {
            var ids = data.map(function (o) {
Henning Leutz's avatar
Henning Leutz committed
                icon       : 'fa fa-remove',
                title      : QUILocale.get(lg, 'cron.window.delete.cron.title'),
                text       : QUILocale.get(lg, 'cron.window.delete.cron.text'),
                information: QUILocale.get(lg, 'cron.window.delete.cron.information', {
                    ids: ids.join(',')
Henning Leutz's avatar
Henning Leutz committed
                }),
                events     : {
                    onSubmit: function (Win) {
                        Ajax.post('package_quiqqer_cron_ajax_delete', function () {
                            'package': 'quiqqer/cron',
                            ids      : JSON.encode(ids)
         * Edit a cron, opens the cron Edit-Window
         *
         * @param {String} cronId - ID of the Cron
         */
        editCron: function (cronId) {
            var self = this;

            require(['package/quiqqer/cron/bin/CronWindow'], function (Window) {
                new Window({
                    cronId: cronId,
                    events: {
                        onSubmit: function () {
                            self.loadCrons();
                        }
                    }
                }).open();
            });

            return this;
        },

        /**
         * Opens the Edit-Window for the marked cron
         */
        editMarkedCron: function () {
            if (!this.$Grid) {
            var data = this.$Grid.getSelectedData();
            if (!data.length) {
            this.editCron(data[0].id);
        },

        /**
         * Open the add Cron-Window
         *
         * @return {self}
         */
        openAddCronWindow: function () {
            require(['package/quiqqer/cron/bin/CronWindow'], function (Window) {
                new Window({
                    events: {
                        onSubmit: function () {
                            self.loadCrons();
                        }
                    }
                }).open();
            });

            return this;
        },

        /**
         * Change the cron status
         * If the cron is active to deactive
         * If the cron is deactive to active
         *
         * @param {Number} cronId - ID of the Cron
         * @return {self}
         */
        toggleStatusOfCron: function (cronId) {
            Ajax.post('package_quiqqer_cron_ajax_cron_toggle', function () {
                'package': 'quiqqer/cron',
                cronId   : cronId
            });

            return this;
         * @param {Number} cronId - ID of the Cron
        execCron: function (cronId) {
            if (this.$Grid) {
                buttons = QUI.Controls.get('cron-play-button-' + cronId);
            for (i = 0, len = buttons.length; i < len; i++) {
Henning Leutz's avatar
Henning Leutz committed
                buttons[i].setAttribute('icon', 'fa fa-spinner fa-spin');
            Ajax.post('package_quiqqer_cron_ajax_cron_executeCron', function () {
                for (i = 0, len = buttons.length; i < len; i++) {
Henning Leutz's avatar
Henning Leutz committed
                    buttons[i].setAttribute('icon', 'fa fa-play');
                'package': 'quiqqer/cron',
                cronId   : cronId
Henning Leutz's avatar
Henning Leutz committed
        },

        /**
         * Show the Cron-History Panel
         */
        showHistory: function () {
Henning Leutz's avatar
Henning Leutz committed
            var self = this;

            require(['package/quiqqer/cron/bin/History'], function (Panel) {
                new Panel().inject(self.getParent());
Henning Leutz's avatar
Henning Leutz committed
            });