shell.vue 10.7 KB
Newer Older
K
khadgarmage 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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.
 */
L
ligang 已提交
17 18 19
<template>
  <div class="shell-model">
    <m-list-box>
G
i18n  
gongzijian 已提交
20
      <div slot="text">{{$t('Script')}}</div>
L
ligang 已提交
21 22 23
      <div slot="content">
        <div class="from-mirror">
          <textarea
24 25 26
            id="code-shell-mirror"
            name="code-shell-mirror"
            style="opacity: 0">
L
ligang 已提交
27
          </textarea>
28
          <a class="ans-modal-box-max">
29
            <em class="el-icon-full-screen" @click="setEditorVal"></em>
30
          </a>
L
ligang 已提交
31 32 33 34
        </div>
      </div>
    </m-list-box>
    <m-list-box>
B
break60 已提交
35 36
      <div slot="text">{{$t('Resources')}}</div>
      <div slot="content">
37
        <treeselect  v-model="resourceList" :multiple="true" maxHeight="200" :options="options" :normalizer="normalizer" :disabled="isDetails" :value-consists-of="valueConsistsOf" :placeholder="$t('Please select resources')">
B
break60 已提交
38 39 40 41
          <div slot="value-label" slot-scope="{ node }">{{ node.raw.fullName }}</div>
        </treeselect>
      </div>
    </m-list-box>
L
ligang 已提交
42
    <m-list-box>
G
i18n  
gongzijian 已提交
43
      <div slot="text">{{$t('Custom Parameters')}}</div>
L
ligang 已提交
44 45 46 47 48 49 50 51 52
      <div slot="content">
        <m-local-params
                ref="refLocalParams"
                @on-local-params="_onLocalParams"
                :udp-list="localParams"
                :hide="false">
        </m-local-params>
      </div>
    </m-list-box>
53 54
    <el-dialog
      :visible.sync="scriptBoxDialog"
55
      append-to-body="true"
56 57 58
      width="80%">
      <m-script-box :item="item" @getSriptBoxValue="getSriptBoxValue" @closeAble="closeAble"></m-script-box>
    </el-dialog>
L
ligang 已提交
59 60 61 62 63 64
  </div>
</template>
<script>
  import _ from 'lodash'
  import i18n from '@/module/i18n'
  import mListBox from './_source/listBox'
65
  import mScriptBox from './_source/scriptBox'
L
ligang 已提交
66 67
  import mLocalParams from './_source/localParams'
  import disabledState from '@/module/mixin/disabledState'
B
break60 已提交
68 69
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
L
ligang 已提交
70 71 72 73 74 75 76 77
  import codemirror from '@/conf/home/pages/resource/pages/file/pages/_source/codemirror'

  let editor

  export default {
    name: 'shell',
    data () {
      return {
B
break60 已提交
78
        valueConsistsOf: 'LEAF_PRIORITY',
L
ligang 已提交
79 80 81 82 83
        // script
        rawScript: '',
        // Custom parameter
        localParams: [],
        // resource(list)
Z
zhukai 已提交
84 85
        resourceList: [],
        // Cache ResourceList
B
break60 已提交
86 87 88
        cacheResourceList: [],
        // define options
        options: [],
89
        normalizer (node) {
B
break60 已提交
90 91 92
          return {
            label: node.name
          }
B
break60 已提交
93 94
        },
        allNoResources: [],
95 96 97
        noRes: [],
        item: '',
        scriptBoxDialog: false
L
ligang 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110
      }
    },
    mixins: [disabledState],
    props: {
      backfillItem: Object
    },
    methods: {
      /**
       * return localParams
       */
      _onLocalParams (a) {
        this.localParams = a
      },
111
      setEditorVal () {
112 113 114 115 116 117 118 119
        this.item = editor.getValue()
        this.scriptBoxDialog = true
      },
      getSriptBoxValue (val) {
        editor.setValue(val)
        // this.scriptBoxDialog = false
      },
      closeAble () {
120
        // this.scriptBoxDialog = false
121
      },
L
ligang 已提交
122 123
      /**
       * return resourceList
Z
zhukai 已提交
124
       *
L
ligang 已提交
125 126 127 128
       */
      _onResourcesData (a) {
        this.resourceList = a
      },
Z
zhukai 已提交
129 130 131 132 133 134
      /**
       * cache resourceList
       */
      _onCacheResourcesData (a) {
        this.cacheResourceList = a
      },
L
ligang 已提交
135 136 137 138 139 140
      /**
       * verification
       */
      _verification () {
        // rawScript verification
        if (!editor.getValue()) {
G
i18n  
gongzijian 已提交
141
          this.$message.warning(`${i18n.$t('Please enter script(required)')}`)
L
ligang 已提交
142 143 144 145 146 147 148
          return false
        }

        // localParams Subcomponent verification
        if (!this.$refs.refLocalParams._verifProp()) {
          return false
        }
B
break60 已提交
149
        // noRes
150
        if (this.noRes.length > 0) {
B
break60 已提交
151 152 153
          this.$message.warning(`${i18n.$t('Please delete all non-existent resources')}`)
          return false
        }
B
break60 已提交
154
        // Process resourcelist
155
        let dataProcessing = _.map(this.resourceList, v => {
B
break60 已提交
156 157 158 159
          return {
            id: v
          }
        })
L
ligang 已提交
160 161
        // storage
        this.$emit('on-params', {
B
break60 已提交
162
          resourceList: dataProcessing,
L
ligang 已提交
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
          localParams: this.localParams,
          rawScript: editor.getValue()
        })
        return true
      },
      /**
       * Processing code highlighting
       */
      _handlerEditor () {
        // editor
        editor = codemirror('code-shell-mirror', {
          mode: 'shell',
          readOnly: this.isDetails
        })

        this.keypress = () => {
          if (!editor.getOption('readOnly')) {
            editor.showHint({
              completeSingle: false
            })
          }
        }

        // Monitor keyboard
        editor.on('keypress', this.keypress)
        editor.setValue(this.rawScript)

        return editor
191
      },
192
      diGuiTree (item) { // Recursive convenience tree structure
B
break60 已提交
193
        item.forEach(item => {
194 195
          item.children === '' || item.children === undefined || item.children === null || item.children.length === 0
            ? this.operationTree(item) : this.diGuiTree(item.children)
B
break60 已提交
196
        })
B
break60 已提交
197
      },
198 199 200
      operationTree (item) {
        if (item.dirctory) {
          item.isDisabled = true
B
break60 已提交
201 202 203
        }
        delete item.children
      },
204
      searchTree (element, id) {
B
break60 已提交
205
        // 根据id查找节点
206
        if (element.id === id) {
207
          return element
208
        } else if (element.children !== null) {
209 210
          let i
          let result = null
211
          for (i = 0; result === null && i < element.children.length; i++) {
212
            result = this.searchTree(element.children[i], id)
B
break60 已提交
213
          }
214
          return result
B
break60 已提交
215
        }
216
        return null
B
break60 已提交
217
      },
218
      dataProcess (backResource) {
B
break60 已提交
219 220
        let isResourceId = []
        let resourceIdArr = []
221 222 223 224 225
        if (this.resourceList.length > 0) {
          this.resourceList.forEach(v => {
            this.options.forEach(v1 => {
              if (this.searchTree(v1, v)) {
                isResourceId.push(this.searchTree(v1, v))
B
break60 已提交
226 227 228
              }
            })
          })
229
          resourceIdArr = isResourceId.map(item => {
B
break60 已提交
230 231
            return item.id
          })
232 233 234 235
          Array.prototype.diff = function (a) {
            return this.filter(function (i) { return a.indexOf(i) < 0 })
          }
          let diffSet = this.resourceList.diff(resourceIdArr)
B
break60 已提交
236
          let optionsCmp = []
237 238 239
          if (diffSet.length > 0) {
            diffSet.forEach(item => {
              backResource.forEach(item1 => {
240
                if (item === item1.id || item === item1.res) {
B
break60 已提交
241 242 243 244 245 246 247
                  optionsCmp.push(item1)
                }
              })
            })
          }
          let noResources = [{
            id: -1,
248
            name: $t('Unauthorized or deleted resources'),
249
            fullName: '/' + $t('Unauthorized or deleted resources'),
B
break60 已提交
250 251
            children: []
          }]
252
          if (optionsCmp.length > 0) {
B
break60 已提交
253
            this.allNoResources = optionsCmp
254 255
            optionsCmp = optionsCmp.map(item => {
              return { id: item.id, name: item.name, fullName: item.res }
B
break60 已提交
256
            })
257
            optionsCmp.forEach(item => {
B
break60 已提交
258 259 260 261 262 263
              item.isNew = true
            })
            noResources[0].children = optionsCmp
            this.options = this.options.concat(noResources)
          }
        }
L
ligang 已提交
264 265
      }
    },
Z
zhukai 已提交
266
    watch: {
267
      // Watch the cacheParams
Z
zhukai 已提交
268
      cacheParams (val) {
269
        this.$emit('on-cache-params', val)
270 271 272 273 274 275 276 277 278 279 280 281
      },
      resourceIdArr (arr) {
        let result = []
        arr.forEach(item => {
          this.allNoResources.forEach(item1 => {
            if (item.id === item1.id) {
              // resultBool = true
              result.push(item1)
            }
          })
        })
        this.noRes = result
Z
zhukai 已提交
282 283 284
      }
    },
    computed: {
285
      resourceIdArr () {
B
break60 已提交
286 287
        let isResourceId = []
        let resourceIdArr = []
288 289 290 291 292
        if (this.resourceList.length > 0) {
          this.resourceList.forEach(v => {
            this.options.forEach(v1 => {
              if (this.searchTree(v1, v)) {
                isResourceId.push(this.searchTree(v1, v))
B
break60 已提交
293 294 295
              }
            })
          })
296 297
          resourceIdArr = isResourceId.map(item => {
            return { id: item.id, name: item.name, res: item.fullName }
B
break60 已提交
298 299
          })
        }
300 301 302
        return resourceIdArr
      },
      cacheParams () {
Z
zhukai 已提交
303
        return {
304
          resourceList: this.resourceIdArr,
305
          localParams: this.localParams
Z
zhukai 已提交
306 307 308
        }
      }
    },
L
ligang 已提交
309
    created () {
B
break60 已提交
310 311 312
      let item = this.store.state.dag.resourcesListS
      this.diGuiTree(item)
      this.options = item
L
ligang 已提交
313
      let o = this.backfillItem
314

L
ligang 已提交
315 316
      // Non-null objects represent backfill
      if (!_.isEmpty(o)) {
Z
zhukai 已提交
317
        this.rawScript = o.params.rawScript || ''
L
ligang 已提交
318 319

        // backfill resourceList
B
break60 已提交
320
        let backResource = o.params.resourceList || []
L
ligang 已提交
321 322
        let resourceList = o.params.resourceList || []
        if (resourceList.length) {
323 324 325
          _.map(resourceList, v => {
            if (!v.id) {
              this.store.dispatch('dag/getResourceId', {
326
                type: 'FILE',
327
                fullName: '/' + v.res
328 329
              }).then(res => {
                this.resourceList.push(res.id)
B
break60 已提交
330
                this.dataProcess(backResource)
331
              }).catch(e => {
B
break60 已提交
332 333
                this.resourceList.push(v.res)
                this.dataProcess(backResource)
334 335 336
              })
            } else {
              this.resourceList.push(v.id)
B
break60 已提交
337
              this.dataProcess(backResource)
338
            }
B
break60 已提交
339
          })
Z
zhukai 已提交
340
          this.cacheResourceList = resourceList
L
ligang 已提交
341
        }
342

L
ligang 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
        // backfill localParams
        let localParams = o.params.localParams || []
        if (localParams.length) {
          this.localParams = localParams
        }
      }
    },
    mounted () {
      setTimeout(() => {
        this._handlerEditor()
      }, 200)
    },
    destroyed () {
      if (editor) {
        editor.toTextArea() // Uninstall
        editor.off($('.code-shell-mirror'), 'keypress', this.keypress)
      }
    },
361
    components: { mLocalParams, mListBox, mScriptBox, Treeselect }
L
ligang 已提交
362 363
  }
</script>