#
Junjie
9 天以前 dc3f9cc91759823ce59486f19b138be4b296a0f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
window.MapCanvasShared = window.MapCanvasShared || {};
 
(function (shared) {
  shared.buildReplayTarget = function (type, deviceNo, stationId, title, detail) {
    return {
      type: type || '',
      deviceNo: deviceNo == null ? null : Number(deviceNo),
      stationId: stationId == null ? null : Number(stationId),
      title: title || '',
      detail: detail || ''
    };
  };
 
  shared.isTaskNoInRange = function (taskNo, range) {
    if (!range) {
      return false;
    }
    var start = parseInt(range.start, 10);
    var end = parseInt(range.end, 10);
    if (isNaN(start) || isNaN(end)) {
      return false;
    }
    return taskNo >= start && taskNo <= end;
  };
 
  shared.getStationTaskClass = function (taskNo, stationTaskRange) {
    if (!(taskNo > 0)) {
      return null;
    }
    var range = stationTaskRange || {};
    if (shared.isTaskNoInRange(taskNo, range.inbound)) {
      return 'machine-pakin';
    }
    if (shared.isTaskNoInRange(taskNo, range.outbound)) {
      return 'machine-pakout';
    }
    return null;
  };
 
  shared.registerMapRuntimeDefinition = function (definition) {
    shared._mapRuntimeDefinition = definition || null;
    return shared._mapRuntimeDefinition;
  };
 
  shared.getMapRuntimeDefinition = function () {
    return shared._mapRuntimeDefinition || null;
  };
 
  shared.createMapRuntimeContext = function (options) {
    var definition = shared.getMapRuntimeDefinition();
    if (!definition) {
      throw new Error('Map runtime definition is not registered.');
    }
 
    var runtimeOptions = options || {};
    var host = runtimeOptions.host || {};
    var data = typeof definition.data === 'function' ? definition.data.call(host) : {};
    var props = runtimeOptions.props || {};
    var refs = runtimeOptions.refs || {};
 
    Object.keys(data || {}).forEach(function (key) {
      if (typeof host[key] === 'undefined') {
        host[key] = data[key];
      }
    });
 
    Object.keys(props).forEach(function (key) {
      host[key] = props[key];
    });
 
    host.$refs = host.$refs || {};
    Object.keys(refs).forEach(function (key) {
      host.$refs[key] = refs[key];
    });
 
    if (runtimeOptions.el) {
      host.$el = runtimeOptions.el;
    }
    if (typeof runtimeOptions.emit === 'function') {
      host.$emit = runtimeOptions.emit;
    }
    if (typeof host.$emit !== 'function') {
      host.$emit = function () {};
    }
 
    host.__mapRuntimeDefinition = definition;
    host.__mapRuntimeOptions = runtimeOptions;
    host.ownsPixiApp = runtimeOptions.ownsPixiApp !== false;
    host.pixiStageHost = runtimeOptions.stageHost || null;
    host.pixiStageTransformTarget = runtimeOptions.transformTarget || null;
 
    Object.keys(definition.methods || {}).forEach(function (name) {
      host[name] = definition.methods[name].bind(host);
    });
 
    return host;
  };
 
  shared.mountMapRuntime = function (runtime, options) {
    if (!runtime) {
      throw new Error('Map runtime is required.');
    }
    var runtimeOptions = options || {};
    var definition = runtime.__mapRuntimeDefinition || shared.getMapRuntimeDefinition();
    if (!definition) {
      throw new Error('Map runtime definition is not registered.');
    }
 
    if (runtimeOptions.app) {
      runtime.pixiApp = runtimeOptions.app;
    }
    if (runtimeOptions.el) {
      runtime.$el = runtimeOptions.el;
    }
    runtime.$refs = runtime.$refs || {};
    Object.keys(runtimeOptions.refs || {}).forEach(function (key) {
      runtime.$refs[key] = runtimeOptions.refs[key];
    });
    if (typeof runtimeOptions.emit === 'function') {
      runtime.$emit = runtimeOptions.emit;
    }
    if (runtimeOptions.props) {
      Object.keys(runtimeOptions.props).forEach(function (key) {
        runtime[key] = runtimeOptions.props[key];
      });
    }
    runtime.ownsPixiApp = runtimeOptions.ownsPixiApp !== false;
    runtime.pixiStageHost = runtimeOptions.stageHost || runtime.pixiStageHost || null;
    runtime.pixiStageTransformTarget = runtimeOptions.transformTarget || runtime.pixiStageTransformTarget || null;
 
    if (typeof definition.mounted === 'function') {
      definition.mounted.call(runtime);
    }
    return runtime;
  };
 
  shared.destroyMapRuntime = function (runtime) {
    if (!runtime) {
      return;
    }
    var definition = runtime.__mapRuntimeDefinition || shared.getMapRuntimeDefinition();
    if (definition && typeof definition.beforeDestroy === 'function') {
      definition.beforeDestroy.call(runtime);
    }
  };
})(window.MapCanvasShared);