m-bean-operation-invocation.vue 5.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<!--
  - Copyright 2014-2018 the original author or authors.
  -
  - Licensed 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.
  -->

<template>
18 19 20 21 22 23 24
  <div class="modal is-active">
    <div class="modal-background" @click="abort"/>
    <div class="modal-content">
      <div class="modal-card">
        <header class="modal-card-head">
          <p class="modal-card-title" v-text="name"/>
        </header>
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        <template v-if="state === 'input-args'">
          <section class="modal-card-body" @keyup.ctrl.enter="invoke(args)">
            <div class="field" v-for="(arg, idx) in descriptor.args" :key="arg.name">
              <label class="label">
                <span v-text="arg.name"/>
                <small class="is-muted has-text-weight-normal" v-text="arg.type"/>
              </label>
              <div class="control">
                <input type="text" class="input" v-model="args[idx]">
              </div>
              <p class="help" v-text="arg.desc"/>
            </div>
          </section>
          <footer class="modal-card-foot">
            <div class="field is-grouped is-grouped-right">
              <div class="control">
                <button class="button is-primary" @click="invoke(args)">Execute</button>
              </div>
            </div>
          </footer>
        </template>
47

48 49 50 51 52 53 54
        <template v-else-if="state === 'executing'">
          <section class="modal-card-body">
            <section class="section is-loading">
              <p>Executing...</p>
            </section>
          </section>
        </template>
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        <template v-else-if="state === 'completed'">
          <section class="modal-card-body">
            <div class="message is-success">
              <div class="message-body">
                <strong>Execution successful.</strong>
              </div>
            </div>
            <pre v-text="result"/>
          </section>
          <footer class="modal-card-foot">
            <div class="field is-grouped is-grouped-right">
              <div class="control">
                <button class="button is-light" @click="abort"> Close</button>
              </div>
            </div>
          </footer>
        </template>
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        <template v-else-if="state === 'failed'">
          <section class="modal-card-body">
            <div class="message is-danger">
              <div class="message-body">
                <strong>
                  <font-awesome-icon class="has-text-danger"
                                     icon="exclamation-triangle"/>
                  Execution failed.
                </strong>
                <p v-text="error.message"/>
              </div>
            </div>
            <pre v-if="error.stacktrace"
                 v-text="error.stacktrace"/>
            <pre v-if="error.response && error.response.data"
                 v-text="error.response.data"/>
          </section>
          <footer class="modal-card-foot">
            <div class="field is-grouped is-grouped-right">
              <div class="control">
                <button class="button is-light" @click="abort"> Close</button>
              </div>
96
            </div>
97 98 99
          </footer>
        </template>
      </div>
100
    </div>
101
  </div>
102 103 104 105 106
</template>

<script>

  export default {
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    props: {
      name: {
        type: String,
        required: true
      },
      descriptor: {
        type: Object,
        required: true
      },
      value: {
        type: null,
        default: null
      },
      onClose: {
        type: Function,
        required: true
      },
      onExecute: {
        type: Function,
        required: true
      }
    },
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    data: () => ({
      state: null,
      error: null,
      args: null,
      result: null
    }),
    computed: {},
    methods: {
      abort() {
        this.onClose();
      },
      invoke(args) {
        this.state = (args || this.descriptor.args.length === 0) ? 'prepared' : 'input-args';
        this.args = args || new Array(this.descriptor.args.length);
        this.error = null;
        this.result = null;

        if (this.state === 'prepared') {
          this.execute()
        }
      },
      async execute() {
        this.state = 'executing';
        try {
          const result = await this.onExecute(this.args);
          if (result.data.status < 400) {
            this.result = JSON.stringify(result.data.value, null, 4);
            this.state = 'completed';
          } else {
            const error = new Error(`Execution failed: ${result.data.error}`);
            error.stacktrace = result.data.stacktrace;
            this.state = 'failed';
            this.error = error;
            console.warn('Invocation failed', error);
          }
        } catch (error) {
          this.state = 'failed';
          this.error = error;
          console.warn('Invocation failed', error);
        }
      },
      keyHandler(event) {
        if (event.keyCode === 27) {
          this.abort()
        }
      }
    },
    created() {
      this.invoke();
    },
    mounted() {
      document.addEventListener('keyup', this.keyHandler)
    },
    beforeDestroy() {
      document.removeEventListener('keyup', this.keyHandler)
    },
  }
</script>