forked from LRNWebComponents/hax-body
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhax-body.html
866 lines (854 loc) · 33.8 KB
/
hax-body.html
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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../vaadin-context-menu/vaadin-context-menu.html">
<link rel="import" href="../paper-menu/paper-menu.html">
<link rel="import" href="../paper-item/paper-item.html">
<link rel="import" href="../hax-blox/hax-blox.html">
<link rel="import" href="../pie-menu/pie-menu.html">
<link rel="import" href="../vaadin-context-menu/vaadin-context-menu.html">
<link rel="import" href="../video-player/video-player.html">
<link rel="import" href="../media-image/media-image.html">
<link rel="import" href="hax-text-context.html">
<link rel="import" href="hax-image-context.html">
<link rel="import" href="hax-iframe-context.html">
<link rel="import" href="hax-ce-context.html">
<link rel="import" href="hax-plate-context.html">
<link rel="import" href="hax-input-mixer.html">
<!--
`hax-body`
A LRN element that will change the world.
@demo demo/index.html
@microcopy - the mental model for this element
- body is effectively a body of content that can be manipulated in the browser. This is for other HAX elements ultimately to interface with and reside in. It is the controller of input and output for all of HAX as it exists in a document. body is not the <body> tag but we need a similar mental model container for all our other elements.
- text-context - the context menu that shows up when an item is active so it can have text based operations performed to it.
- plate/grid plate - a plate or grid plate is a container that we can operate on in HAX. it can also have layout / "global" type of body operations performed on it such as delete, duplicate and higher level format styling.
-->
<dom-module id="hax-body">
<template>
<style>
:host {
display: block;
min-height: 2em;
min-width: 2em;
}
vaadin-context-menu {
--vaadin-context-menu-overlay: {
box-shadow: none;
}
}
:host #bodycontainer ::slotted(.hax-context-menu) {
padding: 0;
margin: 0;
position: absolute;
visibility: hidden;
opacity: 0;
transition: visibility .4s ease, opacity .4s ease;
z-index: 1;
}
:host #bodycontainer ::slotted(#haxinputmixer) {
z-index: 2;
}
:host #bodycontainer ::slotted(.hax-context-visible) {
visibility: visible;
opacity: 1;
border: 1px solid black;
display: block;
}
:host[edit-mode] #bodycontainer ::slotted(*[contenteditable]:hover) {
-webkit-box-shadow: 0 0 0 3px #34e79a;
box-shadow: 0 0 0 3px #34e79a;
}
:host[edit-mode] #bodycontainer ::slotted(*.hax-active[contenteditable]) {
-webkit-box-shadow: 0 0 0 3px #03a87c !important;
box-shadow: 0 0 0 3px #03a87c !important;
cursor: text !important;
outline-color: #34e79a !important;
}
:host[edit-mode] #bodycontainer ::slotted(p.hax-active[contenteditable]) {
padding: .5em;
}
:host[edit-mode] #bodycontainer ::slotted(code.hax-active[contenteditable]) {
display: block;
}
:host[edit-mode] #bodycontainer ::slotted(hr[contenteditable]) {
padding: .5em;
color: black;
}
:host[edit-mode] #bodycontainer ::slotted(video-player[contenteditable]),
:host[edit-mode] #bodycontainer ::slotted(iframe[contenteditable]) {
border: .5em solid transparent;
}
/** Fix to support safari as it defaults to none */
:host[edit-mode] #bodycontainer ::slotted(*[contenteditable]) {
-webkit-user-select: all;
user-select: all;
cursor:pointer;
}
:host[edit-mode] #bodycontainer ::slotted(*[contenteditable]::-moz-selection),
:host[edit-mode] #bodycontainer ::slotted(*[contenteditable] *::-moz-selection) {
background-color: var(--hax-body-highlight, --paper-yellow-300);
color: black;
}
:host[edit-mode] #bodycontainer ::slotted(*[contenteditable]::selection),
:host[edit-mode] #bodycontainer ::slotted(*[contenteditable] *::selection) {
background-color: var(--hax-body-highlight, --paper-yellow-300);
color: black;
}
#bodycontainer {
-webkit-user-select: text;
user-select: text;
}
/* CSS grid overlay */
:host[show-grid] {
--offset: 0;
--max_width: 72rem;
--columns: 3;
--gutter: 1rem;
--baseline: 3rem;
--baseline-shift: 2rem;
--color: hsla(204, 80%, 72%, 0.25);
--color-text: hsla(204, 80%, 72%, 1);
--media-query: 'base';
cursor: crosshair !important;
}
@media (min-width: 35em) {
:host[show-grid] {
--color: hsla(286, 51%, 44%, 0.25);
--color-text: hsla(286, 51%, 44%, 1);
--media-query: 'small';
}
}
@media (min-width: 48em) {
:host[show-grid] {
--columns: 12;
--color: hsla(204, 80%, 72%, 0.25);
--color-text: hsla(204, 80%, 72%, 1);
--media-query: 'medium';
}
}
@media (min-width: 70em) {
:host[show-grid] {
--color: hsla(286, 51%, 44%, 0.25);
--color-text: hsla(286, 51%, 44%, 1);
--media-query: 'large';
}
}
:host[show-grid] {
--repeating-width: calc(100% / var(--columns));
--column-width: calc((100% / var(--columns)) - var(--gutter));
--background-width: calc(100% + var(--gutter));
--background-columns: repeating-linear-gradient(
to right,
var(--color),
var(--color) var(--column-width),
transparent var(--column-width),
transparent var(--repeating-width)
);
--background-baseline: repeating-linear-gradient(
to bottom,
var(--color),
var(--color) 1px,
transparent 1px,
transparent var(--baseline)
);
position: relative;
}
:host[show-grid]::before {
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
margin-right: auto;
margin-left: auto;
width: calc(100% - (2 * var(--offset)));
max-width: var(--max_width);
min-height: 100vh;
content: '';
background-image: var(--background-columns), var(--background-baseline);
background-size: var(--background-width) 100%;
background-position: 0 var(--baseline-shift);
z-index: 1000;
pointer-events: none;
}
:host[show-grid]::after {
content: var(--media-query);
position: fixed;
top: 1rem;
left: 1rem;
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;
color: var(--color-text);
}
#grid2 tile {
background-color: limegreen;
}
#grid2 [placeholder] {
background-color: lightgray;
}
#contextcontainer {
display: none;
}
</style>
<vaadin-context-menu selector=".has-pie-menu">
<template>
<pie-menu
id="pie"
center-label="Move" center-icon="open-with"
top-label="Up" top-icon="arrow-upward"
left-label="Left" left-icon="arrow-back"
bottom-label="Bottom" bottom-icon="arrow-downward"
right-label="Right" right-icon="arrow-forward"></pie-menu>
</template>
<div id="bodycontainer" class="ignore-activation">
<slot id="body"></slot>
</div>
</vaadin-context-menu>
<div id="contextcontainer">
<hax-text-context id="textcontextmenu" class="hax-context-menu ignore-activation"></hax-text-context>
<hax-image-context id="imagecontextmenu" class="hax-context-menu ignore-activation"></hax-image-context>
<hax-iframe-context id="iframecontextmenu" class="hax-context-menu ignore-activation"></hax-iframe-context>
<hax-ce-context id="cecontextmenu" class="hax-context-menu ignore-activation"></hax-ce-context>
<hax-plate-context id="platecontextmenu" class="hax-context-menu ignore-activation"></hax-plate-context>
<hax-input-mixer id="haxinputmixer" class="hax-context-menu ignore-activation"></hax-input-mixer>
</div>
</template>
<script>
Polymer({
is: 'hax-body',
listeners: {
'focusin': '_focusIn',
'mousedown': '_focusIn',
'hax-context-item-selected': '_haxContextOperation',
'hax-input-mixer-update': '_haxInputMixerOperation',
},
properties: {
/**
* State of if we are editing or not.
*/
editMode: {
type: Boolean,
value: false,
reflectToAttribute: true,
notify: true,
observer: '_editModeChanged',
},
/**
* Show grid on the screen.
*/
showGrid: {
type: Boolean,
value: false,
reflectToAttribute: true,
notify: true,
},
/**
* A reference to the active node in the slot.
*/
activeNode: {
type: Object,
value: null,
notify: true,
observer: '_activeNodeChanged',
},
/**
* A reference to the currently hovered node in the slot.
*/
activeHover: {
type: Object,
value: {},
notify: true,
observer: '_activeHoverChanged',
},
},
/**
* Ready state to tee everything up.
*/
ready: function(e) {
// mutation observer that ensures state of hax applied correctly
this._observer = Polymer.dom(this).observeNodes(function(info) {
// if we've got new nodes, we have to react to that
if (info.addedNodes.length > 0) {
info.addedNodes.map((node) => {
if (this._haxElementTest(node)) {
node.contentEditable = this.editMode;
this.fire('hax-body-tag-added', {node: node});
}
});
}
// if we dropped nodes via the UI (delete event basically)
if (info.removedNodes.length > 0) {
// handle removing items... not sure we need to do anything here
info.removedNodes.map((node) => {
if (this._haxElementTest(node) && !node.classList.contains('hax-active')) {
this.fire('hax-body-tag-removed', {node: node});
}
});
}
});
},
/**
* Insert new tag + content into the local DOM as a node.
*/
haxInsert: function(tag, content, properties = {}) {
var tags = this._validTags();
// verify this tag is a valid one
if (tags.includes(tag)) {
// create a new element fragment w/ content in it
// if this is a custom-element it won't expand though
var frag = document.createElement(tag);
frag.innerHTML = content;
// clone the fragment which will force an escalation to full node
var newNode = frag.cloneNode(true);
// support for properties if they exist
for (var property in properties) {
if (properties.hasOwnProperty(property)) {
newNode.setAttribute(property, properties[property]);
}
}
// insert at active insert point if we have one
if (this.activeNode !== null) {
Polymer.dom(this).insertBefore(newNode, this.activeNode.nextElementSibling);
this.activeNode = newNode;
}
else {
// send this into the root, which should filter it back down into the slot
Polymer.dom(this).appendChild(newNode);
this.activeNode = newNode;
}
return true;
}
return false;
},
/**
* Return the current hax content area as text that could be
* inserted into something.
*/
haxToContent: function() {
// remove from the body area so we can generate a legit html structure
this.hideContextMenus();
var __active = this.activeNode;
// null this to drop hax based classes
this.set('activeNode', null);
let children = Polymer.dom(this.$.body).getDistributedNodes();
var content = '';
for (var i = 0, len = children.length; i < len; i++) {
if (this._haxElementTest(children[i])) {
// delete this property since it's silly
children[i].contentEditable = false;
// see if this is a custom element
if (children[i].tagName.indexOf('-') > 0) {
// dig into the light dom and make sure we ONLY save that or else
// we'll save things in that weird shadow / light DOM cross over
// which then will keep unpacking over and over again
content += this.haxNodeToContent(children[i]);
}
else {
content += children[i].outerHTML + "\n";
}
}
}
// remove the contenteditable property
content = content.replace(/\scontenteditable=\"false\"/g, '');
// remove HAX specific classes / scoping classes
content = content.replace(/style-scope\slrnapp-book/g, '');
// remove empty class structures
content = content.replace(/\sclass=\"\"/g, '');
// re-apply contenteditable if needed
this._applyContentEditable(this.editMode);
// set active again
this.set('activeNode', __active);
return content;
},
/**
* Convert a node to the correct content object for saving.
*/
haxNodeToContent: function(node) {
var content = '';
// start to rebuild the same tag we got in a generalized way
content += '<' + node.tagName.toLowerCase();
// grab all of the original's attributes, and pass them to the replacement
for(var j = 0, l = node.attributes.length; j < l; ++j){
var nodeName = node.attributes.item(j).nodeName;
var nodeValue = node.attributes.item(j).nodeValue;
content += ' ' + nodeName + '="' + nodeValue + '"';
}
// set the opening tag
content += '>' + "\n";
// get content that is in the slots
let slotnodes = Polymer.dom(node).getEffectiveChildNodes();
// ensure there's something inside of this
if (slotnodes.length > 0) {
// loop through everything found in the slotted area and put it back in
for (var j = 0, len2 = slotnodes.length; j < len2; j++) {
// @todo this might delete comment tags and other non elements
// in the page.
if (typeof slotnodes[j].tagName !== typeof undefined) {
// if we're a custom element, keep digging, otherwise a simple
// self append is fine.
if (slotnodes[j].tagName.indexOf('-') > 0) {
content += this.haxNodeToContent(slotnodes[j]);
}
else {
content += ' ' + slotnodes[j].outerHTML + "\n";
}
}
}
}
// close the tag
content += '</' + node.tagName.toLowerCase() + '>' + "\n";
return content;
},
/**
* Duplicate node into the local DOM below the current item if we can.
*/
haxDuplicateNode: function(node) {
// move the context menu before duplicating!!!!
this.hideContextMenus();
var nodeClone = node.cloneNode(true);
// shouldn't be possible but might as well check
if (this.activeNode !== null) {
Polymer.dom(this).insertBefore(nodeClone, this.activeNode.nextElementSibling);
this.activeNode = nodeClone;
}
else {
Polymer.dom(this).appendChild(nodeClone);
this.activeNode = nodeClone;
}
return true;
},
/**
* Hide both context menus.
*/
hideContextMenus: function() {
this._hideContextMenu(this.$.textcontextmenu);
this._hideContextMenu(this.$.imagecontextmenu);
this._hideContextMenu(this.$.iframecontextmenu);
this._hideContextMenu(this.$.platecontextmenu);
this._hideContextMenu(this.$.cecontextmenu);
this._hideContextMenu(this.$.haxinputmixer);
},
/**
* Reposition context menus to match an element.
*/
positionContextMenus: function(node) {
// specialized menu based on the type of contextual menu we need
switch (node.tagName) {
// HTML primatives need manually defined and built out
case 'IMG':
this.__activeContextType = this.$.imagecontextmenu;
this._positionContextMenu(this.__activeContextType, node, -46, -46);
break;
case 'IFRAME':
case 'VIDEO':
case 'AUDIO':
this.__activeContextType = this.$.iframecontextmenu;
this._positionContextMenu(this.__activeContextType, node, -46, -46);
break;
default:
// check and see if we have a haxproperties function in place
// this opens the door for ANY custom-element to use HAX and supply
// properties for the three settings areas.
if (typeof node.getHaxProperties === "function") {
this.__activeContextType = this.$.cecontextmenu;
let props = node.getHaxProperties();
props.element = this.activeNode;
this.__activeContextType.setHaxProperties(props);
}
else {
this.__activeContextType = this.$.textcontextmenu;
}
this._positionContextMenu(this.__activeContextType, node, -46, -46);
break;
}
this._positionContextMenu(this.$.platecontextmenu, node, -46, -5);
setTimeout( () => {
this.__activeContextType.scrollIntoView({block: "center", behavior: "smooth", inline: "center"});
}, 200);
},
/**
* Move grid plate around
*/
haxMoveGridPlate: function(direction, node) {
// support moving things multiple directions
switch (direction) {
case 'up':
// menu is actually in the element for render purposes
// need to hide it, THEN check if thing above is null
this.hideContextMenus();
if (node.previousElementSibling !== null) {
Polymer.dom(this).insertBefore(node, node.previousElementSibling);
}
this.positionContextMenus(node);
break;
case 'down':
if (node.nextElementSibling !== null) {
this.hideContextMenus();
Polymer.dom(this).insertBefore(node.nextElementSibling, node);
this.positionContextMenus(node);
}
break;
// @todo support other directions for when inside of an element
}
return true;
},
/**
* Convert an element from one tag to another.
*/
haxChangeTagName: function (node, tagName) {
// ensure we're not in the document tree for the replaced node
this.hideContextMenus();
// Create a replacement tag of the desired type
var replacement = document.createElement(tagName);
// Grab all of the original's attributes, and pass them to the replacement
for(var i = 0, l = node.attributes.length; i < l; ++i){
var nodeName = node.attributes.item(i).nodeName;
var nodeValue = node.attributes.item(i).nodeValue;
replacement.setAttribute(nodeName, nodeValue);
}
// Persist contents
replacement.innerHTML = node.innerHTML;
// Switch!
Polymer.dom(this).replaceChild(replacement, node);
return replacement;
},
/**
* Delete the node passed in
*/
haxDeleteNode: function(node) {
// move the context menu before duplicating!!!!
this.hideContextMenus();
// shift active to a viable replacement
if (this.activeNode.previousElementSibling !== null) {
this.activeNode.previousElementSibling.focus();
}
else if (this.activeNode.nextElementSibling !== null) {
this.activeNode.nextElementSibling.focus();
}
else {
this.activeNode = null;
}
return Polymer.dom(this).removeChild(node);
},
/**
* Respond to hax operations.
*/
_haxContextOperation: function(e) {
let detail = e.detail;
// support a simple insert event to bubble up or everything else
switch(detail.eventName) {
// text based operations for primatives
case 'p':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
case 'code':
case 'blockquote':
// trigger the default selected value in context menu to match
this.$.textcontextmenu.selectedValue = detail.eventName;
this.activeNode = this.haxChangeTagName(this.activeNode, detail.eventName);
break;
case 'text-align-left':
this.activeNode.style.textAlign = null;
this.positionContextMenus(this.activeNode);
break;
case 'text-align-right':
this.activeNode.style.textAlign = 'right';
this.positionContextMenus(this.activeNode);
break;
// grid plate based operations
case 'grid-plate-duplicate':
this.haxDuplicateNode(this.activeNode);
break;
case 'grid-plate-delete':
this.haxDeleteNode(this.activeNode);
break;
case 'grid-plate-up':
this.haxMoveGridPlate('up', this.activeNode);
break;
case 'grid-plate-down':
this.haxMoveGridPlate('down', this.activeNode);
break;
case 'close-menu':
// this is the equivalent of hiding menus and resetting the board
this.activeNode = null;
break;
case 'hax-edit-property':
let haxInputMixer = this.$.haxinputmixer;
haxInputMixer.label = detail.target.label;
haxInputMixer.options = detail.target.options;
haxInputMixer.icon = detail.target.icon;
haxInputMixer.description = detail.target.description;
haxInputMixer.required = detail.target.required;
haxInputMixer.validation = detail.target.validation;
haxInputMixer.validationType = detail.target.validationType;
haxInputMixer.inputMethod = detail.target.inputMethod;
haxInputMixer.value = '';
// see if response should bind to the slot or property
if (typeof detail.target.propertyToBind !== typeof undefined && detail.target.propertyToBind != null && detail.target.propertyToBind != false) {
haxInputMixer.propertyToBind = detail.target.propertyToBind;
if (typeof this.activeNode[detail.target.propertyToBind] !== typeof undefined) {
haxInputMixer.value = this.activeNode[detail.target.propertyToBind];
}
}
else {
haxInputMixer.slotToBind = detail.target.slotToBind;
// @todo need a way of querying the slot's current value
//haxInputMixer.value = this.activeNode[detail.target.slotToBind];
}
// make input mixer show up
this._positionContextMenu(haxInputMixer, this.$.cecontextmenu, 0, -88);
break;
// image/iframe based operations
case 'hax-align-left':
this.activeNode.style.float = null;
this.activeNode.style.margin = null;
this.activeNode.style.display = null;
this.positionContextMenus(this.activeNode);
break;
case 'hax-align-center':
this.activeNode.style.float = null;
this.activeNode.style.margin = '0 auto';
this.activeNode.style.display = 'block';
this.positionContextMenus(this.activeNode);
break;
case 'hax-align-right':
this.activeNode.style.float = 'right';
this.activeNode.style.margin = null;
this.activeNode.style.display = null;
this.positionContextMenus(this.activeNode);
break;
case 'hax-size-change':
this.activeNode.style.width = detail.value + '%';
this.positionContextMenus(this.activeNode);
break;
// iframe based operations
case 'iframe-link':
var url = null;
if (this.activeNode.tagName == 'VIDEO-PLAYER') {
url = prompt("Link to embed:", this.activeNode.source);
}
else {
url = prompt("Link to embed:", this.activeNode.src);
}
if (url !== null && url !== '') {
// special support for our custom video-player
if (this.activeNode.tagName == 'VIDEO-PLAYER') {
this.activeNode.source = url;
}
else {
this.activeNode.src = url;
}
this.positionContextMenus(this.activeNode);
}
break;
// image operations
case 'image-src':
var url = null;
if (this.activeNode.tagName == 'MEDIA-IMAGE') {
url = prompt("Image URL:", this.activeNode.source);
}
else {
url = prompt("Image URL:", this.activeNode.src);
}
if (url !== null && url !== '') {
// special support for our custom video-player
if (this.activeNode.tagName == 'MEDIA-IMAGE') {
this.activeNode.source = url;
}
else {
this.activeNode.src = url;
}
this.positionContextMenus(this.activeNode);
}
break;
// alt info, convert this in the future
case 'image-alt':
var alt = prompt("Image alt information:", this.activeNode.alt);
if (alt !== null) {
this.activeNode.alt = alt;
}
break;
default:
// implies an event we haven't trapped for yet
console.log(detail);
break;
}
},
/**
* Respond to an input mixer call.
*/
_haxInputMixerOperation: function(e) {
// this is a big deal how simple this part is in the end
let mixer = e.detail.inputMixer;
console.log(mixer);
// if we have a property to bind, set that value from the
// widget that was dictated by the element itself
if (mixer.propertyToBind != null) {
this.activeNode[mixer.propertyToBind] = mixer.value;
}
// if we're told instead to do a slot bind, make a span tag
// with height same as parent and then mix in the innerHTML
else if (mixer.slotToBind != null) {
item = document.createElement('span');
item.style.height = 'inherit';
item.innerHTML = mixer.value;
//item.attribute.slot = mixer.slotToBind;
item.slot = mixer.slotToBind;
this.activeNode.appendChild(item);
}
// hide mixer
this._hideContextMenu(this.$.haxinputmixer);
},
/**
* Item has gained focus, change active element to match
*/
_focusIn: function(e) {
// only worry about these when we are in edit mode
if (this.editMode) {
var normalizedEvent = Polymer.dom(e);
var local = normalizedEvent.localTarget;
var tags = this._validTags();
// ensure this is a tag we care about / have support for
if (tags.includes(local.tagName.toLowerCase()) && !local.classList.contains('ignore-activation')) {
this.activeNode = e.target;
}
if (local.classList.contains('ignore-activation')) {
e.stopPropagation();
}
}
},
/**
* Return what tags are valid as part of the hax-body area for working against.
*/
_validTags: function(e) {
// @todo need a global way of hijacking this for integrations
var tags = ['p', 'div', 'ol', 'ul', 'li', 'a', 'hr', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'code', 'img', 'iframe', 'video', 'audio', 'hax-blox', 'video-player', 'media-image'];
return tags;
},
/**
* Notice the change between states for editing.
*/
_editModeChanged: function(newValue, oldValue) {
// fire above that we have changed states so things can react if needed
if (typeof oldValue !== typeof undefined) {
this.fire('hax-body-state', {editMode: newValue});
this._applyContentEditable(newValue);
if (newValue !== false && typeof this.activeNode !== typeof undefined && this.activeNode !== null) {
this.positionContextMenus(this.activeNode);
}
}
// hide menus when state changes
if (newValue === false) {
this.hideContextMenus();
}
},
/**
* Test if this is a HAX element or not
*/
_haxElementTest: function(node) {
if (typeof node.tagName !== typeof undefined && (node.tagName.substring(0, 4) !== "HAX-" || node.tagName === 'HAX-BLOX')) {
return true;
}
return false;
},
/**
* Walk everything we find and either enable or disable editable state.
*/
_applyContentEditable: function(status) {
let children = Polymer.dom(this.$.body).getDistributedNodes();
for (var i = 0, len = children.length; i < len; i++) {
if (this._haxElementTest(children[i])) {
children[i].contentEditable = status;
}
}
},
/**
* React to a new node being set to active.
*/
_activeNodeChanged: function (newValue, oldValue) {
// clean up the older one
if (typeof oldValue !== typeof undefined && oldValue != null) {
oldValue.classList.remove('hax-active');
}
if (this.editMode && typeof newValue !== typeof undefined && newValue !== null) {
// remove the menu, establish the new active, then reapply
// this is nessecary because the context menu gets appended into
// the document
this.hideContextMenus();
newValue.classList.add('hax-active');
this.$.textcontextmenu.selectedValue = newValue.tagName.toLowerCase();
// position the operations / in context element
this.positionContextMenus(newValue);
if (newValue.style.textAlign == 'right') {
this.$.textcontextmenu.justifyIcon = 'editor:format-align-right';
this.$.textcontextmenu.justifyValue = 'text-align-right';
}
else if (newValue.style.textAlign == 'left') {
this.$.textcontextmenu.justifyIcon = 'editor:format-align-left';
this.$.textcontextmenu.justifyValue = 'text-align-left';
}
else if (newValue.style.float == 'left') {
this.$.iframecontextmenu.justifyIcon = 'editor:format-align-left';
this.$.iframecontextmenu.justifyValue = 'hax-align-left';
this.$.cecontextmenu.justifyIcon = 'editor:format-align-left';
this.$.cecontextmenu.justifyValue = 'hax-align-left';
this.$.imagecontextmenu.justifyIcon = 'editor:format-align-left';
this.$.imagecontextmenu.justifyValue = 'hax-align-left';
}
else if (newValue.style.float == 'right') {
this.$.iframecontextmenu.justifyIcon = 'editor:format-align-right';
this.$.iframecontextmenu.justifyValue = 'hax-align-right';
this.$.cecontextmenu.justifyIcon = 'editor:format-align-right';
this.$.cecontextmenu.justifyValue = 'hax-align-right';
this.$.imagecontextmenu.justifyIcon = 'editor:format-align-right';
this.$.imagecontextmenu.justifyValue = 'hax-align-right';
}
else if (newValue.style.margin == '0 auto') {
this.$.iframecontextmenu.justifyIcon = 'editor:format-align-center';
this.$.iframecontextmenu.justifyValue = 'hax-align-center';
this.$.cecontextmenu.justifyIcon = 'editor:format-align-center';
this.$.cecontextmenu.justifyValue = 'hax-align-center';
this.$.imagecontextmenu.justifyIcon = 'editor:format-align-center';
this.$.imagecontextmenu.justifyValue = 'hax-align-center';
}
}
// just hide menus if we don't have an active item
else if (newValue === null) {
this.hideContextMenus();
this.$.textcontextmenu.justifyIcon = 'editor:format-align-left';
this.$.textcontextmenu.justifyValue = 'text-align-left';
}
},
/**
* React to a new node being hovered over.
*/
_activeHoverChanged: function (newValue, oldValue) {
// if we have actually hovered on the element that's active, trigger
// the grid plate context menu to show up
if (typeof newValue !== typeof undefined && newValue !== null && this.activeNode === newValue) {
// position the operations / in context element
this._positionContextMenu(this.$.platecontextmenu, newValue, -48, -5);
}
},
/**
* Handle display and position of the context menu
*/
_positionContextMenu: function (menu, target, xoffset, yoffset) {
menu.classList.add('hax-context-visible');
try {
Polymer.dom(this).insertBefore(menu, target);
}
catch(err) {
Polymer.dom(target.parentNode).insertBefore(menu, target);
}
menu.style["margin-left"] = (xoffset + target.offsetLeft) + 'px';
menu.style["margin-top"] = yoffset + 'px';
},
/**
* Simple hide / reset of whatever menu it's handed.
*/
_hideContextMenu: function(menu) {
menu.classList.remove('hax-context-visible');
Polymer.dom(this.$.contextcontainer).appendChild(menu);
},
});
</script>
</dom-module>