util.js 4.8 KB
Newer Older
L
ligang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import _ from 'lodash'
G
gongzijian 已提交
19
import i18n from '@/module/i18n'
L
ligang 已提交
20 21 22
import store from '@/conf/home/store'

/**
G
gongzijian 已提交
23
 * Node, to array
L
ligang 已提交
24 25
 */
const rtTargetarrArr = (id) => {
26
  const ids = $(`#${id}`).attr('data-targetarr')
27
  return ids ? ids.split(',') : []
L
ligang 已提交
28 29 30
}

/**
G
gongzijian 已提交
31
 * Store node id to targetarr
L
ligang 已提交
32 33
 */
const saveTargetarr = (valId, domId) => {
34 35
  const $target = $(`#${domId}`)
  const targetStr = $target.attr('data-targetarr') ? $target.attr('data-targetarr') + `,${valId}` : `${valId}`
L
ligang 已提交
36 37 38
  $target.attr('data-targetarr', targetStr)
}

G
gongzijian 已提交
39
const rtBantpl = () => {
40
  return `<em class="ans-icon-forbidden" data-toggle="tooltip" data-html="true" data-container="body" data-placement="left" title="${i18n.$t('Prohibition execution')}"></em>`
G
gongzijian 已提交
41 42
}

L
ligang 已提交
43
/**
G
gongzijian 已提交
44
 * return node html
L
ligang 已提交
45
 */
46
const rtTasksTpl = ({ id, name, x, y, targetarr, isAttachment, taskType, runFlag, nodenumber, successNode, failedNode }) => {
47
  let tpl = ''
48
  tpl += `<div class="w jtk-draggable jtk-droppable jtk-endpoint-anchor jtk-connected ${isAttachment ? 'jtk-ep' : ''}" data-targetarr="${targetarr || ''}" data-successNode="${successNode || ''}" data-failedNode="${failedNode || ''}" data-nodenumber="${nodenumber || 0}" data-tasks-type="${taskType}" id="${id}" style="left: ${x}px; top: ${y}px;">`
49 50
  tpl += '<div>'
  tpl += '<div class="state-p"></div>'
51 52
  tpl += `<div class="icos icos-${taskType}"></div>`
  tpl += `<span class="name-p">${name}</span>`
53 54 55
  tpl += '</div>'
  tpl += '<div class="ep"></div>'
  tpl += '<div class="ban-p">'
56 57 58
  if (runFlag === 'FORBIDDEN') {
    tpl += rtBantpl()
  }
59 60
  tpl += '</div>'
  tpl += '</div>'
G
gongzijian 已提交
61

L
ligang 已提交
62 63 64 65
  return tpl
}

/**
G
gongzijian 已提交
66
 * Get all tasks nodes
L
ligang 已提交
67 68
 */
const tasksAll = () => {
69
  const a = []
L
ligang 已提交
70
  $('#canvas .w').each(function (idx, elem) {
71
    const e = $(elem)
L
ligang 已提交
72 73 74 75
    a.push({
      id: e.attr('id'),
      name: e.find('.name-p').text(),
      targetarr: e.attr('data-targetarr') || '',
76
      nodenumber: e.attr('data-nodenumber'),
L
ligang 已提交
77 78 79 80 81 82 83 84
      x: parseInt(e.css('left'), 10),
      y: parseInt(e.css('top'), 10)
    })
  })
  return a
}

/**
G
gongzijian 已提交
85 86
 * Determine if name is in the current dag map
 * rely dom / backfill
L
ligang 已提交
87 88 89 90 91 92 93 94 95 96
 */
const isNameExDag = (name, rely) => {
  if (rely === 'dom') {
    return _.findIndex(tasksAll(), v => v.name === name) !== -1
  } else {
    return _.findIndex(store.state.dag.tasks, v => v.name === name) !== -1
  }
}

/**
G
gongzijian 已提交
97
 * Change svg line color
L
ligang 已提交
98 99
 */
const setSvgColor = (e, color) => {
G
gongzijian 已提交
100
  // Traverse clear all colors
L
ligang 已提交
101 102
  $('.jtk-connector').each((i, o) => {
    _.map($(o)[0].childNodes, v => {
B
fix  
break60 已提交
103 104 105 106 107 108 109 110 111 112 113 114
      if($(v).attr('fill') ==='#ccc') {
        $(v).attr('fill', '#2d8cf0')
      }
      if($(v).attr('fill') ==='#4caf50') {
        $(v).attr('fill','#4caf50').attr('stroke', '#4caf50').attr('stroke-width', 2)
        $(v).prev().attr('stroke', '#4caf50').attr('stroke-width', 2)
      } else if($(v).attr('fill') ==='#252d39') {
        $(v).attr('stroke', '#252d39').attr('stroke-width', 2)
        $(v).prev().attr('stroke', '#252d39').attr('stroke-width', 2)
      } else {
        $(v).attr('stroke', '#2d8cf0').attr('stroke-width', 2)
      }
L
ligang 已提交
115 116 117
    })
  })

G
gongzijian 已提交
118
  // Add color to the selection
L
ligang 已提交
119
  _.map($(e.canvas)[0].childNodes, (v, i) => {
B
fix  
break60 已提交
120 121 122
    if($(v).attr('fill') ==='#2d8cf0') {
      $(v).attr('fill', '#ccc')
    }
123
    $(v).attr('stroke', '#ccc')
L
ligang 已提交
124 125 126 127 128 129 130
    if ($(v).attr('class')) {
      $(v).attr('stroke-width', 2)
    }
  })
}

/**
G
gongzijian 已提交
131
 * Get all node ids
L
ligang 已提交
132 133
 */
const allNodesId = () => {
134
  const idArr = []
L
ligang 已提交
135
  $('.w').each((i, o) => {
136 137
    const $obj = $(o)
    const $span = $obj.find('.name-p').text()
L
ligang 已提交
138 139 140 141 142 143 144 145 146
    if ($span) {
      idArr.push({
        id: $obj.attr('id'),
        name: $span
      })
    }
  })
  return idArr
}
S
satcblue 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159
/**
 * compute scale,because it cant get from jquery directly
 * @param el element
 * @returns {boolean|number}
 */
const computeScale = function (el) {
  const matrix = el.css('transform')
  if (!matrix || matrix === 'none') {
    return false
  }
  const values = matrix.split('(')[1].split(')')[0].split(',')
  return Math.sqrt(values[0] * values[0] + values[1] * values[1])
}
L
ligang 已提交
160 161 162 163 164 165 166 167

export {
  rtTargetarrArr,
  saveTargetarr,
  rtTasksTpl,
  tasksAll,
  isNameExDag,
  setSvgColor,
G
gongzijian 已提交
168
  allNodesId,
S
satcblue 已提交
169 170
  rtBantpl,
  computeScale
L
ligang 已提交
171
}