summaryrefslogtreecommitdiffstats
path: root/browser/src/control/Control.Sidebar.js
blob: 322d0e9480e88bd43ebcccddd6dea81c369a5a05 (plain)
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
/* -*- js-indent-level: 8 -*- */
/*
 * L.Control.Sidebar
 */

/* global $ */
L.Control.Sidebar = L.Control.extend({

	options: {
		animSpeed: 1000 /* Default speed: to be used on load */
	},

	container: null,
	builder: null,

	onAdd: function (map) {
		this.map = map;

		this.builder = new L.control.jsDialogBuilder({mobileWizard: this, map: map, cssClass: 'jsdialog sidebar'});
		this.container = L.DomUtil.create('div', 'sidebar-container', $('#sidebar-panel').get(0));

		this.map.on('sidebar', this.onSidebar, this);
		this.map.on('jsdialogupdate', this.onJSUpdate, this);
		this.map.on('jsdialogaction', this.onJSAction, this);
	},

	onRemove: function() {
		this.map.off('sidebar');
		this.map.off('jsdialogupdate', this.onJSUpdate, this);
		this.map.off('jsdialogaction', this.onJSAction, this);
	},

	closeSidebar: function() {
		$('#sidebar-dock-wrapper').hide();
		this.map._onResize();

		if (!this.map.editorHasFocus()) {
			this.map.fire('editorgotfocus');
			this.map.focus();
		}

		if (window.initSidebarState) {
			this.map.uiManager.setSavedState('ShowSidebar', false);
			window.initSidebarState = false;
		}
	},

	onJSUpdate: function (e) {
		var data = e.data;

		if (data.jsontype !== 'sidebar')
			return;

		if (!this.container)
			return;

		var controlId = data.control.id;
		var control = this.container.querySelector('[id=\'' + controlId + '\']');
		if (!control) {
			window.app.console.warn('jsdialogupdate: not found control with id: "' + data.control.id + '"');
			return;
		}

		var parent = control.parentNode;
		if (!parent)
			return;

		if (!this.builder)
			return;

		var scrollTop = control.scrollTop;
		var focusedElement = document.activeElement;
		var focusedElementInDialog = focusedElement ? this.container.querySelector('[id=\'' + focusedElement.id + '\']') : null;
		var focusedId = focusedElementInDialog ? focusedElementInDialog.id : null;
		control.style.visibility = 'hidden';

		var temporaryParent = L.DomUtil.create('div');
		this.builder.build(temporaryParent, [data.control], false);
		parent.insertBefore(temporaryParent.querySelector('[id=\'' + controlId + '\']'), control.nextSibling);
		L.DomUtil.remove(control);

		var newControl = this.container.querySelector('[id=\'' + controlId + '\']');
		if (newControl)
			newControl.scrollTop = scrollTop;

		if (focusedId)
			this.container.querySelector('[id=\'' + focusedId + '\']').focus();
	},

	onJSAction: function (e) {
		var data = e.data;

		if (data.jsontype !== 'sidebar')
			return;

		if (!this.builder)
			return;

		if (!this.container)
			return;

		// Panels share the same name for main containers, do not execute actions for them
		// if panel has to be shown or hidden, full update will appear
		if (data.data && (data.data.control_id === 'contents' ||
			data.data.control_id === 'Panel' ||
			data.data.control_id === 'titlebar')) {
			window.app.console.log('Ignored action: ' + data.data.action_type + ' for control: ' + data.data.control_id);
			return;
		}

		this.builder.executeAction(this.container, data.data);
	},

	onResize: function() {
		var wrapper = document.getElementById('sidebar-dock-wrapper');
		wrapper.style.maxHeight = document.getElementById('document-container').getBoundingClientRect().height + 'px';
	},

	unsetSelectedSidebar: function() {
		this.map.uiManager.setSavedState('PropertyDeck', false);
		this.map.uiManager.setSavedState('SdSlideTransitionDeck', false);
		this.map.uiManager.setSavedState('SdCustomAnimationDeck', false);
		this.map.uiManager.setSavedState('SdMasterPagesDeck', false);
	},

	onSidebar: function(data) {
		var sidebarData = data.data;
		this.builder.setWindowId(sidebarData.id);
		$(this.container).empty();

		if (sidebarData.action === 'close' || window.app.file.disableSidebar || this.map.isPermissionReadOnly()) {
			this.closeSidebar();
		} else if (sidebarData.children) {
			for (var i = sidebarData.children.length - 1; i >= 0; i--) {
				if (sidebarData.children[i].type !== 'deck' || sidebarData.children[i].visible === false)
					sidebarData.children.splice(i, 1);
			}

			if (sidebarData.children.length) {
				var wrapper = document.getElementById('sidebar-dock-wrapper');

				this.onResize();

				if (this.map.getDocType() === 'presentation' && sidebarData.children && sidebarData.children[0] && sidebarData.children[0].id) {
					this.unsetSelectedSidebar();
					this.map.uiManager.setSavedState(sidebarData.children[0].id, true);
				}

				this.builder.build(this.container, [sidebarData]);
				if (wrapper.style.display === 'none')
					$('#sidebar-dock-wrapper').show(this.options.animSpeed);

				if (window.initSidebarState) {
					this.map.uiManager.setSavedState('ShowSidebar', true);
					window.initSidebarState = false;
				}
			} else {
				this.closeSidebar();
			}
		}
	},

	setTabs: function(/*tabs*/) {
	},

	selectedTab: function() {
		// implement in child classes
	},
});

L.control.sidebar = function (options) {
	return new L.Control.Sidebar(options);
};