-
Notifications
You must be signed in to change notification settings - Fork 0
/
section.js
138 lines (116 loc) · 2.93 KB
/
section.js
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
//@ts-check
import container from "./container.js";
/**
* Section
* @typedef {Object} section
* @property {function():number} getId
* @property {function():string} getType
* @property {function():string} getStyle
* @property {function():string | undefined} getLabel - imageCard does not need label.
* @property {function():Object} getValue
* @property {function(Object):void} setValue
* @property {function():import("./img.js").img[]} getImages
* @property {function({url: string, caption: string}):number} addImage
* @property {function(string, string):void} setCaption
* @property {function(string):number} removeImage
* @property {function} toJSON
*/
/**
* Factory function for section
* @param {Object} data
* @param {number} data.id
* @param {string} data.type
* @param {string | undefined} data.label imageCard does not need label
* @param {Object | undefined} data.value
* @param {{url: string, caption: string}[] | undefined} data.images
* @param {string | undefined} data.style - Title, subtitle, private etc.
* @returns {section} section object
* @throws {TypeError} if data parameter is undefined
* @throws {TypeError} if data.id is undefined
* @throws {TypeError} if data.id is not an Integer
* @throws {TypeError} if data.type is undefined or zero length string
* @throws {TypeError} if data.type is not a String
*/
export default function (data) {
validate(data);
const { id } = data;
const { type } = data;
const { label } = data;
const { style } = data;
let { value } = data;
/**
* @type {import("./container.js").container}
*/
const images = container(data);
function getId() {
return id;
}
function getType() {
return type;
}
function getStyle() {
return style;
}
function getLabel() {
return label;
}
function getValue() {
return value;
}
function setValue(param) {
value = param;
}
function getImages() {
return images.getImgs();
}
function addImage(param) {
return images.addImg(param);
}
function removeImage(url) {
return images.removeImg(url);
}
function setCaption(url, caption) {
const img = images.getImg(url);
img.setCaption(caption);
}
function toJSON() {
return {
id,
type,
style,
label,
value,
images: images ? images.toJSON() : undefined,
};
}
return {
getId,
getType,
getStyle,
getLabel,
getValue,
setValue,
getImages,
addImage,
setCaption,
removeImage,
toJSON,
};
}
function validate(data) {
if (!data) {
throw new TypeError("Constructor is undefined.");
}
if (!data.id) {
throw new TypeError("Id is undefined.");
}
if (!Number.isInteger(data.id)) {
throw new TypeError("Id is not an Integer.");
}
if (!data.type) {
throw new TypeError("Type is undefined or zero length string.");
}
if (typeof data.type !== "string") {
throw new TypeError("Type is not a String.");
}
}