<commit_before>Ext.namespace('Kwc.Abstract.Image');
Kwc.Abstract.Image.DimensionField = Ext.extend(Ext.form.TriggerField, {
    triggerClass: 'x-form-search-trigger',
    width: 300,
    readOnly: true,
    imageData: null,
    cropData: null,

    initComponent: function() {
        Kwc.Abstract.Image.DimensionField.superclass.initComponent.call(this);
    },
    getValue: function() {
        return this.value || {};
    },

    setValue: function(v) {
        this.value = v;
        if (this.rendered) {
            if (v.dimension) {
                this.setRawValue(this._getDimensionString(v));
            } else {
                this.setRawValue('');
            }
        }
    },
    afterRender: function() {
        Kwc.Abstract.Image.DimensionField.superclass.afterRender.call(this);
        if (this.value) {
            this.setValue(this.value);
        }
        this.findParentByType('kwf.autoform').// TODO: retrieve Upload-field more cleanly
            findByType('kwf.file')[0].on('change', function (el, value) {
                this.imageData = value;
        }, this);
    },

    _validateSizes: function()
    {
        var dim = this.dimensionField.getValue();
        if (this.widthField.getValue() < 1 && this.dimensions[dim].width == 'user'
            && this.heightField.getValue() < 1 && this.dimensions[dim].height == 'user'
        ) {
            if (this.widthField.getValue() < 1) {
                this.widthField.markInvalid(trlKwf('Width or height must be higher than 0 when using crop or bestfit.'));
            }
            if (this.heightField.getValue() < 1) {
                this.heightField.markInvalid(trlKwf('Width or height must be higher than 0 when using crop or bestfit.'));
            }
            return false;
        }

        this.widthField.clearInvalid();
        this.heightField.clearInvalid();

        return true;
    },

    onTriggerClick: function() {
        if (!this.sizeWindow) {
            var radios = [];
            for (var i in this.dimensions) {
                radios.push({
                    inputValue: i,
                    boxLabel: this._getDimensionString({dimension: i}),
                    name: 'dimension',
                    listeners: {
                        check: this._enableDisableFields,
                        scope: this
                    }
                });
            }
            this.dimensionField = new Kwf.Form.RadioGroup({
                columns: 1,
                hideLabel: true,
                vertical: false,
                items: radios
            });
            this.dimensionField.on('change', function () {
                //TODO implement disable crop-button if 'original' is selected
            }, this);
            this.widthField = new Ext.form.NumberField({
                fieldLabel: trlKwf('Width'),
                width: 50,
                enableKeyEvents: true,
                validateOnBlur: false,
                validationEvent: false,
                allowNegative: false
            });
            this.heightField = new Ext.form.NumberField({
                fieldLabel: trlKwf('Height'),
                width: 50,
                enableKeyEvents: true,
                validateOnBlur: false,
                validationEvent: false,
                allowNegative: false
            });

            this.widthField.on('blur', this._validateSizes, this);
            this.widthField.on('keyup', this._validateSizes, this);
            this.heightField.on('blur', this._validateSizes, this);
            this.heightField.on('keyup', this._validateSizes, this);
            this.dimensionField.on('change', this._validateSizes, this);

            var button = new Ext.Button({
                text: 'Crop Image',
                handler: function() {
                    var width, height;
                    var preserveRatio = false;
                    var cropData = null;
                    if (this.dimensionField.getValue() == this.getValue().dimension) {
                        // load saved crop-values if not 0
                        if (this.getValue().cropData.width > 0 && this.getValue().cropData.height > 0) {
                            cropData = this.getValue().cropData;
                        }
                    }
                    var dimension = this.dimensions[this.dimensionField.getValue()];
                    if (dimension.height == 'user' && this.heightField.getValue() != '') {
                        height = this.heightField.getValue();
                    } else if (dimension.height > 0) {
                        height = dimension.height;
                    } else {
                        //TODO throw exception no value set
                    }

                    if (dimension.width == 'user' && this.widthField.getValue() != '') {
                        width = this.widthField.getValue();
                    } else if (dimension.width > 0) {
                        width = dimension.width;
                    } else {
                        //TODO throw exception no value set
                    }

                    if (dimension.bestfit == false) {
                        preserveRatio = true;
                    }

                    // call controller to create image with nice size to work with
                    var imageURL = '/kwf/media/upload/download-handy?uploadId='+this.imageData.uploadId+'&hashKey='+this.imageData.hashKey;

                    var cw = new Kwc.Abstract.Image.CropWindow({
                        imageUrl: imageURL,
                        preserveRatio: preserveRatio,
                        outWidth: width,
                        outHeight: height,
                        cropData: cropData,
                        buttons: [{
                            text: trlKwf('OK'),
                            handler: function() {
                                this.cropData = cw.cropData;
                                cw.close();
                            },
                            scope: this
                        },{
                            text: trlKwf('Cancel'),
                            handler: function() {
                                cw.close();
                            },
                            scope: this
                        }]
                    });
                    cw.show();
                },
                scope: this
            });

            this.sizeWindow = new Ext.Window({
                title: trlKwf('Image Size'),
                closeAction: 'hide',
                modal: true,
                width: 350,
                height: 350,
                layout: 'fit',
                items: new Ext.FormPanel({
                    bodyStyle: 'padding: 10px',
                    items: [
                        this.dimensionField,
                        {
                            xtype: 'fieldset',
                            autoHeight: true,
                            title: trlKwf('Size'),
                            items: [
                                this.widthField,<commit_msg>Disable button when original is selected<commit_after>Ext.namespace('Kwc.Abstract.Image');
Kwc.Abstract.Image.DimensionField = Ext.extend(Ext.form.TriggerField, {
    triggerClass: 'x-form-search-trigger',
    width: 300,
    readOnly: true,
    imageData: null,
    cropData: null,

    initComponent: function() {
        Kwc.Abstract.Image.DimensionField.superclass.initComponent.call(this);
    },
    getValue: function() {
        return this.value || {};
    },

    setValue: function(v) {
        this.value = v;
        if (this.rendered) {
            if (v.dimension) {
                this.setRawValue(this._getDimensionString(v));
            } else {
                this.setRawValue('');
            }
        }
    },
    afterRender: function() {
        Kwc.Abstract.Image.DimensionField.superclass.afterRender.call(this);
        if (this.value) {
            this.setValue(this.value);
        }
        this.findParentByType('kwf.autoform').// TODO: retrieve Upload-field more cleanly
            findByType('kwf.file')[0].on('change', function (el, value) {
                this.imageData = value;
        }, this);
    },

    _validateSizes: function()
    {
        var dim = this.dimensionField.getValue();
        if (this.widthField.getValue() < 1 && this.dimensions[dim].width == 'user'
            && this.heightField.getValue() < 1 && this.dimensions[dim].height == 'user'
        ) {
            if (this.widthField.getValue() < 1) {
                this.widthField.markInvalid(trlKwf('Width or height must be higher than 0 when using crop or bestfit.'));
            }
            if (this.heightField.getValue() < 1) {
                this.heightField.markInvalid(trlKwf('Width or height must be higher than 0 when using crop or bestfit.'));
            }
            return false;
        }

        this.widthField.clearInvalid();
        this.heightField.clearInvalid();

        return true;
    },

    onTriggerClick: function() {
        if (!this.sizeWindow) {
            var radios = [];
            for (var i in this.dimensions) {
                radios.push({
                    inputValue: i,
                    boxLabel: this._getDimensionString({dimension: i}),
                    name: 'dimension',
                    listeners: {
                        check: this._enableDisableFields,
                        scope: this
                    }
                });
            }
            this.dimensionField = new Kwf.Form.RadioGroup({
                columns: 1,
                hideLabel: true,
                vertical: false,
                items: radios
            });
            this.widthField = new Ext.form.NumberField({
                fieldLabel: trlKwf('Width'),
                width: 50,
                enableKeyEvents: true,
                validateOnBlur: false,
                validationEvent: false,
                allowNegative: false
            });
            this.heightField = new Ext.form.NumberField({
                fieldLabel: trlKwf('Height'),
                width: 50,
                enableKeyEvents: true,
                validateOnBlur: false,
                validationEvent: false,
                allowNegative: false
            });

            this.widthField.on('blur', this._validateSizes, this);
            this.widthField.on('keyup', this._validateSizes, this);
            this.heightField.on('blur', this._validateSizes, this);
            this.heightField.on('keyup', this._validateSizes, this);
            this.dimensionField.on('change', this._validateSizes, this);

            var button = new Ext.Button({
                text: 'Crop Image',
                handler: function() {
                    var width, height;
                    var preserveRatio = false;
                    var cropData = null;
                    if (this.dimensionField.getValue() == this.getValue().dimension) {
                        // load saved crop-values if not 0
                        if (this.getValue().cropData.width > 0 && this.getValue().cropData.height > 0) {
                            cropData = this.getValue().cropData;
                        }
                    }
                    var dimension = this.dimensions[this.dimensionField.getValue()];
                    if (dimension.height == 'user' && this.heightField.getValue() != '') {
                        height = this.heightField.getValue();
                    } else if (dimension.height > 0) {
                        height = dimension.height;
                    } else {
                        //TODO throw exception no value set
                    }

                    if (dimension.width == 'user' && this.widthField.getValue() != '') {
                        width = this.widthField.getValue();
                    } else if (dimension.width > 0) {
                        width = dimension.width;
                    } else {
                        //TODO throw exception no value set
                    }

                    if (dimension.bestfit == false) {
                        preserveRatio = true;
                    }

                    // call controller to create image with nice size to work with
                    var imageURL = '/kwf/media/upload/download-handy?uploadId='+this.imageData.uploadId+'&hashKey='+this.imageData.hashKey;

                    var cw = new Kwc.Abstract.Image.CropWindow({
                        imageUrl: imageURL,
                        preserveRatio: preserveRatio,
                        outWidth: width,
                        outHeight: height,
                        cropData: cropData,
                        buttons: [{
                            text: trlKwf('OK'),
                            handler: function() {
                                this.cropData = cw.cropData;
                                cw.close();
                            },
                            scope: this
                        },{
                            text: trlKwf('Cancel'),
                            handler: function() {
                                cw.close();
                            },
                            scope: this
                        }]
                    });
                    cw.show();
                },
                scope: this
            });

            this.dimensionField.on('change', function () {
                var dimension = this.dimensions[this.dimensionField.getValue()];
                if (dimension.width == 0 && dimension.height == 0) {
                    button.disable();
                } else {
                    button.enable();
                }
            }, this);

            this.sizeWindow = new Ext.Window({
                title: trlKwf('Image Size'),
                closeAction: 'hide',
                modal: true,
                width: 350,
                height: 350,
                layout: 'fit',
                items: new Ext.FormPanel({
                    bodyStyle: 'padding: 10px',
                    items: [
                        this.dimensionField,
                        {
                            xtype: 'fieldset',
                            autoHeight: true,
                            title: trlKwf('Size'),
                            items: [
                                this.widthField,
{'fraction_non_alphanumeric': 0.10345065087127213, 'fraction_numerical': 0.003994765479716234, 'mean_word_length': 1.1234279028955836, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1867042', 'n_tokens_mistral': 3645, 'n_tokens_neox': 3340, 'n_words': 713}