-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
138 lines (113 loc) · 3.43 KB
/
build.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
Fliplet.Container = Fliplet.Container || {};
const instances = [];
Fliplet().then(function() {
Fliplet.Widget.instance('dynamic-container', function(data, parent) {
const renderingOption = data.renderingOption || 'default';
const container = new Promise((resolve) => {
// const $el = $(this);
let loadData;
const vm = new Vue({
data: {
context: [],
parent: parent
},
methods: {
_setData(key, data) {
return Fliplet.Hooks.run('containerDataRetrieved', { container: this, key, data }).then(() => {
if (!data) {
return;
}
if (typeof this[key] === 'undefined') {
return this.$set(this.context, key, data);
}
if (Array.isArray(data)) {
this.context.length = 0;
this.context.push(...data);
} else {
this.context = data;
}
});
},
load(key, fn) {
if (typeof key === 'function') {
fn = key;
key = 'context';
}
let result = fn();
if (!(result instanceof Promise)) {
result = Promise.resolve(result);
}
return result.then(res => this._setData(key, res));
}
}
});
if (renderingOption === 'default') {
Fliplet.Widget.initializeChildren(this, vm);
}
if (data.dataSourceId) {
loadData = Fliplet.DataSources.connect(data.dataSourceId).then((connection) => {
return connection.find();
});
} else {
loadData = Promise.resolve();
}
loadData.then((result) => {
vm._setData('context', result).then(() => {
if (renderingOption === 'wait') {
Fliplet.Widget.initializeChildren(this, vm);
}
resolve(vm);
});
}).catch((err) => {
console.error(err);
resolve(vm);
});
});
instances.push(container);
}, {
supportsDynamicContext: true
});
});
Fliplet.Container.get = function(name, options) {
options = options || { ts: 10 };
return Fliplet().then(function() {
return Promise.all(instances).then(function(containers) {
var container;
if (typeof name === 'undefined') {
container = containers.length ? containers[0] : undefined;
} else {
containers.some(function(vm) {
if (vm.name === name) {
container = vm;
return true;
}
});
}
if (!container) {
if (options.ts > 5000) {
return Promise.reject('Container not found after ' + Math.ceil(options.ts / 1000) + ' seconds.');
}
// Containers can render over time, so we need to retry later in the process
return new Promise(function(resolve) {
setTimeout(function() {
options.ts = options.ts * 1.5;
Fliplet.Container.get(name, options).then(resolve);
}, options.ts);
});
}
return container;
});
});
};
Fliplet.Container.getAll = function(name) {
return Fliplet().then(function() {
return Promise.all(instances).then(function(containers) {
if (typeof name === 'undefined') {
return containers;
}
return containers.filter(function(form) {
return form.name === name;
});
});
});
};