nnnjjj123
2020-11-17 1b2c1edb61190eeb19f465ff031eaa3b2a1b8dbc
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
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
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
define([
    'dojo/_base/declare',
    'dojo/_base/lang',
    'dojo/_base/array',
    'dojo/_base/html',
    'dojo/on',
    'dojo/aspect',
    'dojo/dom-construct',
    'dojo/dom-geometry',
    'dojo/dom-class',
    'dijit/_WidgetBase',
    'dijit/_TemplatedMixin',
    'jimu/PanelManager',
    'jimu/WidgetManager'
  ],
  function(declare, lang, array, html, on, aspect,
    domConstruct, domGeometry, domClass, _WidgetBase, _TemplatedMixin, PanelManager,
    WidgetManager) {
    var clazz = declare([_WidgetBase, _TemplatedMixin], {
      baseClass: 'jimu-dnd-mobile-controller',
      templateString: '<div>' +
          '<div class="icon-section" data-dojo-attach-point="iconNode"></div>' +
          '<div class="container-section" data-dojo-attach-point="containerNode"></div>' +
        '</div>',
      appConfig: null,
      panelContainerNode: null,
      openIds: null,
      toolsCount: 0,
      panels: null,
      openPanelIds: null,
      widgetOnCloseHandlerIds: null,
      panelOnCloseHandlerIds: null,
 
      postCreate: function() {
        this.inherited(arguments);
        this.panelManager = PanelManager.getInstance();
        this.widgetManager = WidgetManager.getInstance();
        this.openIds = [];
        this.openPanelIds = [];
        this.widgetOnCloseHandlerIds = [];
        this.panelOnCloseHandlerIds = [];
        this.panels = {};
        this.createWidgetIcons();
        if (this.toolsCount === 0) {
          html.setStyle(this.domNode, 'display', 'none');
        }
        this.own(on(this.iconNode, 'click', lang.hitch(this, function(event) {
          event.stopPropagation();
          if (domClass.contains(this.containerNode, 'in')) {
            domClass.remove(this.containerNode, 'in');
            domClass.add(this.containerNode, 'out');
          } else {
            domClass.remove(this.containerNode, 'out');
            domClass.add(this.containerNode, 'in');
          }
        })));
        this.own(on(document.body, 'click', lang.hitch(this, function (event) {
          if (domClass.contains(this.containerNode, 'in')) {
            var target = event.target || event.srcElement;
            if (!this.isPartOfPopup(target)) {
              domClass.remove(this.containerNode, 'in');
              domClass.add(this.containerNode, 'out');
            }
          }
        })));
      },
 
      destroyOnScreenWidgets: function() {
        array.forEach(this.appConfig.widgetOnScreen.widgets, function(widget) {
          if (widget.inPanel) {
            var pid = widget.id + '_panel';
            this.panelManager.destroyPanel(pid);
          } else {
            this.widgetManager.destroyWidget(widget.id);
          }
        }, this);
      },
 
      isPartOfPopup: function (target) {
        var node = this.containerNode;
        var isInternal = target === node || html.isDescendant(target, node);
        return isInternal;
      },
 
      setConfig: function(config) {
        this.appConfig = config;
        this.createWidgetIcons();
      },
 
      createWidgetIcons: function() {
        this.toolsCount = 0;
        domConstruct.empty(this.containerNode);
        if (this.appConfig && this.appConfig.widgetOnScreen) {
          array.forEach(this.appConfig.widgetOnScreen.widgets, function(widget) {
            // widgets in placeholder
            if (widget.uri && widget.closeable) {
              this._addItem(widget);
              this.toolsCount++;
            }
          }, this);
        }
      },
 
      _pushId: function(panelId) {
        this._popId(panelId);
        this.openPanelIds.push(panelId);
      },
 
      _popId: function(panelId, update) {
        var idx = this.openPanelIds.indexOf(panelId), activePanelId;
        if (idx >= 0) {
          this.openPanelIds.splice(idx, 1);
          if (update && this.openPanelIds.length > 0) {
            activePanelId = this.openPanelIds[this.openPanelIds.length - 1];
            this.panelManager.openPanel(activePanelId);
          }
        }
      },
 
      _addItem: function(widget) {
        var widgetCopy = lang.clone(widget);
        var row = domConstruct.create('div', {
          'class': 'row'
        }, this.containerNode);
        domConstruct.create('div', {
          'class': 'widget-icon column',
          'style': 'background: url(' + widget.icon + ') no-repeat;'
        }, row);
        domConstruct.create('div', {
          'class': 'widget-label jimu-ellipsis column',
          title: widget.label,
          innerHTML: widget.label
        }, row);
 
        this.own(on(row, 'click', lang.hitch(this, function(event) {
          var box, panelPosition;
          event.stopPropagation();
          if (widget.inPanel) {
            box = domGeometry.getMarginBox(this.panelContainerNode);
            if (window.isRTL) {
              panelPosition = {
                relativeTo: 'browser',
                right: box.l,
                top: box.t,
                width: box.w,
                height: box.h
              };
            } else {
              panelPosition = {
                relativeTo: 'browser',
                left: box.l,
                top: box.t,
                width: box.w,
                height: box.h
              };
            }
            widgetCopy.panel = {
              uri: 'themes/DashboardTheme/panels/OnScreenPanel/Panel',
              position: panelPosition
            };
            this.panelManager.showPanel(widgetCopy)
            .then(lang.hitch(this, function(panel) {
              this.panels[panel.id] = panel;
              this._pushId(panel.id);
              panel.setPosition(panelPosition);
              if (this.panelOnCloseHandlerIds.indexOf(panel.id) < 0) {
                this.own(aspect.after(panel, 'onClose', lang.hitch(this, function() {
                  this._popId(panel.id, true);
                })));
                this.panelOnCloseHandlerIds.push(panel.id);
              }
              return panel;
            }))
            .then(lang.hitch(this, function(panel) {
              this.panelManager.openPanel(panel);
            }));
          } else {
            box = domGeometry.getMarginBox(row);
            this._toggleOffPanelWidget(widget, box.l + box.w, box.t);
          }
          domClass.remove(this.containerNode, 'in');
          domClass.add(this.containerNode, 'out');
        })));
      },
 
      _toggleOffPanelWidget: function(widgetConfig, left, top) {
        var index = this.openIds.indexOf(widgetConfig.id);
        if (index >= 0) {
          this.widgetManager.closeWidget(widgetConfig.id);
          this.openIds.splice(index, 1);
        } else {
          this.widgetManager.loadWidget(widgetConfig).then(
              lang.hitch(this, function(widget) {
            this.openIds.push(widgetConfig.id);
 
            widget.setPosition({
              left: left,
              top: top,
              zIndex: 100,
              relativeTo: 'map'
            });
            this.widgetManager.openWidget(widget);
            if (this.widgetOnCloseHandlerIds.indexOf(widgetConfig.id) < 0) {
              this.own(aspect.after(widget, 'onClose', lang.hitch(this, function() {
                index = this.openIds.indexOf(widgetConfig.id);
                if (index >= 0) {
                  this.openIds.splice(index, 1);
                }
              })));
              this.widgetOnCloseHandlerIds.push(widgetConfig.id);
            }
          }));
        }
      },
 
      setPanelPosition: function(pos) {
        array.forEach(this.openPanelIds, lang.hitch(this, function(id, index) {
          if (index === this.openPanelIds.length - 1) {
            pos.zIndex = 101;
          }
          if (this.panels[id].state === 'opened' || this.panels[id].state === 'active') {
            this.panels[id].setPosition(pos, true);
          }
        }));
      }
    });
    return clazz;
  });