aboutsummaryrefslogtreecommitdiff
path: root/static/ts/globals.ts
blob: 75fb1df98a08efaa254aa604c5aecd9032d2fff2 (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
/**
 * Different variables and values which for different reasons needs to be
 * global. Window Value's are those that are bound to the `window`
 * context in JavaScript, so is really always available, no opt out.
 * @module
 */

export {
    find_block,
    vcal_objects, event_calendar_mapping
}

import { VEvent } from './vevent'
import { uid } from './types'
import { ComponentBlock } from './components/vevent-block'

import { v4 as uuid } from 'uuid'
import { setup_popup_element } from './components/popup-element'

/**
 * All VEvent objects on current page, indexed by their unique identifiers.
 *
 * A global object store.
 *
 * Also bound to the window object for easy access.
 */
const vcal_objects: Map<uid, VEvent> = new Map;

/**
 * Mapping from VEvent unique identifier, to name of its calendar. Should
 * probably not be global, so refrain from using it.
 */
const event_calendar_mapping: Map<uid, string> = new Map;

declare global {
    interface Window {
        vcal_objects: Map<uid, VEvent>;
        /**
         * How the calendar is currently formatted. Should be set by the backend
         * through a simple `script`-tag.
         */
        VIEW: 'month' | 'week';
        /**
         * However editing of events is enabled or not.
         * Should be set by the backend through a simple `script`-tag.
         */
        EDIT_MODE: boolean;
        /**
         * Name of the calendar to assume when creating new events.
         * Should be set by the backend through a simple `script`-tag.
         */
        default_calendar: string;

        addNewEvent(): void;
    }
}
window.vcal_objects = vcal_objects;


window.addNewEvent = () => {
    let ev = new VEvent();
    let uid = uuid()
    let now = new Date()
    /* Round seconds to 0, since time inputs wants exact seconds */
    now.setUTCSeconds(0);
    ev.setProperties([
        ['uid', uid],
        ['dtstart', now, 'date-time'],
        ['dtend', new Date(now.getTime() + 3600 * 1000), 'date-time'],
    ])
    ev.calendar = window.default_calendar;

    vcal_objects.set(uid, ev);

    let popup = setup_popup_element(ev);
    popup.maximize();
}

function find_block(uid: uid): ComponentBlock | null {
    let obj = vcal_objects.get(uid)
    if (obj === undefined) {
        return null;
    }
    for (let el of obj.registered) {
        if (el instanceof ComponentBlock) {
            return el;
        }
    }
    // throw 'Popup not fonud';
    return null;
}