shell.vue 7.3 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="ans-icon-max" @click="setEditorVal"></em>
30
          </a>
L
ligang 已提交
31 32 33 34
        </div>
      </div>
    </m-list-box>
    <m-list-box>
G
i18n  
gongzijian 已提交
35
      <div slot="text">{{$t('Resources')}}</div>
L
ligang 已提交
36 37 38 39
      <div slot="content">
        <m-resources
                ref="refResources"
                @on-resourcesData="_onResourcesData"
Z
zhukai 已提交
40
                @on-cache-resourcesData="_onCacheResourcesData"
L
ligang 已提交
41 42 43 44 45
                :resource-list="resourceList">
        </m-resources>
      </div>
    </m-list-box>
    <m-list-box>
G
i18n  
gongzijian 已提交
46
      <div slot="text">{{$t('Custom Parameters')}}</div>
L
ligang 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      <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'
62
  import mScriptBox from './_source/scriptBox'
L
ligang 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  import mResources from './_source/resources'
  import mLocalParams from './_source/localParams'
  import disabledState from '@/module/mixin/disabledState'
  import codemirror from '@/conf/home/pages/resource/pages/file/pages/_source/codemirror'

  let editor

  export default {
    name: 'shell',
    data () {
      return {
        // script
        rawScript: '',
        // Custom parameter
        localParams: [],
        // resource(list)
Z
zhukai 已提交
79 80 81
        resourceList: [],
        // Cache ResourceList
        cacheResourceList: []
L
ligang 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94
      }
    },
    mixins: [disabledState],
    props: {
      backfillItem: Object
    },
    methods: {
      /**
       * return localParams
       */
      _onLocalParams (a) {
        this.localParams = a
      },
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      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 已提交
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
          return false
        }

        if (!this.$refs.refResources._verifResources()) {
          return false
        }

        // localParams Subcomponent verification
        if (!this.$refs.refLocalParams._verifProp()) {
          return false
        }
        // storage
        this.$emit('on-params', {
          resourceList: this.resourceList,
          localParams: this.localParams,
          rawScript: editor.getValue()
        })
        return true
      },
      /**
       * Processing code highlighting
       */
      _handlerEditor () {
166 167
        this._destroyEditor()

L
ligang 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181
        // editor
        editor = codemirror('code-shell-mirror', {
          mode: 'shell',
          readOnly: this.isDetails
        })

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

182 183 184 185
        this.changes = () => {
          this._cacheParams()
        }

L
ligang 已提交
186 187
        // Monitor keyboard
        editor.on('keypress', this.keypress)
188 189 190

        editor.on('changes', this.changes)

L
ligang 已提交
191 192 193
        editor.setValue(this.rawScript)

        return editor
194 195 196 197 198 199 200 201 202 203 204 205 206 207
      },
      _cacheParams () {
        this.$emit('on-cache-params', {
          resourceList: this.cacheResourceList,
          localParams: this.localParams,
          rawScript: editor ? editor.getValue() : ''
        });
      },
      _destroyEditor () {
         if (editor) {
          editor.toTextArea() // Uninstall
          editor.off($('.code-sql-mirror'), 'keypress', this.keypress)
          editor.off($('.code-sql-mirror'), 'changes', this.changes)
        }
L
ligang 已提交
208 209
      }
    },
Z
zhukai 已提交
210 211 212
    watch: {
      //Watch the cacheParams
      cacheParams (val) {
213
        this._cacheParams()
Z
zhukai 已提交
214 215 216 217 218 219
      }
    },
    computed: {
      cacheParams () {
        return {
          resourceList: this.cacheResourceList,
220
          localParams: this.localParams
Z
zhukai 已提交
221 222 223
        }
      }
    },
L
ligang 已提交
224 225 226 227 228
    created () {
      let o = this.backfillItem

      // Non-null objects represent backfill
      if (!_.isEmpty(o)) {
Z
zhukai 已提交
229
        this.rawScript = o.params.rawScript || ''
L
ligang 已提交
230 231 232 233 234

        // backfill resourceList
        let resourceList = o.params.resourceList || []
        if (resourceList.length) {
          this.resourceList = resourceList
Z
zhukai 已提交
235
          this.cacheResourceList = resourceList
L
ligang 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
        }

        // 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)
254
        editor.off($('.code-shell-mirror'), 'changes', this.changes)
L
ligang 已提交
255 256
      }
    },
257
    components: { mLocalParams, mListBox, mResources, mScriptBox }
L
ligang 已提交
258 259
  }
</script>
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
<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;
  }
Z
zhukai 已提交
279

280
</style>