Skip to content

Commit 6078d06

Browse files
committed
Merge branch 'MAGETWO-32485' into MAGETWO-31191
2 parents ae883b8 + 680a68f commit 6078d06

File tree

4 files changed

+566
-52
lines changed

4 files changed

+566
-52
lines changed

app/code/Magento/PageCache/view/frontend/requirejs-config.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@
66
var config = {
77
map: {
88
'*': {
9-
formKey: 'Magento_PageCache/js/form-key',
109
pageCache: 'Magento_PageCache/js/page-cache'
1110
}
12-
},
13-
deps: [
14-
'Magento_PageCache/js/form-key',
15-
'Magento_PageCache/js/msg-box'
16-
]
17-
};
11+
}
12+
};

app/code/Magento/PageCache/view/frontend/templates/javascript.phtml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77
<?php /** @var \Magento\PageCache\Block\Javascript $this */ ?>
88
<script>
99
require([
10-
"jquery",
11-
"mage/mage"
12-
], function(jQuery){
13-
14-
//<![CDATA[
15-
jQuery(function () {
16-
jQuery('body').mage('pageCache', <?php echo $this->getScriptOptions(); ?>);
17-
});
18-
//]]>
10+
'jquery',
11+
'pageCache',
12+
'domReady!'
13+
], function($){
14+
'use strict';
1915

16+
$('body').pageCache(<?php echo $this->getScriptOptions(); ?>);
2017
});
2118
</script>

app/code/Magento/PageCache/view/frontend/web/js/page-cache.js

Lines changed: 218 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,109 @@
44
* Copyright © 2015 Magento. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7-
/*jshint browser:true jquery:true expr:true*/
7+
88
define([
9-
"jquery",
10-
"jquery/ui",
11-
"mage/cookies",
12-
"Magento_PageCache/js/comments"
13-
], function($){
14-
"use strict";
15-
9+
'jquery',
10+
'domReady',
11+
'jquery/ui',
12+
'mage/cookies'
13+
], function ($, domReady) {
14+
'use strict';
15+
16+
/**
17+
* Nodes tree to flat list converter
18+
* @returns {Array}
19+
*/
20+
$.fn.comments = function () {
21+
var elements = [];
22+
23+
/**
24+
* @param {jQuery} element - Comment holder
25+
*/
26+
(function lookup(element) {
27+
$(element).contents().each(function (index, el) {
28+
switch (el.nodeType) {
29+
case 1: // ELEMENT_NODE
30+
lookup(el);
31+
break;
32+
33+
case 8: // COMMENT_NODE
34+
elements.push(el);
35+
break;
36+
37+
case 9: // DOCUMENT_NODE
38+
var hostName = window.location.hostname,
39+
iFrameHostName = $('<a>')
40+
.prop('href', element.prop('src'))
41+
.prop('hostname');
42+
43+
if (hostName === iFrameHostName) {
44+
lookup($(el).find('body'));
45+
}
46+
break;
47+
}
48+
});
49+
})(this);
50+
51+
return elements;
52+
};
53+
54+
/**
55+
* MsgBox Widget checks if message box is displayed and sets cookie
56+
*/
57+
$.widget('mage.msgBox', {
58+
options: {
59+
msgBoxCookieName: 'message_box_display',
60+
msgBoxSelector: '.main div.messages'
61+
},
62+
63+
/**
64+
* Creates widget 'mage.msgBox'
65+
* @private
66+
*/
67+
_create: function () {
68+
if ($.mage.cookies.get(this.options.msgBoxCookieName)) {
69+
$.mage.cookies.clear(this.options.msgBoxCookieName);
70+
} else {
71+
$(this.options.msgBoxSelector).hide();
72+
}
73+
}
74+
});
75+
76+
/**
77+
* FormKey Widget - this widget is generating from key, saves it to cookie and
78+
*/
79+
$.widget('mage.formKey', {
80+
options: {
81+
inputSelector: 'input[name="form_key"]',
82+
allowedCharacters: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
83+
length: 16
84+
},
85+
86+
/**
87+
* Creates widget 'mage.formKey'
88+
* @private
89+
*/
90+
_create: function () {
91+
var date,
92+
formKey = $.mage.cookies.get('form_key');
93+
94+
if (!formKey) {
95+
formKey = generateRandomString(this.options.allowedCharacters, this.options.length);
96+
date = new Date();
97+
date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
98+
$.mage.cookies.set('form_key', formKey, {
99+
expires: date,
100+
path: '/'
101+
});
102+
}
103+
$(this.options.inputSelector).val(formKey);
104+
}
105+
});
106+
107+
/**
108+
* PageCache Widget
109+
*/
16110
$.widget('mage.pageCache', {
17111
options: {
18112
url: '/',
@@ -21,26 +115,46 @@ define([
21115
versionCookieName: 'private_content_version',
22116
handles: []
23117
},
118+
119+
/**
120+
* Creates widget 'mage.pageCache'
121+
* @private
122+
*/
24123
_create: function () {
25-
var version = $.mage.cookies.get(this.options.versionCookieName);
124+
var placeholders,
125+
version = $.mage.cookies.get(this.options.versionCookieName);
126+
26127
if (!version) {
27-
return ;
128+
return;
28129
}
29-
var placeholders = this._searchPlaceholders(this.element.comments());
30-
if (placeholders.length) {
130+
placeholders = this._searchPlaceholders(this.element.comments());
131+
132+
if (placeholders && placeholders.length) {
31133
this._ajax(placeholders, version);
32134
}
33135
},
136+
137+
/**
138+
* Parse page for placeholders.
139+
* @param {Array} elements
140+
* @returns {Array}
141+
* @private
142+
*/
34143
_searchPlaceholders: function (elements) {
35144
var placeholders = [],
36-
tmp = {};
37-
if (!elements.length) {
145+
tmp = {},
146+
ii,
147+
len,
148+
el, matches, name;
149+
150+
if (!(elements && elements.length)) {
38151
return placeholders;
39152
}
40-
for (var i = 0; i < elements.length; i++) {
41-
var el = elements[i],
42-
matches = this.options.patternPlaceholderOpen.exec(el.nodeValue),
43-
name = null;
153+
154+
for (ii = 0, len = elements.length; ii < len; ii++) {
155+
el = elements[ii];
156+
matches = this.options.patternPlaceholderOpen.exec(el.nodeValue);
157+
name = null;
44158

45159
if (matches) {
46160
name = matches[1];
@@ -50,8 +164,10 @@ define([
50164
};
51165
} else {
52166
matches = this.options.patternPlaceholderClose.exec(el.nodeValue);
167+
53168
if (matches) {
54169
name = matches[1];
170+
55171
if (tmp[name]) {
56172
tmp[name].closeElement = el;
57173
placeholders.push(tmp[name]);
@@ -60,44 +176,74 @@ define([
60176
}
61177
}
62178
}
179+
63180
return placeholders;
64181
},
65-
_replacePlaceholder: function(placeholder, html) {
182+
183+
/**
184+
* Parse for page and replace placeholders
185+
* @param {Object} placeholder
186+
* @param {Object} html
187+
* @protected
188+
*/
189+
_replacePlaceholder: function (placeholder, html) {
190+
if (!placeholder || !html) {
191+
return;
192+
}
193+
66194
var parent = $(placeholder.openElement).parent(),
67195
contents = parent.contents(),
68196
startReplacing = false,
69-
prevSibling = null;
70-
for (var y = 0; y < contents.length; y++) {
71-
var element = contents[y];
197+
prevSibling = null,
198+
yy,
199+
len,
200+
element;
201+
202+
for (yy = 0, len = contents.length; yy < len; yy++) {
203+
element = contents[yy];
204+
72205
if (element == placeholder.openElement) {
73206
startReplacing = true;
74207
}
208+
75209
if (startReplacing) {
76210
$(element).remove();
77211
} else if (element.nodeType != 8) {
78212
//due to comment tag doesn't have siblings we try to find it manually
79213
prevSibling = element;
80214
}
215+
81216
if (element == placeholder.closeElement) {
82217
break;
83218
}
84219
}
220+
85221
if (prevSibling) {
86222
$(prevSibling).after(html);
87223
} else {
88224
$(parent).prepend(html);
89225
}
226+
90227
// trigger event to use mage-data-init attribute
91228
$(parent).trigger('contentUpdated');
92229
},
230+
231+
/**
232+
* AJAX helper
233+
* @param {Object} placeholders
234+
* @param {String} version
235+
* @private
236+
*/
93237
_ajax: function (placeholders, version) {
94-
var data = {
95-
blocks: [],
96-
handles: this.options.handles,
97-
version: version
98-
};
99-
for (var i = 0; i < placeholders.length; i++) {
100-
data.blocks.push(placeholders[i].name);
238+
var ii,
239+
data = {
240+
blocks: [],
241+
handles: this.options.handles,
242+
version: version
243+
};
244+
245+
for (ii = 0; ii < placeholders.length; ii++) {
246+
data.blocks.push(placeholders[ii].name);
101247
}
102248
data.blocks = JSON.stringify(data.blocks.sort());
103249
data.handles = JSON.stringify(data.handles);
@@ -108,18 +254,54 @@ define([
108254
cache: true,
109255
dataType: 'json',
110256
context: this,
257+
258+
/**
259+
* Response handler
260+
* @param {Object} response
261+
*/
111262
success: function (response) {
112-
for (var i = 0; i < placeholders.length; i++) {
113-
var placeholder = placeholders[i];
114-
if (!response.hasOwnProperty(placeholder.name)) {
115-
continue;
263+
var placeholder, i;
264+
265+
for (i = 0; i < placeholders.length; i++) {
266+
placeholder = placeholders[i];
267+
268+
if (response.hasOwnProperty(placeholder.name)) {
269+
this._replacePlaceholder(placeholder, response[placeholder.name]);
116270
}
117-
this._replacePlaceholder(placeholder, response[placeholder.name]);
118271
}
119272
}
120273
});
121274
}
122275
});
123-
124-
return $.mage.pageCache;
276+
277+
domReady(function () {
278+
$('body')
279+
.pageCache()
280+
.msgBox()
281+
.formKey();
282+
});
283+
284+
return {
285+
'pageCache': $.mage.pageCache,
286+
'formKey': $.mage.formKey,
287+
'msgBox': $.mage.msgBox
288+
};
289+
290+
/**
291+
* Helper. Generate random string
292+
* TODO: Merge with mage/utils
293+
* @param {String} chars - list of symbols
294+
* @param {Number} length - length for need string
295+
* @returns {String}
296+
*/
297+
function generateRandomString(chars, length) {
298+
var result = '';
299+
length = length > 0 && Number.isFinite(length) ? length : 1;
300+
301+
while (length--) {
302+
result += chars[Math.round(Math.random() * (chars.length - 1))];
303+
}
304+
305+
return result;
306+
}
125307
});

0 commit comments

Comments
 (0)