Newer
Older
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
/**
* @module package/quiqqer/menu/bin/Controls/Independent/MenuPanel
* @author www.pcsg.de (Henning Leutz)
*/
define('package/quiqqer/menu/bin/Controls/Independent/MenuPanel', [
'qui/QUI',
'qui/controls/desktop/Panel',
'qui/controls/sitemap/Map',
'qui/controls/sitemap/Item',
'qui/controls/windows/Confirm',
'qui/controls/contextmenu/Menu',
'qui/controls/contextmenu/Item',
'Locale',
'package/quiqqer/menu/bin/classes/Independent/Handler',
'Mustache',
'text!package/quiqqer/menu/bin/Controls/Independent/MenuPanel.Create.html',
'css!package/quiqqer/menu/bin/Controls/Independent/MenuPanel.css'
], function (QUI, QUIPanel, QUIMap, QUIMapItem, QUIConfirm, QUIContextMenu, QUIContextMenuItem,
QUILocale, MenuHandler, Mustache, templateCreate) {
"use strict";
const lg = 'quiqqer/menu';
const Handler = new MenuHandler();
return new Class({
Extends: QUIPanel,
Type : 'package/quiqqer/menu/bin/Controls/Independent/MenuPanel',
Binds: [
'$onShow',
'$onCreate',
'$onInject',
'$openItem',
'$onContextMenu',
'addItem',
'save',
],
options: {
menuId: false
},
initialize: function (options) {
this.parent(options);
this.$ActiveItem = null;
this.$ActiveMapItem = null;
this.$Map = null;
this.addEvents({
onShow : this.$onShow,
onInject: this.$onInject,
onCreate: this.$onCreate
});
},
$onCreate: function () {
this.getContent().addClass('quiqqer-menu-menuPanel');
// buttons
this.addButton({
name : 'save',
text : QUILocale.get('quiqqer/quiqqer', 'save'),
events: {
click: this.save
}
});
this.addButton({
type: 'separator'
});
this.addButton({
name : 'add',
text : QUILocale.get('quiqqer/quiqqer', 'add'),
events: {
disabled: true,
text : QUILocale.get('quiqqer/quiqqer', 'remove'),
events : {
click: this.addItem
}
});
// content
this.$MapContainer = new Element('div', {
'class': 'quiqqer-menu-menuPanel-mapContainer'
}).inject(this.getContent());
this.$InnerContainer = new Element('div', {
'class': 'quiqqer-menu-menuPanel-innerContainer'
}).inject(this.getContent());
this.$Map = new QUIMap({}).inject(this.$MapContainer);
},
$onInject: function () {
this.Loader.show();
Handler.getMenu(this.getAttribute('menuId')).then((menuData) => {
this.setAttribute('title', menuData.title);
this.setAttribute('icon', 'fa fa-bars');
this.refresh();
// build sitemap
if (menuData.data !== null &&
typeof menuData.data.children !== 'undefined' &&
menuData.data.children.length
) {
const buildChildren = (Parent, children) => {
for (i = 0, len = children.length; i < len; i++) {
data = children[i];
text : data.titleFrontend,
icon : data.typeIcon,
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
itemTitle: data.title,
itemType : data.type,
itemIcon : data.icon,
itemData : data.data,
events : {
click : this.$openItem,
contextMenu: this.$onContextMenu
}
});
Parent.appendChild(Item);
if (typeof data.children !== 'undefined' && data.children.length) {
buildChildren(Item, data.children);
}
}
};
buildChildren(this.$Map, menuData.data.children);
}
this.Loader.hide();
});
},
$onShow: function () {
},
save: function () {
this.Loader.show();
// map to array
const toArray = function (Item) {
let items = Item.getChildren();
if (Item.getType() === 'qui/controls/sitemap/Map') {
items = Item.$items;
}
let children = [];
for (let i = 0, len = items.length; i < len; i++) {
children.push(toArray(items[i]));
}
if (Item.getType() === 'qui/controls/sitemap/Map') {
return {
children: children
};
}
return {
title : Item.getAttribute('itemTitle'),
type : Item.getAttribute('itemType'),
data : Item.getAttribute('itemData'),
children: children
};
};
let result = toArray(this.$Map);
let title = null;
let workingTitle = null;
Handler.saveMenu(
this.getAttribute('menuId'),
title,
workingTitle,
result
).then(() => {
this.Loader.hide();
});
},
addItem: function (Parent, where) {
if (typeof where === 'undefined') {
where = 'bottom';
}
if (typeof Parent === 'undefined') {
const items = this.$Map.getSelectedChildren();
if (items.length) {
Parent = items[0];
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
}
}
new QUIConfirm({
title : QUILocale.get(lg, 'quiqqer.menu.independent.addItem.title'),
maxHeight: 400,
maxWidth : 500,
autoclose: false,
events : {
onOpen: (Win) => {
const Content = Win.getContent();
Win.Loader.show();
Content.set('html', Mustache.render(templateCreate, {
textTitle: QUILocale.get('quiqqer/quiqqer', 'title'),
textName : QUILocale.get('quiqqer/quiqqer', 'name'),
textType : QUILocale.get('quiqqer/quiqqer', 'type')
}));
Handler.getItemTypes().then((list) => {
const Types = Content.getElement('[name="itemType"]');
for (let i = 0, len = list.length; i < len; i++) {
new Element('option', {
html : list[i].title,
value : list[i].class,
'data-icon': list[i].icon
}).inject(Types);
}
Types.value = 'QUI\\Menu\\Independent\\Items\\Custom';
return QUI.parse(Win.getContent());
}).then(function () {
Win.Loader.hide();
});
},
onSubmit: (Win) => {
const Content = Win.getContent();
const Form = Content.getElement('form');
let current = QUILocale.getCurrent();
let title = Form.elements.itemTitle.value;
let type = Form.elements.itemType.value;
const Option = Form.elements.itemType.getElement(
'option[value="' + CSS.escape(type) + '"]'
);
try {
title = JSON.decode(title);
if (typeof title[current] !== 'undefined') {
title = title[current];
}
} catch (e) {
}
let itemAttributes = {
text : title,
icon : Option.get('data-icon'),
itemTitle: Form.elements.itemTitle.value,
itemType : Form.elements.itemType.value,
itemIcon : '',
events : {
click : this.$openItem,
contextMenu: this.$onContextMenu
}
};
if (where === 'bottom') {
Parent.appendChild(new QUIMapItem(itemAttributes));
if (typeof Parent === 'function') {
Parent.open();
}
} else {
Parent.appendChild(new QUIMapItem(itemAttributes), where);
}
Win.close();
}
}
}).open();
},
/**
* opens the item - display the item data
*
* @param Item
*/
$openItem: function (Item) {
this.Loader.show();
this.$ActiveItem = null;
this.$ActiveMapItem = null;
Handler.getItemTypes().then((list) => {
let control = '';
let type = Item.getAttribute('itemType');
for (let i = 0, len = list.length; i < len; i++) {
if (list[i].class === type) {
control = list[i].jsControl;
break;
}
}
if (control === '') {
this.Loader.hide();
return;
}
require([control], (cls) => {
const SitemapItem = this.$getActiveSitemapItem();
this.$ActiveMapItem = this.$getActiveSitemapItem();
this.$ActiveItem = new cls({
title : SitemapItem.getAttribute('itemTitle'),
type : SitemapItem.getAttribute('itemType'),
icon : SitemapItem.getAttribute('itemIcon'),
data : SitemapItem.getAttribute('itemData'),
events: {
load: () => {
this.Loader.hide();
}
}
}).inject(this.$InnerContainer);
}, (err) => {
console.error(err);
this.Loader.hide();
});
});
},
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
$unloadCurrentItem: function () {
if (!this.$ActiveItem) {
return;
}
if (typeof this.$ActiveItem.save !== 'function') {
return;
}
const data = this.$ActiveItem.save();
if (!this.$ActiveMapItem) {
return;
}
this.$ActiveMapItem.setAttribute('itemTitle', data.title);
this.$ActiveMapItem.setAttribute('itemIcon', data.icon);
this.$ActiveMapItem.setAttribute('itemData', data.data);
},
$getActiveSitemapItem: function () {
const item = this.$Map.getSelectedChildren();
if (item.length) {
return item[0];
}
return null;
},
/**
* Context menu für a sitemap item
*
* @param Item
* @param event
*/
$onContextMenu: function (Item, event) {
event.stop();
const pos = Item.getElm().getPosition();
const Menu = new QUIContextMenu({
events: {
onBlur: function (Instance) {
Instance.hide();
Instance.destroy();
}
}
});
Menu.appendChild(new QUIContextMenuItem({
icon : 'fa fa-arrow-up',
text : QUILocale.get(lg, 'context.menu.insertBefore'),
events: {
click: () => {
this.addItem(Item, 'before');
}
}
}));
Menu.appendChild(new QUIContextMenuItem({
icon : 'fa fa-arrow-down',
text : QUILocale.get(lg, 'context.menu.insertAfter'),
events: {
click: () => {
this.addItem(Item, 'after');
}
}
}));
Menu.appendChild(new QUIContextMenuItem({
icon : 'fa fa-level-down',
text : QUILocale.get(lg, 'context.menu.insertChild'),
events: {
click: () => {
this.addItem(Item);
}
}
}));
Menu.appendChild(new QUIContextMenuItem({
icon : 'fa fa-trash',
text : QUILocale.get(lg, 'context.menu.deleteChild'),
events: {
click: () => {
Item.destroy();
}
}
}));
Menu.inject(document.body);
Menu.setPosition(pos.x, pos.y + 30);
Menu.setTitle(Item.getAttribute('text'));
Menu.show();
Menu.focus();
}
});
});