util.js 3.7 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 27
  let ids = $(`#${id}`).attr('data-targetarr')
  return ids ? ids.split(',') : []
L
ligang 已提交
28 29 30
}

/**
G
gongzijian 已提交
31
 * Store node id to targetarr
L
ligang 已提交
32 33 34 35 36 37 38
 */
const saveTargetarr = (valId, domId) => {
  let $target = $(`#${domId}`)
  let targetStr = $target.attr('data-targetarr') ? $target.attr('data-targetarr') + `,${valId}` : `${valId}`
  $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 }) => {
L
ligang 已提交
47 48 49
  let tpl = ``
  tpl += `<div class="w jtk-draggable jtk-droppable jtk-endpoint-anchor jtk-connected ${isAttachment ? 'jtk-ep' : ''}" data-targetarr="${targetarr || ''}" data-tasks-type="${taskType}" id="${id}" style="left: ${x}px; top: ${y}px;">`
  tpl += `<div>`
50 51 52
  tpl += `<div class="state-p"></div>`
  tpl += `<div class="icos icos-${taskType}"></div>`
  tpl += `<span class="name-p">${name}</span>`
L
ligang 已提交
53 54
  tpl += `</div>`
  tpl += `<div class="ep"></div>`
55 56 57 58 59
  tpl += `<div class="ban-p">`
  if (runFlag === 'FORBIDDEN') {
    tpl += rtBantpl()
  }
  tpl += `</div>`
L
ligang 已提交
60
  tpl += `</div>`
G
gongzijian 已提交
61

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

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

/**
G
gongzijian 已提交
84 85
 * Determine if name is in the current dag map
 * rely dom / backfill
L
ligang 已提交
86 87 88 89 90 91 92 93 94 95
 */
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 已提交
96
 * Change svg line color
L
ligang 已提交
97 98
 */
const setSvgColor = (e, color) => {
G
gongzijian 已提交
99
  // Traverse clear all colors
L
ligang 已提交
100 101 102 103 104 105
  $('.jtk-connector').each((i, o) => {
    _.map($(o)[0].childNodes, v => {
      $(v).attr('fill', '#555').attr('stroke', '#555').attr('stroke-width', 2)
    })
  })

G
gongzijian 已提交
106
  // Add color to the selection
L
ligang 已提交
107 108 109 110 111 112 113 114 115
  _.map($(e.canvas)[0].childNodes, (v, i) => {
    $(v).attr('fill', color).attr('stroke', color)
    if ($(v).attr('class')) {
      $(v).attr('stroke-width', 2)
    }
  })
}

/**
G
gongzijian 已提交
116
 * Get all node ids
L
ligang 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
 */
const allNodesId = () => {
  let idArr = []
  $('.w').each((i, o) => {
    let $obj = $(o)
    let $span = $obj.find('.name-p').text()
    if ($span) {
      idArr.push({
        id: $obj.attr('id'),
        name: $span
      })
    }
  })
  return idArr
}

export {
  rtTargetarrArr,
  saveTargetarr,
  rtTasksTpl,
  tasksAll,
  isNameExDag,
  setSvgColor,
G
gongzijian 已提交
140 141
  allNodesId,
  rtBantpl
L
ligang 已提交
142
}