RSS Git Download  Clone
Raw Blame History 7kB 173 lines
p3xr.ng.factory('p3xrDialogJsonEditor', function (p3xrCommon, $mdDialog, $timeout) {
    const debounce = require('lodash/debounce')

    return new function () {

        this.show = async(options) => {
            /* webpackChunkName: "editor" */
          await import(
                /* webpackPrefetch: true */
                "../../editor"
            )




                return $mdDialog.show({
                    controller: function ($scope, $mdDialog, p3xrCommon, $rootScope, p3xrTheme, $mdMedia) {

                        $rootScope.$broadcast('p3xr-main-resizer', {
                            drag: false
                        })



                        let editor
                        let obj

                        try {
                            obj = JSON.parse(options.value)
                            $scope.isJson = true
                        } catch (e) {
                            obj = undefined
                            $scope.isJson = false
                        }

                        if ($scope.isJson) {
                            // https://webpack.js.org/guides/code-splitting/
                            // /* webpackMode: "lazy" */
                            const execAsync = async() => {
                                try {


                                    $timeout(() => {

                                        const container = document.getElementById("p3xr-jsoneditor")

                                        const options = {
                                            //sortObjectKeys: false,
                                            limitDragging: false,
                                            modes: ['tree', 'code', 'view', 'preview'],
                                            //history: false,
                                            mode: 'code',
                                            //search: true,
                                            //mainMenuBar: false,
                                            language: p3xr.settings.getJSONEditorLanguage(),
                                            //enableSort: false,
                                            //enableTransform: false,
                                            //ace: ace,
                                            indentation: p3xr.settings.jsonFormat,
                                            //theme: p3xrTheme.isDark() ? 'ace/theme/twilight' : 'ace/theme/github'
                                        }
                                        if (p3xrTheme.isDark()) {
                                            options.theme = 'ace/theme/twilight'
                                        }
                                        /*
                                        if (JSON.stringify(obj).length > 10240) {
                                            p3xrCommon.toast({
                                                message: p3xr.strings.label.bigJson,
                                                hideDelay: 10000
                                            })
                                        }
                                         */
                                        editor = new JSONEditor(container, options, obj)

                                    })
                                } catch(e) {
                                    p3xrCommon.generalHandleError(e)
                                }

                            }
                            execAsync()
                        }

                        const close = (event) => {
                            const keycode = event.which || event.keyCode;
                            // If this is the escape key
                            //console.log('close')
                            if ( keycode === 27 ) {
                                event.preventDefault();
                                event.stopPropagation();
                               // $mdDialog.cancel();
                                const pico = $('.pico-close')
                                if (pico.length > 0) {
                                    pico.click()
                                } else {
                                    $mdDialog.cancel();
                                }
                            }

                        }
                        document.documentElement.addEventListener('keydown', close, true)

                        const resize = () => {
                            if ($mdMedia('(max-width:959px)')) {
                                $scope.minHeight = '100%'
                            } else {
                                $scope.minHeight = `${Math.max(10, window.innerHeight - 100)}px`
                            }
                        }

                        resize()
                        const editorResize = debounce(() => {
                            if (editor && editor.aceEditor) {
                                console.log('resize json editor ace resize')
                                resize()
                                $scope.$digest()
                                editor.aceEditor.resize()
                            }
                        }, p3xr.settings.debounce)

                        $window.on('resize', editorResize)

                        $scope.$on('$destroy', () => {
                            $window.off('resize', editorResize)
                            if (editor){
                                editor.destroy()
                                editor = undefined
                            }
                            $rootScope.$broadcast('p3xr-main-resizer', {
                                drag: true
                            })
                            document.documentElement.removeEventListener('keydown', close, true)
                        })


                        // Promise reject
                        $scope.ok = function () {
                            $mdDialog.cancel();
                        };

                        // Promise resolve
                        $scope.save = function ({ format }) {
                            try {
                                $mdDialog.hide({
                                    obj: JSON.stringify(editor.get(), null, format ? p3xr.settings.jsonFormat : 0)
                                });
                            } catch(e) {
                                p3xrCommon.generalHandleError(e)
                            }
                        };

                        /*
                        // Promise resolve - with result
                        $scope.answer = function (answer) {
                            $mdDialog.hide(answer);
                        };
                        */

                    },
                    template: require('./p3xr-dialog-json-editor.html'),
                    parent: angular.element(document.body),
                    targetEvent: options.$event,
                    clickOutsideToClose: false,
                    fullscreen: true, // Only for -xs, -sm breakpoints.
                    multiple: true,
                    escapeToClose: false,
                })
        }

    }

});