forked from borisbutenko/storefront
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvuse.js
More file actions
508 lines (451 loc) · 14 KB
/
Copy pathvuse.js
File metadata and controls
508 lines (451 loc) · 14 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import merge from 'lodash-es/merge'
import Section from './section'
import VuseBuilder from './components/VuseBuilder.vue'
import VuseRenderer from './components/VuseRenderer.vue'
import styler from './styler'
import mixin from './mixin'
import { cleanDOM } from './util'
import * as _ from 'lodash-es'
let PLUGINS = []
let mixier = {}
const BUILDER_OPTIONS = {
landing: '',
title: '',
intro: true,
sections: [],
plugins: [],
themes: [],
columnsPrefix: {
mobile: 'is-mobile-',
tablet: 'is-tablet-',
desktop: 'is-desktop-',
widescreen: 'is-widescreen-',
ultrawide: 'is-ultrawide-'
}
}
// To tell if it is installed or not
let _Vue = null
class Vuse {
constructor (options) {
this.isEditing = true
this.isRendered = false
this.title = options.title
this.landing = options.landing
this.intro = options.intro
this.sections = options.sections
this.columnsPrefix = options.columnsPrefix
this.themes = options.themes
this.components = {}
this.assets = {
css: options.css,
js: options.js || './../js/cjs.js'
}
this.installPlugins()
}
/**
* Creates and adds a new section to the list of sections.
* @param {*} options
*/
add (options, position) {
if (position !== undefined) {
this.sections.splice(position, 0, new Section(options))
return
}
this.sections.push(new Section(options))
}
/**
* Finds a section with the specified id.
*
* @param {String|Number} id
*/
find (id) {
return this.sections.find(s => s.id === id)
}
/**
* Removes a section with the specified id.
* @param {String|Number} id
*/
remove (section) {
const id = this.sections.findIndex(s => s.id === section.id)
this.sections.splice(id, 1)
section.destroy()
}
/**
* Removes a section with the specified id.
* @param {String|Number} oldIndex
* @param {String|Number} newIndex
*/
sort (oldIndex, newIndex) {
const section = this.sections[oldIndex]
this.sections.splice(oldIndex, 1)
this.sections.splice(newIndex, 0, section)
}
/**
* Constructs a document fragment.
*/
outputFragment () {
const frag = document.createDocumentFragment()
frag.appendChild(document.head.cloneNode(true))
frag.appendChild(this.rootEl.cloneNode(true))
return frag
}
/**
* clears the builder sections.
*/
clear () {
const tempSections = this.sections
this.sections.forEach(section => section.destroy())
this.sections = []
return tempSections
}
/**
* Static helper for components registration pre-installation.
*
* @param {String} name
* @param {Object} definition
*/
static component (name, definition) {
// Just make a plugin that installs a component.
Vuse.use((ctx) => {
ctx.builder.component(name, definition)
})
}
/**
* Acts as a mixin for subcomponents.
* @param {Object} mixinObj
*/
static mix (mixinObj) {
mixier = merge(mixier, mixinObj)
}
/**
* Adds a component section to the builder and augments it with the styler.
* @param {*} name
* @param {*} definition
*/
component (name, definition) {
// reoslve the component name automatically.
if (typeof name === 'object') {
definition = name
name = definition.name
}
// if passed a plain object
if (!definition.extend) {
definition = _Vue.extend(definition)
}
this.components[name] = definition.extend({
directives: { styler: this.styler },
mixins: [this.mixin],
components: mixier.components
})
}
/**
* Installs added plugins.
*/
installPlugins () {
PLUGINS.forEach((ctx) => {
ctx.plugin({ builder: this, Vue: _Vue }, ctx.options)
})
// reset to prevent duplications.
PLUGINS = []
}
static install (Vue, options = {}) {
// already installed
if (_Vue) return
_Vue = Vue
const builder = new Vuse(Object.assign({}, BUILDER_OPTIONS, options))
// configer assets output location
Vue.util.defineReactive(builder, 'sections', builder.sections)
Vue.util.defineReactive(builder, 'isEditing', builder.isEditing)
const extension = {
components: builder.components,
beforeCreate () {
this.$builder = builder
}
}
// register the main components.
Vue.component('VuseBuilder', Vue.extend(VuseBuilder).extend(extension))
Vue.component('VuseRenderer', Vue.extend(VuseRenderer).extend(extension))
}
/**
* The plugin to be installed with the builder. The function receives the installation context which
* contains the builder instance and the Vue prototype.
*
* @param {Function} plugin
* @param {Object} options
*/
static use (plugin, options = {}) {
if (typeof plugin !== 'function') {
return console.warn('Plugins must be a function')
}
// append to the list of to-be installed plugins.
PLUGINS.push({ plugin, options })
}
set (data) {
this.title = data.title !== undefined ? data.title : this.title
if (data.sections && Array.isArray(data.sections)) {
this.sections = data.sections.map(section => {
let component
let sectionData
if (typeof section === 'string') {
component = this.components[section].options
sectionData = {
name: component.name,
group: component.group,
schema: component.schema
}
if (!sectionData.schema) {
sectionData.schema = this.components[sectionData.name].options.$schema
}
} else {
// restore saved data
component = this.components[section.name].options
sectionData = {
name: component.name,
group: component.group,
schema: component.schema,
data: section.data,
isHeader: component.isHeader
}
if (!sectionData.schema) {
sectionData.schema = this.components[sectionData.name].options.$schema
}
}
return new Section(sectionData)
})
}
}
/**
* Outputs a JSON representation of the builder that can be used for rendering with the renderer component.
*/
toJSON () {
return JSON.stringify({
slug: this.landing,
title: this.title,
settings: this.settings,
sections: this.sections.map(({ name, data }) => ({
name,
data: _.mapValues(data, (value, key) => {
// --- coz html element in component's array has circular structure
if (key.match(/^components/)) {
return value.map(c => {
if (c.element.hasOwnProperty('element') && c.element.element instanceof HTMLElement) {
c.element.element = c.element.element.cloneNode(true)
}
return c
})
}
return value
})
}))
})
}
/**
* Previews the created page in a seperate tap/window.
*/
preview () {
const frag = this.outputFragment()
const artboard = frag.querySelector('#artboard')
const printPreview = window.open('about:blank', 'print_preview')
const printDocument = printPreview.document
void cleanDOM(frag)
let { video, title } = this.settings
let customCss = this.getCustomCss()
let script = this.getJsScript()
let bodyStyles = this.getBodyStyles()
let scrollSetup = this.getScrollSetup()
printDocument.open()
printDocument.write(
`<!DOCTYPE html>
<html>
<head>
<title>${title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${window.location.origin}/ptah_sections.css">
<link href="https://fonts.googleapis.com/css?family=Lato|Heebo|PT+Serif|Montserrat:400,500|Roboto:400,700|Cinzel:400,700|IBM+Plex+Sans:400,600|IBM+Plex+Mono:400,600&subset=cyrillic" rel="stylesheet">
${scrollSetup.style}
<style>
${customCss}
</style>
</head>
<body class="b-body_preview" style="${bodyStyles}">
${(video) ? this.getVideoBg(video) : ''}
<div id="main" class="main">
${artboard.innerHTML}
</div>
${this.getCookiesPreview()}
${scrollSetup.setup}
<script>
${script}
</script>
<script src="${window.location.origin + '/js/cjs.js'}"></script>
<body>
</html>`
)
}
getCookiesPreview () {
let { enabled, pdf } = this.settings.cookiesPolicy
if (!enabled || !pdf) {
return ''
}
return `
<div id="cookies-policy" class="cookies-policy">
<span id="cookies-policy-close" class="cookies-policy__close">×</span>
<h2 class="cookies-policy__title">Cookies policy</h2>
<p class="cookies-policy__description">
This website uses cookies.
If you do not wish us to set cookies on your device, please do not use the website.
Please read the <a href="${pdf}" target="_blank">Cookies Policy</a> for more information.
</p>
</div>
`
}
getCustomCss () {
if (!!this.settings.css) {
return this.settings.css
} else {
return ''
}
}
getJsScript () {
if (!!this.settings.script) {
return this.settings.script
} else {
return ''
}
}
/**
* get inline body styles
* @returns {string}
*/
getBodyStyles () {
if (!!this.settings.styles) {
let styles = ''
Object.keys(this.settings.styles).forEach((i) => {
if (this.settings.styles[i] && i !== 'backgroundImage')
styles = styles + `${_.kebabCase(i)}: ${this.settings.styles[i]};`
if (this.settings.styles[i] && i === 'backgroundImage')
styles = styles + `${_.kebabCase(i)}: url(${this.settings.styles[i]});`
})
if (this.settings.fullPageScroll === 'yes') {
styles += `overflow:hidden!important;`
}
return styles
} else {
return ''
}
}
getVideoBg (video) {
return `<video id="video_bg" class="${this.settings.videoPosition}" autoplay="true" loop="loop" muted="muted">
<source src="${video}" type="video/mp4"></source>
</video>`
}
getScrollSetup () {
let scroll = {
style: '',
setup: ''
}
if (this.settings.fullPageScroll === 'yes') {
scroll.style = `
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="${window.location.origin + '/js/onepage-scroll.min.js'}"></script>
<link href="${window.location.origin + '/css/onepage-scroll.css'}" rel="stylesheet">
`
scroll.setup = `
<script>
function detectMobile () {
return $(window).width() < 500 ? true : false;
}
if (!detectMobile()) {
$(".main").onepage_scroll();
}
$(window).resize(function() {
let className = 'disabled-onepage-scroll';
let classWrapName = 'onepage-wrapper';
if (detectMobile()) {
if ($(".main").data("onepage_scroll")){
$(".main").disable();
$(".main").data("onepage_scroll").destroy();
if ($(".main").hasClass(classWrapName)) $(".main").removeClass(classWrapName);
}
$("body").addClass(className);
$("body").css('overflow', '')
} else {
if (!$(".main").data("onepage_scroll")){
$(".main").onepage_scroll();
}
$("body").css('overflow', 'hidden');
if ($("body").hasClass(className)) $("body").removeClass(className);
}
});
</script>`
}
return scroll
}
gtmSetup () {
let setup = {
head: '',
body: ''
}
if (this.settings.gtmId.length) {
setup.head = `<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','${this.settings.gtmId}');</script>
<!-- End Google Tag Manager -->`
setup.body =`<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=${this.settings.gtmId}"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->`
}
return setup
}
getManifest () {
let description = _.find(this.settings.ogTags, o => o.property === 'og:description') || { content: '' }
let manifest = {
name: this.settings.title,
description: description.content,
icons: [{
src: this.settings.favicon,
sizes: '64x64'
}],
start_url: '.'
}
return JSON.stringify(manifest)
}
/**
* Return open graph meta tags
* @param tags
* @returns {string}
*/
getOgMetaTags (tags) {
let og = ''
tags.forEach((i) => {
og = og + `<meta property="${i.property}" content="${i.content}">`
})
return og
}
/**
* Exports the builder instance to a specified output. default is json.
*
* @param {String} method
*/
export (method = 'json') {
if (method === 'pwa' || method === 'zip') {
if (typeof this.download === 'function') {
return this.download(this.assets)
}
return console.warn('You do not have the zip plugin installed.')
}
if (method === 'preview') {
return this.preview()
}
return this.toJSON()
}
};
// use the plugin API to add the styler and mixin functionalities.
Vuse.use(styler)
Vuse.use(mixin)
export default Vuse