App.vue 9.01 KB
Newer Older
Nanyang Fu's avatar
init  
Nanyang Fu committed
1 2
<template>
  <div id="app">
WuFeiyang's avatar
WuFeiyang committed
3 4 5
    <b-navbar class="Widget">
      <b-dropdown id="dropdown-1" text="Add Widget" class="m-md-2" variant="primary">
        <b-dropdown-item v-for="(availableWidget,index) in availableWidgets" :key="index">
WuFeiyang's avatar
WuFeiyang committed
6
          <div v-on:click="addWidget(availableWidget)">{{availableWidget}}</div>
WuFeiyang's avatar
WuFeiyang committed
7 8 9
        </b-dropdown-item>
      </b-dropdown>
      <b-button @click="saveWidgetList" style="margin-left:2%" variant="primary">Save</b-button>
WuFeiyang's avatar
WuFeiyang committed
10 11 12 13 14 15 16 17
      <b-form-file
        id="file"
        type="file"
        @change="loadTextFromFile"
        placeholder="Choose a widgetTemplate file to laod"
        accept=".json"
        style="width:30%;margin-left:2%"
      />
WuFeiyang's avatar
WuFeiyang committed
18 19 20
    </b-navbar>

    <grid-layout
WuFeiyang's avatar
WuFeiyang committed
21 22 23 24 25 26 27 28 29
      :layout.sync="widgetList"
      :col-num="12"
      :row-height="30"
      :is-draggable="true"
      :is-resizable="true"
      :is-mirrored="false"
      :vertical-compact="true"
      :margin="[10, 10]"
      :use-css-transforms="true"
WuFeiyang's avatar
WuFeiyang committed
30
    >
WuFeiyang's avatar
WuFeiyang committed
31 32 33 34 35 36 37 38
      <grid-item
        v-for="(widget) in widgetList"
        :x="widget.x"
        :y="widget.y"
        :w="widget.w"
        :h="widget.h"
        :i="widget.i"
        :key="widget.i"
WuFeiyang's avatar
WuFeiyang committed
39 40
        drag-allow-from=".vue-draggable-handle"
        drag-ignore-from=".no-drag"
WuFeiyang's avatar
WuFeiyang committed
41 42
      >
        <div style="border-color: rgb(206, 212, 218);">
WuFeiyang's avatar
WuFeiyang committed
43 44
          <div class="vue-draggable-handle" style="height:20px;background-color:rgb(0, 123, 255)"></div>
          <component  class="no-drag" :is="widget.widgetComponentName" :ref="widget.ref"></component>
WuFeiyang's avatar
WuFeiyang committed
45 46
        </div>
      </grid-item>
WuFeiyang's avatar
WuFeiyang committed
47
    </grid-layout>
Nanyang Fu's avatar
init  
Nanyang Fu committed
48 49 50
  </div>
</template>

WuFeiyang's avatar
WuFeiyang committed
51 52
     

Nanyang Fu's avatar
init  
Nanyang Fu committed
53
<script lang="ts">
WuFeiyang's avatar
WuFeiyang committed
54 55
import { Component, Vue } from "vue-property-decorator";
import axios from "axios";
WuFeiyang's avatar
WuFeiyang committed
56
import {  GridItemData,GridLayout,GridItem} from 'vue-grid-layout';
WuFeiyang's avatar
WuFeiyang committed
57 58 59 60 61

import { WidgetRef } from "./models/WidgetRef";
import { WidgetConfig, AllWidgetConfig } from "./models/WidgetConfig";
import { Action, UpdatePayload } from "./models/UpdatePayload";
import { Widget } from "./models/wiget";
WuFeiyang's avatar
WuFeiyang committed
62
import { ResourceInfo } from "./models/Customview";
Nanyang Fu's avatar
init  
Nanyang Fu committed
63

WuFeiyang's avatar
WuFeiyang committed
64 65 66 67 68
//when add more available widgets add ref to the widgets
import Status from "./components/Status/Status.vue";
import WaveView from "./components/WaveView/WaveView.vue";
import Method from "./components/Method/Method.vue";
import Config from "./components/Config/Config.vue";
WuFeiyang's avatar
WuFeiyang committed
69
import Thing from "./components/Thing/Thing.vue";
WuFeiyang's avatar
WuFeiyang committed
70 71

//this is the view selecotr class
Nanyang Fu's avatar
init  
Nanyang Fu committed
72 73
@Component({
  components: {
WuFeiyang's avatar
WuFeiyang committed
74 75 76 77
    //when add more available widgets add ref to the widgets
    Status,
    WaveView,
    Method,
WuFeiyang's avatar
WuFeiyang committed
78
    Config,
WuFeiyang's avatar
WuFeiyang committed
79
    Thing,
WuFeiyang's avatar
WuFeiyang committed
80 81
    GridLayout,
    GridItem
WuFeiyang's avatar
WuFeiyang committed
82
  }
Nanyang Fu's avatar
init  
Nanyang Fu committed
83
})
Nanyang Fu's avatar
t  
Nanyang Fu committed
84
export default class App extends Vue {
WuFeiyang's avatar
WuFeiyang committed
85 86 87 88 89 90 91
  widgetList: WidgetRef[] = [];
  fileName: string = "config.json";
  private lastWidgetIndex: number = 0;
  isShowAddWidget: Boolean = false;
  text: string = "";

  //when add more available widgets add its name here
WuFeiyang's avatar
WuFeiyang committed
92
  availableWidgets = ["Status", "Config", "WaveView", "Method","Thing"];
WuFeiyang's avatar
WuFeiyang committed
93 94 95 96 97

  toggleShowAddWidget(): void {
    this.isShowAddWidget = !this.isShowAddWidget;
  }

WuFeiyang's avatar
WuFeiyang committed
98 99 100 101 102 103 104 105
  pokeAndUpdateUI(ref:string, sample:ResourceInfo[], samplePath:string)
  {
    Vue.nextTick(() => {
      console.log(this.$refs[ref]);
      ((this.$refs[ref] as Array<Widget>)[0] as Widget).samplePoke(sample,samplePath);
      ((this.$refs[ref] as Array<Widget>)[0] as Widget).updateUI();
    })
  }
WuFeiyang's avatar
WuFeiyang committed
106

WuFeiyang's avatar
WuFeiyang committed
107
  mounted() {
WuFeiyang's avatar
WuFeiyang committed
108
  // var fragment = window.location.hash;
WuFeiyang's avatar
WuFeiyang committed
109
  var fragment = "#/dataserver/DataByTimeFuzzy";
WuFeiyang's avatar
WuFeiyang committed
110 111 112 113 114 115 116 117 118
  if (fragment != "#") {
    fragment = fragment.substring(1,fragment.length);
    var customViewURL = "customView/template" + fragment;
    axios.get(customViewURL).then(response => {
      if(response.data.CFET2CORE_SAMPLE_ISVALID == false || response.data.CFET2CORE_SAMPLE_VAL == null)
      {
        //直接访问对应的值
        var dataURL = fragment;
        axios.get(fragment).then(response => {
WuFeiyang's avatar
WuFeiyang committed
119 120 121 122 123 124 125 126 127 128 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
          var resourcetype = response.data.ResourceType;
          var samplePath = response.data.CFET2CORE_SAMPLE_PATH;
          var sample: ResourceInfo[] = [];
          //这里不知道要以什么类型接收json,所以写的比较死。后续displaytype加上后需要加上WaveView
          switch(resourcetype){
            case "Thing":
              {
                this.addWidget(resourcetype);
                break;
              }
            case "Status":
              {
                this.addWidget(resourcetype);
                var tempRef = (this.lastWidgetIndex - 1).toString();
                sample[0] = response.data.Actions.get as ResourceInfo;
                console.log(sample[0]);
                console.log(sample[0].Parameters);
                this.pokeAndUpdateUI(tempRef, sample,samplePath);
                break;
              }
            case "Method":
              {
                this.addWidget(resourcetype);
                var tempRef = (this.lastWidgetIndex - 1).toString();
                sample[0] = response.data.Actions.invoke as ResourceInfo;
                this.pokeAndUpdateUI(tempRef, sample,samplePath);
                break;
              }
            case "Config":
              {
                this.addWidget(resourcetype);
                var tempRef = (this.lastWidgetIndex - 1).toString();
                //这里传的sample为数组是考虑到config,默认set位于sample[1]
                sample[0] = response.data.Actions.get as ResourceInfo;
                sample[1] = response.data.Actions.set as ResourceInfo;
                this.pokeAndUpdateUI(tempRef, sample,samplePath);
                break;
              }
          }
WuFeiyang's avatar
WuFeiyang committed
158
        })
WuFeiyang's avatar
WuFeiyang committed
159 160 161 162 163
      }
      else{
        //返回有值的customview template,进行load处理
          var customviewTemplate:string;
          customviewTemplate = response.data.CFET2CORE_SAMPLE_VAL;
WuFeiyang's avatar
WuFeiyang committed
164
          var widgets = Object.assign(
WuFeiyang's avatar
WuFeiyang committed
165 166 167 168
          new AllWidgetConfig(),
          JSON.parse(customviewTemplate));
          this.widgetList = widgets.widgetList;
          this.lastWidgetIndex = Number(widgets.currentRef);
WuFeiyang's avatar
WuFeiyang committed
169 170 171
          this.$forceUpdate(); 
          this.importActiveWidgetList();
          //替换startpath
WuFeiyang's avatar
WuFeiyang committed
172 173 174 175
          Vue.nextTick(() => { 
          fragment = fragment.substring(1,fragment.length);
          for (var wid of this.widgetList) {
               ((this.$refs[wid.ref] as Array<Widget>)[0] as Widget).replaceStartPath( fragment as string);
WuFeiyang's avatar
WuFeiyang committed
176 177 178 179 180
           } 
          //刷新值
           for (var wid of this.widgetList) {
               ((this.$refs[wid.ref] as Array<Widget>)[0] as Widget).refresh();
           } 
WuFeiyang's avatar
WuFeiyang committed
181 182 183 184
      }); 

      }
    })    
WuFeiyang's avatar
WuFeiyang committed
185
    }
WuFeiyang's avatar
WuFeiyang committed
186 187 188 189 190 191 192 193 194 195 196 197 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 249
  }

  exportActiveWidgetList(): AllWidgetConfig {
    for (var widget of this.widgetList) {
      widget.widgetConfig = ((this.$refs[widget.ref] as Array<Widget>)[0] as Widget).getConfig();
    }
    var widgetConfigList = new AllWidgetConfig();
    widgetConfigList.widgetList = this.widgetList;
    widgetConfigList.currentRef = this.lastWidgetIndex.toString();
    console.log(widgetConfigList);
    return widgetConfigList;
  }

  importActiveWidgetList() {
    for (var wid of this.widgetList) {
       ((this.$refs[wid.ref] as Array<Widget>)[0] as Widget).setConfig( wid.widgetConfig as WidgetConfig);
    }
  }


  loadTextFromFile(ev: any) {
    const file = ev.target.files[0];
    const reader = new FileReader();
    reader.readAsText(file);
    var widgets;
    // console.log(this.$refs);
    reader.onload = e => {
      widgets = Object.assign(
        new AllWidgetConfig(),
        JSON.parse((e.target as any).result)
      );
      // console.log(widgets);
      this.widgetList = widgets.widgetList;
      this.lastWidgetIndex = Number(widgets.currentRef);
      this.$forceUpdate();
      Vue.nextTick(() => {                                         // changed here
        this.importActiveWidgetList();
      });  
      // console.log(this.$refs);
    };
    
}

  saveWidgetList(): void {
    var data = JSON.stringify(this.exportActiveWidgetList());
    console.log(data);

    const blob = new Blob([data]);
    if (window.navigator.msSaveOrOpenBlob) {
      // 兼容IE10
      navigator.msSaveBlob(blob, this.fileName);
    } else {
      //  chrome/firefox
      let aTag = document.createElement("a");
      aTag.download = this.fileName;
      aTag.href = URL.createObjectURL(blob);
      aTag.click();
      URL.revokeObjectURL(aTag.href);
    }
  }

  addWidget(widgetName: string): void {
    var newWidget = new WidgetRef();
    newWidget.widgetComponentName = widgetName;
WuFeiyang's avatar
WuFeiyang committed
250
    newWidget.ref = this.lastWidgetIndex.toString();
WuFeiyang's avatar
WuFeiyang committed
251 252
    newWidget.y=this.lastWidgetIndex*4;
    newWidget.i=Number(newWidget.ref);
WuFeiyang's avatar
WuFeiyang committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266
    this.lastWidgetIndex++;
    this.widgetList = [...this.widgetList, newWidget];
  }

  update(payload: UpdatePayload) {
    for (var wid of this.widgetList) {
      (this.$refs[wid.ref] as Widget).parentUpdate(payload);
    }
    if (payload.action == "") {
      for (var wid of this.widgetList) {
        (this.$refs[wid.ref] as Widget).refresh();
      }
    }
  }
Nanyang Fu's avatar
t  
Nanyang Fu committed
267
}
Nanyang Fu's avatar
init  
Nanyang Fu committed
268 269 270 271
</script>

<style>
#app {
WuFeiyang's avatar
WuFeiyang committed
272
  font-family: "Avenir", Helvetica, Arial, sans-serif;
Nanyang Fu's avatar
init  
Nanyang Fu committed
273 274 275 276
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
WuFeiyang's avatar
WuFeiyang committed
277
}
WuFeiyang's avatar
WuFeiyang committed
278
.Widget {
WuFeiyang's avatar
WuFeiyang committed
279 280
  width: 100%;
  border-color: rgb(206, 212, 218);
Nanyang Fu's avatar
init  
Nanyang Fu committed
281 282
}
</style>