/**
 * Created by Zhang Haijun on 2018/1/25.
 */
/* global $ */
import Vue from 'vue';
import rules from '../validate/validate-rules.js';
import 'jquery.json-viewer/json-viewer/jquery.json-viewer.js';
import 'jquery.json-viewer/json-viewer/jquery.json-viewer.css';
// 自定义表单验证
export const validate = Vue.directive('validate', {
    bind: function (el, binding, vnode) {
        const obj = binding.value;// 获取表单规则
        $(el).find('.el-form-item').each(function () {
          let target = $(this);
          let key = target.find('.el-form-item__label').attr('for');
          if (!key) return; // 获取验证的表单字段名称，如果没有则不进行验证
          obj[key] = [];
          let validate = (target.attr('validate') || '').split(',');// 获取此字段需要的匹配规则
          if (target.hasClass('is-required')) validate.push('required');
          validate.forEach((row) => {
            if (!row) return;
            let item = Object.assign({}, rules[row]);// 获取匹配规则
            let message = target.attr(row + '-message');
            if (message) item.message = message;// 自定义报错信息
            obj[key].push(item);
        });
      });
    }
});
// json数据展示
export const jsonView = Vue.directive('jsonView', {
    bind: function (el, binding, vnode) {
        try {
          binding.value = JSON.parse(binding.value);
      } catch (e) {}
        $(el).jsonViewer(binding.value);
    }
});
