shell.vue 11.1 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-rank" @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 55 56 57 58
    <el-dialog
      :visible.sync="scriptBoxDialog"
      modal-append-to-body="true"
      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 68
  import mResources from './_source/resources'
  import mLocalParams from './_source/localParams'
  import disabledState from '@/module/mixin/disabledState'
B
break60 已提交
69 70
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
L
ligang 已提交
71 72 73 74 75 76 77 78
  import codemirror from '@/conf/home/pages/resource/pages/file/pages/_source/codemirror'

  let editor

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

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

        // backfill resourceList
B
break60 已提交
316
        let backResource = o.params.resourceList || []
L
ligang 已提交
317 318
        let resourceList = o.params.resourceList || []
        if (resourceList.length) {
319
           _.map(resourceList, v => {
B
break60 已提交
320
            if(!v.id) {
321 322 323 324 325
              this.store.dispatch('dag/getResourceId',{
                type: 'FILE',
                fullName: '/'+v.res
              }).then(res => {
                this.resourceList.push(res.id)
B
break60 已提交
326
                this.dataProcess(backResource)
327
              }).catch(e => {
B
break60 已提交
328 329
                this.resourceList.push(v.res)
                this.dataProcess(backResource)
330 331 332
              })
            } else {
              this.resourceList.push(v.id)
B
break60 已提交
333
              this.dataProcess(backResource)
334
            }
B
break60 已提交
335
          })
Z
zhukai 已提交
336
          this.cacheResourceList = resourceList
L
ligang 已提交
337
        }
B
break60 已提交
338
        
L
ligang 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
        // 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)
      }
    },
B
break60 已提交
357
    components: { mLocalParams, mListBox, mResources, mScriptBox, Treeselect }
L
ligang 已提交
358 359
  }
</script>
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
<style lang="scss" rel="stylesheet/scss" scope>
  .scriptModal {
    .ans-modal-box-content-wrapper {
      width: 90%;
      .ans-modal-box-close {
        right: -12px;
        top: -16px;
        color: #fff;
      }
    }
  }
  .ans-modal-box-close {
    z-index: 100;
  }
  .ans-modal-box-max {
    position: absolute;
    right: -12px;
    top: -16px;
  }
379 380 381 382 383 384 385 386
  .vue-treeselect--disabled {
    .vue-treeselect__control {
      background-color: #ecf3f8;
      .vue-treeselect__single-value {
        color: #6d859e;
      }
    }
  }
387
</style>