vova07\fileapi\Widget::registerDefaultCallbacks PHP Method

registerDefaultCallbacks() protected method

Register default widget callbacks
protected registerDefaultCallbacks ( )
    protected function registerDefaultCallbacks()
    {
        // File complete handler
        $this->callbacks['filecomplete'][] = new JsExpression('function (evt, uiEvt) {' . 'if (uiEvt.result.error) {' . 'alert(uiEvt.result.error);' . '} else {' . 'jQuery(this).find("input[type=\\"hidden\\"]").val(uiEvt.result.name);' . 'jQuery(this).find("[data-fileapi=\\"browse-text\\"]").addClass("hidden");' . 'jQuery(this).find("[data-fileapi=\\"delete\\"]").attr("data-fileapi-uid", FileAPI.uid(uiEvt.file));' . '}' . '}');
        if ($this->crop === true) {
            $view = $this->getView();
            $selector = $this->getSelector();
            $jcropSettings = Json::encode($this->jcropSettings);
            if ($this->cropResizeWidth !== null && $this->cropResizeHeight !== null) {
                $cropResizeJs = "el.fileapi('resize', ufile, {$this->cropResizeWidth}, {$this->cropResizeHeight});";
            } elseif ($this->cropResizeWidth !== null && $this->cropResizeHeight == null) {
                $cropResizeJs = "el.fileapi('resize', ufile, {$this->cropResizeWidth}, ((coordinates.h * {$this->cropResizeWidth})/coordinates.w));";
            } elseif ($this->cropResizeWidth == null && $this->cropResizeHeight !== null) {
                $cropResizeJs = "el.fileapi('resize', ufile, ((coordinates.w * {$this->cropResizeHeight})/coordinates.h), {$this->cropResizeHeight});";
            } elseif ($this->cropResizeMaxWidth !== null && $this->cropResizeMaxHeight !== null) {
                $cropResizeJs = "if(coordinates.w > {$this->cropResizeMaxWidth}) el.fileapi('resize', ufile, {$this->cropResizeMaxWidth}, ((coordinates.h * {$this->cropResizeMaxWidth})/coordinates.w));";
                $cropResizeJs .= "else if(coordinates.h > {$this->cropResizeMaxHeight}) el.fileapi('resize', ufile, ((coordinates.w * {$this->cropResizeMaxHeight})/coordinates.h), {$this->cropResizeMaxHeight});";
            } elseif ($this->cropResizeMaxWidth !== null && $this->cropResizeMaxHeight == null) {
                $cropResizeJs = "if(coordinates.w > {$this->cropResizeMaxWidth}) el.fileapi('resize', ufile, {$this->cropResizeMaxWidth}, ((coordinates.h * {$this->cropResizeMaxWidth})/coordinates.w));";
            } elseif ($this->cropResizeMaxWidth == null && $this->cropResizeMaxHeight !== null) {
                $cropResizeJs = "if(coordinates.h > {$this->cropResizeMaxHeight}) el.fileapi('resize', ufile, ((coordinates.w * {$this->cropResizeMaxHeight})/coordinates.h), {$this->cropResizeMaxHeight});";
            } else {
                $cropResizeJs = '';
            }
            // Add event handler for crop button
            $view->registerJs('jQuery(document).on("click", "#modal-crop .crop", function() {' . '$("#' . $selector . '").fileapi("upload");' . 'jQuery("#modal-crop").modal("hide");' . '});');
            // Crop event handler
            $this->callbacks['select'] = new JsExpression('function (evt, ui) {' . 'var ufile = ui.files[0],' . 'jcropSettings = ' . $jcropSettings . ',' . 'el = jQuery(this);' . 'if (ufile) {' . 'jcropSettings.file = ufile;' . 'jcropSettings.onSelect = function (coordinates) {' . '$("#' . $selector . '").fileapi("crop", ufile, coordinates);' . $cropResizeJs . '};' . 'FileAPI.getInfo (ufile, function (err, info) {' . 'if (!err) { var coordinates = { w: info.width, h: info.height }; ' . $cropResizeJs . '}' . '});;' . 'jQuery("#modal-crop").modal("show");' . 'setTimeout(function () {' . '$("#modal-preview").cropper(jcropSettings);' . '}, 700);' . '}' . '}');
        }
    }