<commit_before>﻿using jQueryApi;
using Serenity.ComponentModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Serenity
{
    [Editor, DisplayName("Resim Yükleme"), OptionsType(typeof(ImageUploadEditorOptions))]
    [Element("<div/>")]
    public class ImageUploadEditor : Widget<ImageUploadEditorOptions>, IGetEditValue, ISetEditValue, IReadOnly
    {
        private UploadedFile entity;
        private Toolbar toolbar;
        private jQueryObject fileSymbols;
        private jQueryObject uploadInput;

        public ImageUploadEditor(jQueryObject div, ImageUploadEditorOptions opt)
            : base(div, opt)
        {
            div.AddClass("s-ImageUploadEditor");

            if (options.OriginalNameProperty.IsEmptyOrNull())
                div.AddClass("hide-original-name");

            var self = this;

            toolbar = new Toolbar(J("<div/>").AppendTo(this.Element), new ToolbarOptions
            {
                Buttons = new List<ToolButton>
                {
                    new ToolButton {
                        Title = "Dosya Seç",
                        CssClass = "add-file-button",
                        OnClick = delegate {
                        }
                    },
                    new ToolButton {
                        Title = "Kaldır",
                        CssClass = "delete-button",
                        OnClick = delegate {
                            self.entity = null;
                            self.Populate();
                            self.UpdateInterface();
                        }
                    }
                }
            });

            var progress = J("<div><div></div></div>").AddClass("upload-progress")
                .PrependTo(toolbar.Element);

            var addFileButton = toolbar.FindButton("add-file-button");
            uploadInput = UploadHelper.AddUploadInput(new UploadInputOptions
            {
                Container = addFileButton,
                InputName = this.uniqueName,
                Progress = progress,
                FileDone = (response, name, data) =>
                {
                    if (!UploadHelper.CheckImageConstraints(response, options))
                        return;

                    var newEntity = new UploadedFile
                    {
                        OriginalName = name,
                        Filename = response.TemporaryFile
                    };
                    
                    self.entity = newEntity;
                    self.Populate();
                    self.UpdateInterface();
                }
            });

            fileSymbols = jQuery.FromHtml("<ul/>")
                .AppendTo(this.element);

            this.UpdateInterface();
        }

        private void Populate()
        {
            bool displayOriginalName = !options.OriginalNameProperty.IsTrimmedEmpty();

            if (entity == null)
                UploadHelper.PopulateFileSymbols(fileSymbols, null, displayOriginalName);
            else
                UploadHelper.PopulateFileSymbols(fileSymbols, new List<UploadedFile> { entity }, displayOriginalName);
        }

        private void UpdateInterface()
        {
            var addButton = this.toolbar.FindButton("add-file-button");
            var delButton = this.toolbar.FindButton("delete-button");
            addButton.ToggleClass("disabled", ReadOnly);
            delButton.ToggleClass("disabled", ReadOnly || entity == null);
        }

        public bool ReadOnly
        {
            get { return !Script.IsNullOrUndefined(uploadInput.GetAttribute("disabled")); }
            set
            {
                if (this.ReadOnly != value)
                {
                    if (value)
                        uploadInput.Attribute("disabled", "disabled");
                    else
                        uploadInput.RemoveAttr("disabled");
                    UpdateInterface();
                }
            }
        }

        public UploadedFile Value
        {
            get
            {
                if (entity == null)
                    return null;

                var copy = jQuery.ExtendObject(new UploadedFile(), this.entity);
                return copy;
            }
            set
            {
                if (value != null)
                {
                    if (Script.IsNullOrUndefined(value.Filename))<commit_msg>image upload editor virtual override edilebilir hale getirildi.<commit_after>﻿using jQueryApi;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Serenity
{
    [Editor, DisplayName("Resim Yükleme"), OptionsType(typeof(ImageUploadEditorOptions))]
    [Element("<div/>")]
    public class ImageUploadEditor : Widget<ImageUploadEditorOptions>, IGetEditValue, ISetEditValue, IReadOnly
    {
        protected UploadedFile entity;
        protected Toolbar toolbar;
        protected jQueryObject fileSymbols;
        protected jQueryObject uploadInput;

        public ImageUploadEditor(jQueryObject div, ImageUploadEditorOptions opt)
            : base(div, opt)
        {
            div.AddClass("s-ImageUploadEditor");

            if (options.OriginalNameProperty.IsEmptyOrNull())
                div.AddClass("hide-original-name");

            var self = this;

            toolbar = new Toolbar(J("<div/>").AppendTo(this.Element), new ToolbarOptions
            {
                Buttons = GetToolButtons()
            });

            var progress = J("<div><div></div></div>").AddClass("upload-progress")
                .PrependTo(toolbar.Element);

            var addFileButton = toolbar.FindButton("add-file-button");
            uploadInput = UploadHelper.AddUploadInput(new UploadInputOptions
            {
                Container = addFileButton,
                InputName = this.uniqueName,
                Progress = progress,
                FileDone = (response, name, data) =>
                {
                    if (!UploadHelper.CheckImageConstraints(response, options))
                        return;

                    var newEntity = new UploadedFile
                    {
                        OriginalName = name,
                        Filename = response.TemporaryFile
                    };
                    
                    self.entity = newEntity;
                    self.Populate();
                    self.UpdateInterface();
                }
            });

            fileSymbols = jQuery.FromHtml("<ul/>")
                .AppendTo(this.element);

            this.UpdateInterface();
        }

        protected virtual List<ToolButton> GetToolButtons()
        {
            var self = this;

            return new List<ToolButton>
            {
                new ToolButton
                {
                    Title = "Dosya Seç",
                    CssClass = "add-file-button",
                    OnClick = delegate {
                    }
                },
                new ToolButton
                {
                    Title = "Kaldır",
                    CssClass = "delete-button",
                    OnClick = delegate
                    {
                        self.entity = null;
                        self.Populate();
                        self.UpdateInterface();
                    }
                }
            };
        }

        protected virtual void Populate()
        {
            bool displayOriginalName = !options.OriginalNameProperty.IsTrimmedEmpty();

            if (entity == null)
                UploadHelper.PopulateFileSymbols(fileSymbols, null, displayOriginalName);
            else
                UploadHelper.PopulateFileSymbols(fileSymbols, new List<UploadedFile> { entity }, displayOriginalName);
        }

        protected virtual void UpdateInterface()
        {
            var addButton = this.toolbar.FindButton("add-file-button");
            var delButton = this.toolbar.FindButton("delete-button");
            addButton.ToggleClass("disabled", ReadOnly);
            delButton.ToggleClass("disabled", ReadOnly || entity == null);
        }

        public bool ReadOnly
        {
            get { return !Script.IsNullOrUndefined(uploadInput.GetAttribute("disabled")); }
            set
            {
                if (this.ReadOnly != value)
                {
                    if (value)
                        uploadInput.Attribute("disabled", "disabled");
                    else
                        uploadInput.RemoveAttr("disabled");
                    UpdateInterface();
                }
            }
        }

        public virtual UploadedFile Value
        {
            get
            {
                if (entity == null)
                    return null;

                var copy = jQuery.ExtendObject(new UploadedFile(), this.entity);
                return copy;
            }
            set
            {
                if (value != null)
                {
                    if (Script.IsNullOrUndefined(value.Filename))
{'fraction_non_alphanumeric': 0.08700404415783146, 'fraction_numerical': 0.0, 'mean_word_length': 1.3030455575132143, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4432381', 'n_tokens_mistral': 2146, 'n_tokens_neox': 2004, 'n_words': 402}