4
4
* Copyright © 2015 Magento. All rights reserved.
5
5
* See COPYING.txt for license details.
6
6
*/
7
- /*jshint browser:true jquery:true expr:true*/
7
+
8
8
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
+ */
16
110
$ . widget ( 'mage.pageCache' , {
17
111
options : {
18
112
url : '/' ,
@@ -21,26 +115,46 @@ define([
21
115
versionCookieName : 'private_content_version' ,
22
116
handles : [ ]
23
117
} ,
118
+
119
+ /**
120
+ * Creates widget 'mage.pageCache'
121
+ * @private
122
+ */
24
123
_create : function ( ) {
25
- var version = $ . mage . cookies . get ( this . options . versionCookieName ) ;
124
+ var placeholders ,
125
+ version = $ . mage . cookies . get ( this . options . versionCookieName ) ;
126
+
26
127
if ( ! version ) {
27
- return ;
128
+ return ;
28
129
}
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 ) {
31
133
this . _ajax ( placeholders , version ) ;
32
134
}
33
135
} ,
136
+
137
+ /**
138
+ * Parse page for placeholders.
139
+ * @param {Array } elements
140
+ * @returns {Array }
141
+ * @private
142
+ */
34
143
_searchPlaceholders : function ( elements ) {
35
144
var placeholders = [ ] ,
36
- tmp = { } ;
37
- if ( ! elements . length ) {
145
+ tmp = { } ,
146
+ ii ,
147
+ len ,
148
+ el , matches , name ;
149
+
150
+ if ( ! ( elements && elements . length ) ) {
38
151
return placeholders ;
39
152
}
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 ;
44
158
45
159
if ( matches ) {
46
160
name = matches [ 1 ] ;
@@ -50,8 +164,10 @@ define([
50
164
} ;
51
165
} else {
52
166
matches = this . options . patternPlaceholderClose . exec ( el . nodeValue ) ;
167
+
53
168
if ( matches ) {
54
169
name = matches [ 1 ] ;
170
+
55
171
if ( tmp [ name ] ) {
56
172
tmp [ name ] . closeElement = el ;
57
173
placeholders . push ( tmp [ name ] ) ;
@@ -60,44 +176,74 @@ define([
60
176
}
61
177
}
62
178
}
179
+
63
180
return placeholders ;
64
181
} ,
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
+
66
194
var parent = $ ( placeholder . openElement ) . parent ( ) ,
67
195
contents = parent . contents ( ) ,
68
196
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
+
72
205
if ( element == placeholder . openElement ) {
73
206
startReplacing = true ;
74
207
}
208
+
75
209
if ( startReplacing ) {
76
210
$ ( element ) . remove ( ) ;
77
211
} else if ( element . nodeType != 8 ) {
78
212
//due to comment tag doesn't have siblings we try to find it manually
79
213
prevSibling = element ;
80
214
}
215
+
81
216
if ( element == placeholder . closeElement ) {
82
217
break ;
83
218
}
84
219
}
220
+
85
221
if ( prevSibling ) {
86
222
$ ( prevSibling ) . after ( html ) ;
87
223
} else {
88
224
$ ( parent ) . prepend ( html ) ;
89
225
}
226
+
90
227
// trigger event to use mage-data-init attribute
91
228
$ ( parent ) . trigger ( 'contentUpdated' ) ;
92
229
} ,
230
+
231
+ /**
232
+ * AJAX helper
233
+ * @param {Object } placeholders
234
+ * @param {String } version
235
+ * @private
236
+ */
93
237
_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 ) ;
101
247
}
102
248
data . blocks = JSON . stringify ( data . blocks . sort ( ) ) ;
103
249
data . handles = JSON . stringify ( data . handles ) ;
@@ -108,18 +254,54 @@ define([
108
254
cache : true ,
109
255
dataType : 'json' ,
110
256
context : this ,
257
+
258
+ /**
259
+ * Response handler
260
+ * @param {Object } response
261
+ */
111
262
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 ] ) ;
116
270
}
117
- this . _replacePlaceholder ( placeholder , response [ placeholder . name ] ) ;
118
271
}
119
272
}
120
273
} ) ;
121
274
}
122
275
} ) ;
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
+ }
125
307
} ) ;
0 commit comments