python.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 24
      <div slot="content">
        <div class="from-mirror">
          <textarea id="code-python-mirror" name="code-python-mirror" style="opacity: 0;">
          </textarea>
25 26 27
          <a class="ans-modal-box-max">
            <em class="ans-icon-max" @click="setEditorVal"></em>
          </a>
L
ligang 已提交
28 29 30 31
        </div>
      </div>
    </m-list-box>
    <m-list-box>
G
i18n  
gongzijian 已提交
32
      <div slot="text">{{$t('Resources')}}</div>
L
ligang 已提交
33
      <div slot="content">
34
        <treeselect v-model="resourceList" :multiple="true" maxHeight="200" :options="resourceOptions" :normalizer="normalizer" :value-consists-of="valueConsistsOf" :disabled="isDetails" :placeholder="$t('Please select resources')">
B
break60 已提交
35 36 37 38 39 40 41 42
          <div slot="value-label" slot-scope="{ node }">{{ node.raw.fullName }}</div>
        </treeselect>
        <!-- <m-resources
            ref="refResources"
            @on-resourcesData="_onResourcesData"
            @on-cache-resourcesData="_onCacheResourcesData"
            :resource-list="resourceList">
        </m-resources> -->
L
ligang 已提交
43 44 45 46
      </div>
    </m-list-box>

    <m-list-box>
G
i18n  
gongzijian 已提交
47
      <div slot="text">{{$t('Custom Parameters')}}</div>
L
ligang 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
      <div slot="content">
        <m-local-params
                ref="refLocalParams"
                @on-local-params="_onLocalParams"
                :udp-list="localParams"
                :hide="false">
        </m-local-params>
      </div>
    </m-list-box>
  </div>
</template>
<script>
  import _ from 'lodash'
  import i18n from '@/module/i18n'
  import mListBox from './_source/listBox'
63
  import mScriptBox from './_source/scriptBox'
L
ligang 已提交
64 65
  import mResources from './_source/resources'
  import mLocalParams from './_source/localParams'
B
break60 已提交
66 67
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
L
ligang 已提交
68 69 70 71 72 73 74 75 76
  import disabledState from '@/module/mixin/disabledState'
  import codemirror from '@/conf/home/pages/resource/pages/file/pages/_source/codemirror'

  let editor

  export default {
    name: 'python',
    data () {
      return {
B
break60 已提交
77
        valueConsistsOf: 'LEAF_PRIORITY',
L
ligang 已提交
78 79 80 81 82
        // script
        rawScript: '',
        // Custom parameter
        localParams: [],
        // resource(list)
Z
zhukai 已提交
83 84
        resourceList: [],
        // Cache ResourceList
B
break60 已提交
85 86
        cacheResourceList: [],
        resourceOptions: [],
87 88 89 90 91 92 93
        normalizer(node) {
          return {
            label: node.name
          }
        },
        allNoResources: [],
        noRes: []
L
ligang 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106
      }
    },
    mixins: [disabledState],
    props: {
      backfillItem: Object
    },
    methods: {
      /**
       * return localParams
       */
      _onLocalParams (a) {
        this.localParams = a
      },
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
      setEditorVal() {
        let self = this
          let modal = self.$modal.dialog({
            className: 'scriptModal',
            closable: false,
            showMask: true,
            maskClosable: true,
            onClose: function() {

            },
            render (h) {
              return h(mScriptBox, {
                on: {
                  getSriptBoxValue (val) {
                    editor.setValue(val)
                  },
                  closeAble () {
                    // this.$modal.destroy()
                    modal.remove()
                  }
                },
                props: {
                  item: editor.getValue()
                }
              })
            }
          })
      },
L
ligang 已提交
135 136 137
      /**
       * return resourceList
       */
138 139 140
      // _onResourcesData (a) {
      //   this.resourceList = a
      // },
Z
zhukai 已提交
141 142 143 144 145 146
      /**
       * cache resourceList
       */
      _onCacheResourcesData (a) {
        this.cacheResourceList = a
      },
L
ligang 已提交
147 148 149 150 151 152
      /**
       * verification
       */
      _verification () {
        // rawScript 验证
        if (!editor.getValue()) {
G
i18n  
gongzijian 已提交
153
          this.$message.warning(`${i18n.$t('Please enter script(required)')}`)
L
ligang 已提交
154 155 156 157 158 159 160 161
          return false
        }

        // localParams Subcomponent verification
        if (!this.$refs.refLocalParams._verifProp()) {
          return false
        }

162 163 164 165 166 167
        // noRes
        if (this.noRes.length>0) {
          this.$message.warning(`${i18n.$t('Please delete all non-existent resources')}`)
          return false
        }

L
ligang 已提交
168 169
        // storage
        this.$emit('on-params', {
B
break60 已提交
170 171 172
          resourceList: _.map(this.resourceList, v => {
            return {id: v}
          }),
L
ligang 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
          localParams: this.localParams,
          rawScript: editor.getValue()
        })
        return true
      },
      /**
       * Processing code highlighting
       */
      _handlerEditor () {
        // editor
        editor = codemirror('code-python-mirror', {
          mode: 'python',
          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
B
break60 已提交
202 203 204 205 206 207 208 209 210 211 212 213
      },
      diGuiTree(item) {  // Recursive convenience tree structure
        item.forEach(item => {
          item.children === '' || item.children === undefined || item.children === null || item.children.length === 0?        
            this.operationTree(item) : this.diGuiTree(item.children);
        })
      },
      operationTree(item) {
        if(item.dirctory) {
          item.isDisabled =true
        }
        delete item.children
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 249 250 251 252 253 254 255 256 257 258
      },
      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.resourceOptions.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,
B
break60 已提交
259 260
            name: $t('Unauthorized or deleted resources'),
            fullName: '/'+$t('Unauthorized or deleted resources'),
261 262 263 264 265 266 267 268 269 270 271 272 273 274
            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.resourceOptions = this.resourceOptions.concat(noResources)
          }
        }
L
ligang 已提交
275 276
      }
    },
Z
zhukai 已提交
277 278 279
    watch: {
      //Watch the cacheParams
      cacheParams (val) {
B
break60 已提交
280
        this.$emit('on-cache-params', val);
Z
zhukai 已提交
281 282 283 284
      }
    },
    computed: {
      cacheParams () {
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
        let isResourceId = []
        let resourceIdArr = []
        if(this.resourceList.length>0) {
          this.resourceList.forEach(v=>{
            this.resourceOptions.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 已提交
309
        return {
310
          resourceList: resourceIdArr,
311
          localParams: this.localParams
Z
zhukai 已提交
312 313 314
        }
      }
    },
L
ligang 已提交
315
    created () {
B
break60 已提交
316 317
      let item = this.store.state.dag.resourcesListS
      this.diGuiTree(item)
318
      this.resourceOptions = item
L
ligang 已提交
319 320 321 322
      let o = this.backfillItem

      // Non-null objects represent backfill
      if (!_.isEmpty(o)) {
Z
zhukai 已提交
323
        this.rawScript = o.params.rawScript || ''
L
ligang 已提交
324 325

        // backfill resourceList
326
        let backResource = o.params.resourceList || []
L
ligang 已提交
327 328
        let resourceList = o.params.resourceList || []
        if (resourceList.length) {
329
          _.map(resourceList, v => {
330
            if(!v.id) {
331 332 333 334 335
              this.store.dispatch('dag/getResourceId',{
                type: 'FILE',
                fullName: '/'+v.res
              }).then(res => {
                this.resourceList.push(res.id)
336
                this.dataProcess(backResource)
337
              }).catch(e => {
338 339
                this.resourceList.push(v.res)
                this.dataProcess(backResource)
340 341 342
              })
            } else {
              this.resourceList.push(v.id)
B
break60 已提交
343
              this.dataProcess(backResource)
344
            }
B
break60 已提交
345
          })
Z
zhukai 已提交
346
          this.cacheResourceList = resourceList
L
ligang 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
        }

        // backfill localParams
        let localParams = o.params.localParams || []
        if (localParams.length) {
          this.localParams = localParams
        }
      }
    },
    mounted () {
      setTimeout(() => {
        this._handlerEditor()
      }, 200)
    },
    destroyed () {
B
break60 已提交
362 363
      editor.toTextArea() // Uninstall
      editor.off($('.code-python-mirror'), 'keypress', this.keypress)
L
ligang 已提交
364
    },
365
    components: { mLocalParams, mListBox, mResources,Treeselect }
L
ligang 已提交
366
  }
Z
zhukai 已提交
367
</script>
368 369 370 371 372 373 374 375 376
<style lang="scss" rel="stylesheet/scss" scope>
  .vue-treeselect--disabled {
    .vue-treeselect__control {
      background-color: #ecf3f8;
      .vue-treeselect__single-value {
        color: #6d859e;
      }
    }
  }
377 378 379 380 381
  .ans-modal-box-max {
    position: absolute;
    right: -12px;
    top: -16px;
  }
382
</style>