« get me outta code hell

FlashSide.js « flash « things « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things/flash/FlashSide.js
blob: 72782bdd27519a3b79fd1a0294542f44d7c379f0 (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
const FLASH_DATA_FILE = 'flashes.yaml';

import {V} from '#composite';
import {sortFlashesChronologically} from '#sort';
import Thing from '#thing';

import {exposeConstant} from '#composite/control-flow';
import {color, contentString, directory, name, soupyFind, thingList}
  from '#composite/wiki-properties';

export class FlashSide extends Thing {
  static [Thing.referenceType] = 'flash-side';
  static [Thing.friendlyName] = `Flash Side`;
  static [Thing.wikiData] = 'flashSideData';

  static [Thing.getPropertyDescriptors] = ({FlashAct}) => ({
    // Update & expose

    name: name(V('Unnamed Flash Side')),
    directory: directory(),
    color: color(),
    listTerminology: contentString(),

    acts: thingList(V(FlashAct)),

    // Update only

    find: soupyFind(),

    // Expose only

    isFlashSide: exposeConstant(V(true)),
  });

  static [Thing.yamlDocumentSpec] = {
    fields: {
      'Side': {property: 'name'},
      'Directory': {property: 'directory'},
      'Color': {property: 'color'},
      'List Terminology': {property: 'listTerminology'},
    },
  };

  static [Thing.findSpecs] = {
    flashSide: {
      referenceTypes: ['flash-side'],
      bindTo: 'flashSideData',
    },
  };

  static [Thing.reverseSpecs] = {
    flashSidesWhoseActsInclude: {
      bindTo: 'flashSideData',

      referencing: flashSide => [flashSide],
      referenced: flashSide => flashSide.acts,
    },
  };

  static [Thing.getYamlLoadingSpec] = ({
    documentModes: {allInOne},
    thingConstructors: {Flash, FlashAct},
  }) => ({
    title: `Process flashes file`,
    file: FLASH_DATA_FILE,

    documentMode: allInOne,
    documentThing: document =>
      ('Side' in document
        ? FlashSide
     : 'Act' in document
        ? FlashAct
        : Flash),

    connect(results) {
      let thing, i;

      for (i = 0; thing = results[i]; i++) {
        if (thing.isFlashSide) {
          const side = thing;
          const acts = [];

          for (i++; thing = results[i]; i++) {
            if (thing.isFlashAct) {
              const act = thing;
              const flashes = [];

              for (i++; thing = results[i]; i++) {
                if (thing.isFlash) {
                  const flash = thing;

                  flash.act = act;
                  flashes.push(flash);

                  continue;
                }

                i--;
                break;
              }

              act.side = side;
              act.flashes = flashes;
              acts.push(act);

              continue;
            }

            if (thing.isFlash) {
              throw new Error(`Flashes must be under an act`);
            }

            i--;
            break;
          }

          side.acts = acts;

          continue;
        }

        if (thing.isFlashAct) {
          throw new Error(`Acts must be under a side`);
        }

        if (thing.isFlash) {
          throw new Error(`Flashes must be under a side and act`);
        }
      }
    },

    sort({flashData}) {
      sortFlashesChronologically(flashData);
    },
  });
}