Blame view

src/components/Common/FormData/FormData.js 10.2 KB
liding committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
module.exports = {
    name: 'list',
    data() {
        return {
            checkall_temp: '_checkall_temp',

            fields: this.FieldList || [],
            editor: this.Editor || {},
            submit_data: this.DefaultValue || {},
            rules: this.Rules || {},

            /**
             * 富文本编辑器信息
             * @type {Object}
             */
            wangEditor: {
                //存富文本实例的对象,支持多个
                //key为富文本对象ID,value为实例
                editor: {},
                //默认为单个编辑器,如果为多个,此值为true,因为多个编辑器时,地图菜单不可用
                many: false,
                has: false,
                //默认显示菜单,支持自定义
                bar: [
                    'source', '|',
                    'bold',
                    'underline',
                    'italic',
                    'strikethrough',
                    'eraser',
                    'forecolor',
                    'bgcolor', '|',
                    'quote',
                    'fontfamily',
                    'fontsize',
                    'head',
                    'unorderlist',
                    'orderlist',
                    'alignleft',
                    'aligncenter',
                    'alignright', '|',
                    'link',
                    'unlink',
                    'table',
                    'emotion', '|',
                    'img',
                    'video',
                    // 'location',
                    'insertcode', '|',
                    'undo',
                    'redo',
                    'fullscreen'
                ],
                temp: {

                },
            },
        }
    },
    methods: {
        /**
         * 初始化编辑器
         * @param  {string} id     编辑器ID属性
         * @param  {object} config 初始化配置
         * @return {object}        所有编辑器所在对象,属性已ID为key
         */
        initEditor(id, config) {
            if (id) {
                this.wangEditor.editor[id] = new wangEditor(id);
                this.wangEditor.temp[id] = {
                    html: '',
                    text: ''
                };
            }
            this.configEditor(id, config).eventEditor(id).createEditor(id);
        },


        /**
         * 配置编辑器参数
         * @param  {string} id     编辑器ID
         * @param  {object} config 编辑器配置信息
         */
        configEditor(id, config) {
            if (id && config) {
                this.wangEditor.editor[id].config.uploadImgFileName = config.name || this.editor.name || 'sls-admin';
                this.wangEditor.editor[id].config.uploadImgUrl = config.url || this.editor.url || '';
                this.wangEditor.editor[id].config.uploadParams = config.params || this.editor.params || {};

                /**
                 * 显示的菜单,分四种情况
                 * 1-只传显示的菜单,直接赋值
                 * 2-只传隐藏的菜单,过滤不需要显示的
                 * 3-显示隐藏都传了,显示优先级高于隐藏优先级
                 * 4-啥都不传,取默认全部显示
                 * @type {object}
                 */
                if (Array.isArray(config.show_bar) && config.show_bar.length) {
                    var bar = config.show_bar;
                } else if (Array.isArray(config.hide_bar) && config.hide_bar.length) {
                    var bar = this.wangEditor.bar.filter((item) => {
                        return config.hide_bar.indexOf(item) === -1;
                    });
                } else if (Array.isArray(this.editor.show_bar) && this.editor.show_bar.length) {
                    var bar = this.editor.show_bar;
                } else if (Array.isArray(this.editor.hide_bar) && this.editor.hide_bar.length) {
                    var bar = this.wangEditor.bar.filter((item) => {
                        return this.editor.hide_bar.indexOf(item) === -1;
                    });
                } else {
                    var bar = this.wangEditor.bar;
                }

                if (this.wangEditor.many === true && bar.indexOf('location') !== -1) {
                    var bar = bar.splice(bar.indexOf('location'), 1);
                }

                this.wangEditor.editor[id].config.menus = bar;
            }

            return this;
        },


        /**
         * 编辑器常用事件
         * @param  {string} id 编辑器ID
         */
        eventEditor(id) {
            var self = this;
            this.wangEditor.editor[id].config.uploadImgFns.onload = function(data) {
                if (data.status === 200) {
                    var originalName = this.uploadImgOriginalName || '';

                    this.command(null, 'insertHtml', '<img src="' + data.data.fileinfo.getSaveName + '" alt="' + originalName + '" style="max-width:100%;"/>');
                } else {
                    if (data.status === 404) {
                        self.$message.error('上传错误信息:token无效!');
                    } else {
                        self.$message.error('上传错误信息:' + data.msg);
                    }
                }

            };

            this.wangEditor.editor[id].config.uploadImgFns.onerror = (xhr) => {
                this.$message.error('上传错误信息:网络错误!');
            };

            this.wangEditor.editor[id].onchange = function() {
                var text = this.$txt.text().replace(/(^\s*)|(\s*$)/g, ""),
                    html = this.$txt.html();

                self.wangEditor.temp[id].html = html;
                self.wangEditor.temp[id].text = text;

                self.$emit('onEditorChange', {
                    id,
                    data: {
                        html,
                        text
                    }
                });
            };

            return this;
        },


        /**
         * 创建编辑器
         * @param  {string} id 编辑器ID
         */
        createEditor(id) {
            this.wangEditor.editor[id].create();
        },


        /**
         * 从字段列表中提取出来表单字段       
         * @return {object} [表单需要的字段]
         */
        deepObj() {
            if (this.fields) {
                var fields = this.fields,
                    k = 0,
                    update = this.submit_data.id ? true : false;
                for (var i = 0; i < fields.length; i++) {
                    var field = fields[i];

                    if (field.value && field.value.constructor === Object) {
                        if (field.checkall && typeof field.checkall === 'object') {
                            var temp = {};
                            temp.text = field.checkall.text;
                            temp.value = field.checkall.value;
                            temp.indeterminate = field.checkall.indeterminate;
                            temp.checkbox_list = field.value.list;
                            temp.checkbox_value = field.value.default;
                            this.$set(this.submit_data, field.key + this.checkall_temp, temp);
                        } else {
                            this.$set(this.submit_data, field.key, field.value.default);
                        }
                    } else {
                        this.$set(this.submit_data, field.key, field.value);
                    }


                    if (field.type && field.type === 'editor') {
                        k++;
                        this.initEditor(field.id, field.config || {});
                        if (k == 2) {
                            this.wangEditor.many = true;
                        }
                        if (k == 1) {
                            this.wangEditor.has = true;
                        }
                    }
                }

                console.log(this.submit_data);
            }
        },


        /**
         * 表单提交事件
         */
        onSubmit(ref) {
            var data = {
                data: this.submit_data,
            };
            if (this.wangEditor.has === true) {
                data.editor_temp_data = this.wangEditor.temp;
            }

            if (this.rules) {
                this.$refs[ref].validate((valid) => {
                    if (valid) {
                        this.$emit('onSubmit', data);
                    }
                });
            } else {
                this.$emit('onSubmit', data);
            }
        },


        onCheckboxChange(key) {
            var checkall_temp = this.submit_data[key + this.checkall_temp];

            if (checkall_temp.checkbox_value.length > 0 && checkall_temp.checkbox_value.length < checkall_temp.checkbox_list.length) {
                checkall_temp.indeterminate = true;
            } else {
                checkall_temp.indeterminate = false;
            }

            checkall_temp.value = checkall_temp.checkbox_value.length === checkall_temp.checkbox_list.length;
        },


        onCheckallChange(key) {
            var checkall_temp = this.submit_data[key + this.checkall_temp];
            checkall_temp.indeterminate = false;

            var value = [];
            if (checkall_temp.value == true) {
                for (var i = 0; i < checkall_temp.checkbox_list.length; i++) {
                    value.push(checkall_temp.checkbox_list[i].value);
                }
            }

            checkall_temp.checkbox_value = value;
        }
    },


    /**
     * ready
     */
    mounted() {
        this.deepObj();
    },


    /**
     * 接收参数
     * @type {Object}
     */
    props: {
        FieldList: {
            type: Array,
            required: true
        },
        Editor: {
            type: Object
        },
        Rules: {
            type: Object
        },
        DefaultValue: {
            type: Object
        }
    },


    /**
     * 监控参数
     * @type {Object}
     */
    watch: {
        FieldList(v) {
            if (v) {
                this.fields = v;
            }
        },
        DefaultValue(v) {
            if (v) {
                this.submit_data = v;
            }
        }
    }
}