util.js 2.72 KB
Newer Older
葛齐林's avatar
葛齐林 committed
1 2 3 4 5 6 7 8 9 10 11 12 13
import log from './util.log.js'
import cookies from './util.cookies.js'

let util = {
  cookies,
  log
}

/**
 * @description 更新标题
 * @param {String} title 标题
 */
util.title = function (titleText) {
xiexingan's avatar
xiexingan committed
14
  const processTitle = 'AI节能管理系统'
葛齐林's avatar
葛齐林 committed
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
  window.document.title = `${processTitle}${titleText ? ` | ${titleText}` : ''}`
}

/**
 * @description 打开新页面
 * @param {String} url 地址
 */
util.open = function (url) {
  var a = document.createElement('a')
  a.setAttribute('href', url)
  a.setAttribute('target', '_blank')
  a.setAttribute('id', 'd2admin-menu-link')
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(document.getElementById('d2admin-menu-link'))
}

 /**
 * @about Tree 数据格式转化
 * @param rows:json数据对象
 * @param idFieldName:表id的字段名
 * @param pidFieldName:表父级id的字段名
 * @param fileds:要显示的字段,多个字段用逗号分隔
 *
 * @return 无
 * @author dake
 */
util.ConvertToTreeJson = function (rows, idFieldName, pidFieldName, fileds) {
   function exists(rows, ParentId) {
        for (var i = 0; i < rows.length; i++) {
xiexingan's avatar
xiexingan committed
45
            if (rows[i][idFieldName] == ParentId) { return true }
葛齐林's avatar
葛齐林 committed
46
        }
xiexingan's avatar
xiexingan committed
47
        return false
葛齐林's avatar
葛齐林 committed
48
    }
xiexingan's avatar
xiexingan committed
49
    var nodes = []
葛齐林's avatar
葛齐林 committed
50 51
    // get the top level nodes
    for (var i = 0; i < rows.length; i++) {
xiexingan's avatar
xiexingan committed
52
        var row = rows[i]
葛齐林's avatar
葛齐林 committed
53 54 55 56
        if (!exists(rows, row[pidFieldName])) {
            var data = {
                id: row[idFieldName]
            }
xiexingan's avatar
xiexingan committed
57 58 59
            var arrFiled = fileds.split(',')
            for (var j = 0; j < arrFiled.length; j++) {
                if (arrFiled[j] != idFieldName) { data[arrFiled[j]] = row[arrFiled[j]] }
葛齐林's avatar
葛齐林 committed
60
            }
xiexingan's avatar
xiexingan committed
61
            nodes.push(data)
葛齐林's avatar
葛齐林 committed
62 63 64
        }
    }

xiexingan's avatar
xiexingan committed
65
    var toDo = []
葛齐林's avatar
葛齐林 committed
66
    for (var i = 0; i < nodes.length; i++) {
xiexingan's avatar
xiexingan committed
67
        toDo.push(nodes[i])
葛齐林's avatar
葛齐林 committed
68 69 70
    }

    while (toDo.length) {
xiexingan's avatar
xiexingan committed
71
        var node = toDo.shift() // the parent node
葛齐林's avatar
葛齐林 committed
72 73
        // get the children nodes
        for (var i = 0; i < rows.length; i++) {
xiexingan's avatar
xiexingan committed
74
            var row = rows[i]
葛齐林's avatar
葛齐林 committed
75 76 77
            if (row[pidFieldName] == node.id) {
                var child = {
                    id: row[idFieldName]
xiexingan's avatar
xiexingan committed
78 79
                }
                var arrFiled = fileds.split(',')
葛齐林's avatar
葛齐林 committed
80 81
                for (var j = 0; j < arrFiled.length; j++) {
                    if (arrFiled[j] != idFieldName) {
xiexingan's avatar
xiexingan committed
82
                        child[arrFiled[j]] = row[arrFiled[j]]
葛齐林's avatar
葛齐林 committed
83 84 85
                    }
                }
                if (node.children) {
xiexingan's avatar
xiexingan committed
86
                    node.children.push(child)
葛齐林's avatar
葛齐林 committed
87
                } else {
xiexingan's avatar
xiexingan committed
88
                    node.children = [child]
葛齐林's avatar
葛齐林 committed
89
                }
xiexingan's avatar
xiexingan committed
90
                toDo.push(child)
葛齐林's avatar
葛齐林 committed
91 92 93
            }
        }
    }
xiexingan's avatar
xiexingan committed
94
    return nodes
葛齐林's avatar
葛齐林 committed
95 96 97
}

export default util