widget-compiler-fns 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- package/index.js +107 -0
- package/package.json +21 -0
package/index.js
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
"use strict";
|
2
|
+
var r = null,
|
3
|
+
i = require('lodash.defaults'),
|
4
|
+
o = require('lodash.assign'),
|
5
|
+
u = require('client-template-compiler/lib/compiler'),
|
6
|
+
c = require('client-template-compiler/lib/template'),
|
7
|
+
a = "var $ = w.optimizely.get('jquery');",
|
8
|
+
l = ["widget", "editor_panel"];
|
9
|
+
function getWidgetDefaultsString(n) {
|
10
|
+
return JSON.stringify(n.form_schema.map((function(n) {
|
11
|
+
return {
|
12
|
+
name: n.name,
|
13
|
+
default_value: n.default_value
|
14
|
+
}
|
15
|
+
})))
|
16
|
+
}
|
17
|
+
function compileSingleListener(n) {
|
18
|
+
var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
|
19
|
+
o(t, {
|
20
|
+
render_template: !0,
|
21
|
+
include_jquery: !0
|
22
|
+
});
|
23
|
+
var e = "",
|
24
|
+
r = getWidgetDefaultsString(n),
|
25
|
+
i = "function(widg) {\n var i = 0;\n var field;\n for (; i < widg.$fieldDefaults.length; ++i) {\n field = widg.$fieldDefaults[i];\n if (!widg.hasOwnProperty(field.name)) {\n widg[field.name] = field.default_value;\n }\n }\n }";
|
26
|
+
if ("widget" === n.plugin_type) {
|
27
|
+
var u = getAppendCSSCode(n),
|
28
|
+
a = getDestroyCSSCode(n),
|
29
|
+
l = c.compile(n.options.html, {
|
30
|
+
asString: !0
|
31
|
+
});
|
32
|
+
e = "\nPluginManager.registerWidget({\n widgetId: '" + n.plugin_id + "',\n showFn: function(event) {\n var $ = window.optimizely.get('jquery');\n var widget = event.data.config;\n var _template = new Hogan(" + l + ')\n widget.$id = "' + n.plugin_id + '";\n widget.$instance = event.data.id;\n widget.$render = _template.render.bind(_template)\n widget.$fieldDefaults = ' + r + ";\n (" + i + ")(widget);\n widget.$html = _template.render({ widget: widget, extension: widget })\n var extension = widget;\n " + u + "\n " + n.options.apply_js + "\n },\n hideFn: function(event) {\n var $ = window.optimizely.get('jquery');\n var widget = event.data.config;\n widget.$id = \"" + n.plugin_id + '";\n widget.$instance = event.data.id;\n widget.$fieldDefaults = ' + r + ";\n (" + i + ")(widget);\n var extension = widget;\n " + a + "\n " + n.options.undo_js + "\n },\n });"
|
33
|
+
} else "event" === n.plugin_type && (e = "PluginManager.registerEventImplementation('" + n.plugin_id + "', {\n attach: function(event) {\n var $ = window.optimizely.get('jquery');\n event.config = event.config || {};\n event.config.$fieldDefaults = " + r + ";\n (" + i + ")(event.config);\n " + n.options.attach_js + "\n },\n detach: function(event) {\n var $ = window.optimizely.get('jquery');\n event.config = event.config || {};\n event.config.$fieldDefaults = " + r + ";\n (" + i + ")(event.config);\n " + n.options.detach_js + "\n },\n})");
|
34
|
+
return e
|
35
|
+
}
|
36
|
+
function getAppendCSSCode(n) {
|
37
|
+
return n.options.css && n.options.css.length > 0 ? "widget._styleTag = document.createElement('style');\nwidget._styleTag.id = '" + ("widget-css-" + n.plugin_id) + "';\nwidget._styleTag.innerHTML = '" + n.options.css.replace(/\n/g, "").replace(/'/g, '"') + "';\ndocument.getElementsByTagName('head')[0].appendChild(widget._styleTag);" : ""
|
38
|
+
}
|
39
|
+
function getDestroyCSSCode(n) {
|
40
|
+
var t = n.options.css && n.options.css.length > 0,
|
41
|
+
e = "widget-css-" + n.plugin_id;
|
42
|
+
return t ? "widget._styleTag = document.getElementById('" + e + "');\nif (widget._styleTag) widget._styleTag.parentNode.removeChild(widget._styleTag);" : ""
|
43
|
+
}
|
44
|
+
t.compilePluginListeners = function(n) {
|
45
|
+
var e = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {},
|
46
|
+
r = n.some((function(n) {
|
47
|
+
return l.indexOf(n.plugin_type) > -1
|
48
|
+
}));
|
49
|
+
o(e, {
|
50
|
+
minify: !1,
|
51
|
+
include_jquery: !0,
|
52
|
+
include_template_runtime: r
|
53
|
+
});
|
54
|
+
var u = n.map((function(n) {
|
55
|
+
return compileSingleListener(n, e)
|
56
|
+
})).join("\n");
|
57
|
+
return t.__wrapWidgetCode(u, i({}, e, {
|
58
|
+
for_plugin_manager: !0
|
59
|
+
}))
|
60
|
+
}, t.compilePreviewCreateCode = function(n, e) {
|
61
|
+
var i = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : {};
|
62
|
+
if ("widget" === n.plugin_type) {
|
63
|
+
var o = getAppendCSSCode(n),
|
64
|
+
u = c.compile(n.options.html, {
|
65
|
+
asString: !0
|
66
|
+
}),
|
67
|
+
l = JSON.stringify(e),
|
68
|
+
f = a + "\nvar widget = " + l + "\nvar _template = new Hogan(" + u + ')\nwidget.$id = "' + n.plugin_id + "\";\nwidget.$instance = 'preview_create';\nwidget.$render = _template.render.bind(_template)\nwidget.$html = _template.render({ widget: widget, extension: widget });\nvar extension = widget;\n" + o + "\n" + n.options.apply_js;
|
69
|
+
return t.__wrapWidgetCode(f, i)
|
70
|
+
}
|
71
|
+
if ("editor_panel" === n.plugin_type) {
|
72
|
+
l = JSON.stringify(e), f = a + "\nvar widget = " + l + "\nvar _template = new Hogan(" + u + ')\nwidget.$id = "' + n.plugin_id + "\";\nwidget.$instance = 'preview_create';\nvar extension = widget;\n" + n.options.apply_js;
|
73
|
+
return t.__wrapWidgetCode(f, i)
|
74
|
+
}
|
75
|
+
if ("event" === n.plugin_type) {
|
76
|
+
l = JSON.stringify(e), f = a + "\nvar event = {\n id: 'previewId',\n viewId: 'previewViewId',\n name: 'previewName',\n apiName: 'previewApiName',\n category: 'other',\n eventType: '" + n.plugin_id + "',\n eventFilter: null,\n config: " + l + "\n};\n" + n.options.attach_js;
|
77
|
+
return t.__wrapWidgetCode(f, r.extend({}, i, {
|
78
|
+
include_template_runtime: !1
|
79
|
+
}))
|
80
|
+
}
|
81
|
+
return ""
|
82
|
+
}, t.compilePreviewDestroyCode = function(n, e) {
|
83
|
+
var i = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : {};
|
84
|
+
if ("widget" === n.plugin_type) {
|
85
|
+
var o = getDestroyCSSCode(n),
|
86
|
+
u = JSON.stringify(e),
|
87
|
+
c = a + "\nvar widget = " + u + '\nwidget.$id = "' + n.plugin_id + "\";\nwidget.$instance = 'preview_create';\nvar extension = widget;\n" + o + "\n" + n.options.undo_js;
|
88
|
+
return t.__wrapWidgetCode(c, i)
|
89
|
+
}
|
90
|
+
if ("event" === n.plugin_type) {
|
91
|
+
u = JSON.stringify(e), c = a + "\nvar event = {\n id: 'previewId',\n viewId: 'previewViewId',\n name: 'previewName',\n apiName: 'previewApiName',\n category: 'other',\n eventType: '" + n.plugin_id + "',\n eventFilter: null,\n config: " + u + "\n};\n" + n.options.detach_js;
|
92
|
+
return t.__wrapWidgetCode(c, r.extend({}, i, {
|
93
|
+
include_template_runtime: !1
|
94
|
+
}))
|
95
|
+
}
|
96
|
+
}, t.compileTemplate = function(n, t) {
|
97
|
+
return c.compile(n).render(t)
|
98
|
+
}, t.__wrapWidgetCode = function(n) {
|
99
|
+
var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
|
100
|
+
o(t, {
|
101
|
+
include_jquery: !0,
|
102
|
+
include_template_runtime: !0,
|
103
|
+
for_plugin_manager: !1
|
104
|
+
});
|
105
|
+
var e = t.include_template_runtime ? u : "";
|
106
|
+
return t.for_plugin_manager ? "function(PluginManager) {\n " + e + "\n " + n + "\n}" : "(function(w) {\nw.optimizely = w.optimizely || [];\n" + e + "\n" + n + "\n})(window)"
|
107
|
+
}
|
package/package.json
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
{
|
2
|
+
"name": "widget-compiler-fns",
|
3
|
+
"version": "0.0.12",
|
4
|
+
"private": false,
|
5
|
+
"description": "",
|
6
|
+
"license": "MIT",
|
7
|
+
"author": "hbrz",
|
8
|
+
"main": "lib/index.js",
|
9
|
+
"scripts": {
|
10
|
+
"build": "npm run mkdir && node build.js",
|
11
|
+
"mkdir": "node build.js",
|
12
|
+
"prepublishOnly": "npm run build",
|
13
|
+
"test": "exit 0"
|
14
|
+
},
|
15
|
+
"dependencies": {
|
16
|
+
"lodash": "^4.17.21"
|
17
|
+
},
|
18
|
+
"publishConfig": {
|
19
|
+
"access": "public"
|
20
|
+
}
|
21
|
+
}
|