<commit_before><?php

namespace flexibuild\file\widgets;

use yii\helpers\Json;

/**
 * The ChangeFormEnctype trait adds methods that allow change form enc-type 
 *  to 'multipart/form-data' automatically with using javascript.
 * 
 * @author SeynovAM <<EMAIL>>
 * 
 * @property \yii\web\View $view The view object that can be used to render views or view files.
 * @method \yii\web\View getView() returns the view object that can be used to render views or view files.
 */
trait FormEnctypeTrait
{
    /**
     * @var boolean whether the widget must to change form enctype automatically or not.
     */
    public $autoSetEnctype = true;

    /**
     * Register scripts that changes form enctype to multipart/form-data.
     * @param string $inputId the id attribute of file input tag.
     */
    public function registerChangeEnctypeScripts($inputId)
    {
        if (!$this->autoSetEnctype) {
            return;
        }

        // @todo must be without jquery
        $js = 'jQuery("#" + ' . Json::encode($inputId) . ').closest("form").attr("enctype", "multipart/form-data");';
        $this->getView()->registerJs($js);
    }
}<commit_msg>Added non-jQuery logic for FormEnctypeTrait.<commit_after><?php

namespace flexibuild\file\widgets;

use yii\helpers\Json;

/**
 * The ChangeFormEnctype trait adds methods that allow change form enc-type 
 *  to 'multipart/form-data' automatically with using javascript.
 * 
 * @author SeynovAM <<EMAIL>>
 * 
 * @property \yii\web\View $view The view object that can be used to render views or view files.
 * @method \yii\web\View getView() returns the view object that can be used to render views or view files.
 */
trait FormEnctypeTrait
{
    /**
     * @var boolean whether the widget must to change form enctype automatically or not.
     */
    public $autoSetEnctype = true;

    /**
     * @var boolean whether the widget may to use jQuery library. True by default.
     */
    public $useJQuery = true;

    /**
     * Register scripts that changes form enctype to multipart/form-data.
     * @param string $inputId the id attribute of file input tag.
     */
    public function registerChangeEnctypeScripts($inputId)
    {
        if (!$this->autoSetEnctype) {
            return;
        }
        $encodedInputId = Json::encode($inputId);

        if ($this->useJQuery) {
            $js = "jQuery('#' + $encodedInputId).closest('form').attr('enctype', 'multipart/form-data');";
            $this->getView()->registerJs($js);
            return;
        }

        $js = <<<JS
            (function () {
                var onReady = function () {
                        var input = document.getElementById($encodedInputId), parentNode = input;
                        while ((parentNode = parentNode.parentNode) && (parentNode.tagName.toUpperCase() !== 'FORM'));
                        parentNode && parentNode.setAttribute && parentNode.setAttribute("enctype", "multipart/form-data");
                    },
                    completed = function () {
                        document.removeEventListener && document.removeEventListener("DOMContentLoaded", completed, false);
                        window.removeEventListener && window.removeEventListener("load", completed, false);
                        onReady();
                    };
                if (document.readyState === "complete") {
                    setTimeout(onReady);
                } else {
                    document.addEventListener && document.addEventListener("DOMContentLoaded", completed, false);
                    window.addEventListener && window.addEventListener("load", completed, false);
                }
            })();
JS;
        $this->getView()->registerJs($js, \yii\web\View::POS_END);
    }
}
{'fraction_non_alphanumeric': 0.11778782238766286, 'fraction_numerical': 0.0, 'mean_word_length': 2.314537444933921, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 16, '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': '2462357', 'n_tokens_mistral': 1021, 'n_tokens_neox': 980, 'n_words': 286}