aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/DropConverter.ts
blob: f46ea393a11d45fc020010c51e486c5cd1c3bf77 (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
import { DragManager } from './DragManager';
import { Doc, DocListCast, Opt } from '../../fields/Doc';
import { DocumentType } from '../documents/DocumentTypes';
import { ObjectField } from '../../fields/ObjectField';
import { StrCast, Cast } from '../../fields/Types';
import { Docs } from '../documents/Documents';
import { ScriptField, ComputedField } from '../../fields/ScriptField';
import { RichTextField } from '../../fields/RichTextField';
import { ImageField } from '../../fields/URLField';
import { ScriptingGlobals } from './ScriptingGlobals';
import { listSpec } from '../../fields/Schema';
import { ButtonType } from '../views/nodes/button/FontIconBox';

export function MakeTemplate(doc: Doc, first: boolean = true, rename: Opt<string> = undefined, templateField: string = '') {
    if (templateField) Doc.GetProto(doc).title = templateField; /// the title determines which field is being templated
    doc.isTemplateDoc = makeTemplate(doc, first, rename);
    return doc;
}
//
// converts 'doc' into a template that can be used to render other documents.
// the title of doc is used to determine which field is being templated, so
// passing a value for 'rename' allows the doc to be given a meangingful name
// after it has been converted to
function makeTemplate(doc: Doc, first: boolean = true, rename: Opt<string> = undefined): boolean {
    const layoutDoc = doc.layout instanceof Doc && doc.layout.isTemplateForField ? doc.layout : doc;
    if (layoutDoc.layout instanceof Doc) {
        // its already a template
        return true;
    }
    const layout = StrCast(layoutDoc.layout).match(/fieldKey={'[^']*'}/)![0];
    const fieldKey = layout.replace("fieldKey={'", '').replace(/'}$/, '');
    const docs = DocListCast(layoutDoc[fieldKey]);
    let any = false;
    docs.forEach(d => {
        if (!StrCast(d.title).startsWith('-')) {
            any = Doc.MakeMetadataFieldTemplate(d, Doc.GetProto(layoutDoc)) || any;
        } else if (d.type === DocumentType.COL || d.data instanceof RichTextField) {
            any = makeTemplate(d, false) || any;
        }
    });
    if (first) {
        if (!docs.length) {
            // bcz: feels hacky : if the root level document has items, it's not a field template
            any = Doc.MakeMetadataFieldTemplate(doc, Doc.GetProto(layoutDoc)) || any;
        }
    }
    if (layoutDoc[fieldKey] instanceof RichTextField || layoutDoc[fieldKey] instanceof ImageField) {
        if (!StrCast(layoutDoc.title).startsWith('-')) {
            any = Doc.MakeMetadataFieldTemplate(layoutDoc, Doc.GetProto(layoutDoc));
        }
    }
    rename && (doc.title = rename);
    return any;
}
export function convertDropDataToButtons(data: DragManager.DocumentDragData) {
    data?.draggedDocuments.map((doc, i) => {
        let dbox = doc;
        // bcz: isButtonBar is intended to allow a collection of linear buttons to be dropped and nested into another collection of buttons... it's not being used yet, and isn't very elegant
        if (doc.type === DocumentType.FONTICON || StrCast(Doc.Layout(doc).layout).includes('FontIconBox')) {
            if (data.removeDropProperties || dbox.removeDropProperties) {
                //dbox = Doc.MakeEmbedding(doc);  // don't need to do anything if dropping an icon doc onto an icon bar since there should be no layout data for an icon
                dbox = Doc.MakeEmbedding(dbox);
                const dragProps = Cast(dbox.removeDropProperties, listSpec('string'), []);
                const remProps = (data.removeDropProperties || []).concat(Array.from(dragProps));
                remProps.map(prop => (dbox[prop] = undefined));
            }
        } else if (!doc.onDragStart && !doc.isButtonBar) {
            const layoutDoc = doc; // doc.layout instanceof Doc && doc.layout.isTemplateForField ? doc.layout : doc;
            if (layoutDoc.type !== DocumentType.FONTICON) {
                !layoutDoc.isTemplateDoc && makeTemplate(layoutDoc);
            }
            layoutDoc.isTemplateDoc = true;
            dbox = Docs.Create.FontIconDocument({
                _nativeWidth: 100,
                _nativeHeight: 100,
                _width: 100,
                _height: 100,
                backgroundColor: StrCast(doc.backgroundColor),
                title: StrCast(layoutDoc.title),
                btnType: ButtonType.ClickButton,
                icon: layoutDoc.isTemplateDoc ? 'font' : 'bolt',
            });
            dbox.dragFactory = layoutDoc;
            dbox.removeDropProperties = doc.removeDropProperties instanceof ObjectField ? ObjectField.MakeCopy(doc.removeDropProperties) : undefined;
            dbox.onDragStart = ScriptField.MakeFunction('makeDelegate(this.dragFactory)');
        } else if (doc.isButtonBar) {
            dbox.ignoreClick = true;
        }
        data.droppedDocuments[i] = dbox;
    });
}
ScriptingGlobals.add(
    function convertToButtons(dragData: any) {
        convertDropDataToButtons(dragData as DragManager.DocumentDragData);
    },
    'converts the dropped data to buttons',
    '(dragData: any)'
);