3
3
*
4
4
* @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
5
5
*/
6
- /*jshint browser:true jquery:true expr:true*/
6
+
7
7
define ( [
8
- "jquery" ,
9
- "jquery/ui" ,
10
- "mage/cookies" ,
11
- "Magento_PageCache/js/comments"
12
- ] , function ( $ ) {
13
- "use strict" ;
14
-
8
+ 'jquery' ,
9
+ 'domReady' ,
10
+ 'jquery/ui' ,
11
+ 'mage/cookies'
12
+ ] , function ( $ , domReady ) {
13
+ 'use strict' ;
14
+
15
+ /**
16
+ * Nodes tree to flat list converter
17
+ * @returns {Array }
18
+ */
19
+ $ . fn . comments = function ( ) {
20
+ var elements = [ ] ;
21
+
22
+ /**
23
+ * @param {jQuery } element - Comment holder
24
+ */
25
+ ( function lookup ( element ) {
26
+ if ( element . is ( 'iframe' ) ) {
27
+ var hostName = window . location . hostname ,
28
+ iFrameHostName = $ ( '<a>' ) . prop ( 'href' , element . prop ( 'src' ) ) . prop ( 'hostname' ) ;
29
+
30
+ if ( hostName != iFrameHostName ) {
31
+ return ;
32
+ }
33
+ }
34
+ element . contents ( ) . each ( function ( i , el ) {
35
+ if ( el . nodeType == 8 ) {
36
+ elements . push ( el ) ;
37
+ } else if ( el . nodeType == 1 ) {
38
+ lookup ( $ ( el ) ) ;
39
+ }
40
+ } ) ;
41
+ } ) ( this ) ;
42
+
43
+ return elements ;
44
+ } ;
45
+
46
+ /**
47
+ * MsgBox Widget checks if message box is displayed and sets cookie
48
+ */
49
+ $ . widget ( 'mage.msgBox' , {
50
+ options : {
51
+ msgBoxCookieName : 'message_box_display' ,
52
+ msgBoxSelector : '.main div.messages'
53
+ } ,
54
+
55
+ /**
56
+ * Creates widget 'mage.msgBox'
57
+ * @private
58
+ */
59
+ _create : function ( ) {
60
+ if ( $ . mage . cookies . get ( this . options . msgBoxCookieName ) ) {
61
+ $ . mage . cookies . set ( this . options . msgBoxCookieName , null , {
62
+ expires : new Date ( ) ,
63
+ path : '/'
64
+ } ) ;
65
+ } else {
66
+ $ ( this . options . msgBoxSelector ) . hide ( ) ;
67
+ }
68
+ }
69
+ } ) ;
70
+
71
+ /**
72
+ * FormKey Widget - this widget is generating from key, saves it to cookie and
73
+ */
74
+ $ . widget ( 'mage.formKey' , {
75
+ options : {
76
+ inputSelector : 'input[name="form_key"]' ,
77
+ allowedCharacters : '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ,
78
+ length : 16
79
+ } ,
80
+
81
+ /**
82
+ * Creates widget 'mage.formKey'
83
+ * @private
84
+ */
85
+ _create : function ( ) {
86
+ var date ,
87
+ formKey = $ . mage . cookies . get ( 'form_key' ) ;
88
+
89
+ if ( ! formKey ) {
90
+ formKey = generateRandomString ( this . options . allowedCharacters , this . options . length ) ;
91
+ date = new Date ( ) ;
92
+ date . setTime ( date . getTime ( ) + ( 365 * 24 * 60 * 60 * 1000 ) ) ;
93
+ $ . mage . cookies . set ( 'form_key' , formKey , {
94
+ expires : date ,
95
+ path : '/'
96
+ } ) ;
97
+ }
98
+ $ ( this . options . inputSelector ) . val ( formKey ) ;
99
+ }
100
+ } ) ;
101
+
102
+ /**
103
+ * PageCache Widget
104
+ */
15
105
$ . widget ( 'mage.pageCache' , {
16
106
options : {
17
107
url : '/' ,
@@ -20,26 +110,45 @@ define([
20
110
versionCookieName : 'private_content_version' ,
21
111
handles : [ ]
22
112
} ,
113
+
114
+ /**
115
+ * Creates widget 'mage.pageCache'
116
+ * @private
117
+ */
23
118
_create : function ( ) {
24
- var version = $ . mage . cookies . get ( this . options . versionCookieName ) ;
119
+ var placeholders ,
120
+ version = $ . mage . cookies . get ( this . options . versionCookieName ) ;
121
+
25
122
if ( ! version ) {
26
- return ;
123
+ return ;
27
124
}
28
- var placeholders = this . _searchPlaceholders ( this . element . comments ( ) ) ;
125
+ placeholders = this . _searchPlaceholders ( this . element . comments ( ) ) ;
126
+
29
127
if ( placeholders . length ) {
30
128
this . _ajax ( placeholders , version ) ;
31
129
}
32
130
} ,
131
+
132
+ /**
133
+ * Parse page for placeholders.
134
+ * @param {Array } elements
135
+ * @returns {Array }
136
+ * @private
137
+ */
33
138
_searchPlaceholders : function ( elements ) {
34
139
var placeholders = [ ] ,
35
- tmp = { } ;
140
+ tmp = { } ,
141
+ ii ,
142
+ el , matches , name ;
143
+
36
144
if ( ! elements . length ) {
37
145
return placeholders ;
38
146
}
39
- for ( var i = 0 ; i < elements . length ; i ++ ) {
40
- var el = elements [ i ] ,
41
- matches = this . options . patternPlaceholderOpen . exec ( el . nodeValue ) ,
42
- name = null ;
147
+
148
+ for ( ii = 0 ; ii < elements . length ; ii ++ ) {
149
+ el = elements [ ii ] ;
150
+ matches = this . options . patternPlaceholderOpen . exec ( el . nodeValue ) ;
151
+ name = null ;
43
152
44
153
if ( matches ) {
45
154
name = matches [ 1 ] ;
@@ -49,8 +158,10 @@ define([
49
158
} ;
50
159
} else {
51
160
matches = this . options . patternPlaceholderClose . exec ( el . nodeValue ) ;
161
+
52
162
if ( matches ) {
53
163
name = matches [ 1 ] ;
164
+
54
165
if ( tmp [ name ] ) {
55
166
tmp [ name ] . closeElement = el ;
56
167
placeholders . push ( tmp [ name ] ) ;
@@ -59,44 +170,69 @@ define([
59
170
}
60
171
}
61
172
}
173
+
62
174
return placeholders ;
63
175
} ,
64
- _replacePlaceholder : function ( placeholder , html ) {
176
+
177
+ /**
178
+ * Parse for page and replace placeholders
179
+ * @param {Object } placeholder
180
+ * @param {Object } html
181
+ * @protected
182
+ */
183
+ _replacePlaceholder : function ( placeholder , html ) {
65
184
var parent = $ ( placeholder . openElement ) . parent ( ) ,
66
185
contents = parent . contents ( ) ,
67
186
startReplacing = false ,
68
- prevSibling = null ;
69
- for ( var y = 0 ; y < contents . length ; y ++ ) {
70
- var element = contents [ y ] ;
187
+ prevSibling = null ,
188
+ yy ,
189
+ element ;
190
+
191
+ for ( yy = 0 ; yy < contents . length ; yy ++ ) {
192
+ element = contents [ yy ] ;
193
+
71
194
if ( element == placeholder . openElement ) {
72
195
startReplacing = true ;
73
196
}
197
+
74
198
if ( startReplacing ) {
75
199
$ ( element ) . remove ( ) ;
76
200
} else if ( element . nodeType != 8 ) {
77
201
//due to comment tag doesn't have siblings we try to find it manually
78
202
prevSibling = element ;
79
203
}
204
+
80
205
if ( element == placeholder . closeElement ) {
81
206
break ;
82
207
}
83
208
}
209
+
84
210
if ( prevSibling ) {
85
211
$ ( prevSibling ) . after ( html ) ;
86
212
} else {
87
213
$ ( parent ) . prepend ( html ) ;
88
214
}
215
+
89
216
// trigger event to use mage-data-init attribute
90
217
$ ( parent ) . trigger ( 'contentUpdated' ) ;
91
218
} ,
219
+
220
+ /**
221
+ * AJAX helper
222
+ * @param {Object } placeholders
223
+ * @param {String } version
224
+ * @private
225
+ */
92
226
_ajax : function ( placeholders , version ) {
93
- var data = {
94
- blocks : [ ] ,
95
- handles : this . options . handles ,
96
- version : version
97
- } ;
98
- for ( var i = 0 ; i < placeholders . length ; i ++ ) {
99
- data . blocks . push ( placeholders [ i ] . name ) ;
227
+ var ii ,
228
+ data = {
229
+ blocks : [ ] ,
230
+ handles : this . options . handles ,
231
+ version : version
232
+ } ;
233
+
234
+ for ( ii = 0 ; ii < placeholders . length ; ii ++ ) {
235
+ data . blocks . push ( placeholders [ ii ] . name ) ;
100
236
}
101
237
data . blocks = JSON . stringify ( data . blocks . sort ( ) ) ;
102
238
data . handles = JSON . stringify ( data . handles ) ;
@@ -107,18 +243,53 @@ define([
107
243
cache : true ,
108
244
dataType : 'json' ,
109
245
context : this ,
246
+
247
+ /**
248
+ * Response handler
249
+ * @param {Object } response
250
+ */
110
251
success : function ( response ) {
111
- for ( var i = 0 ; i < placeholders . length ; i ++ ) {
112
- var placeholder = placeholders [ i ] ;
113
- if ( ! response . hasOwnProperty ( placeholder . name ) ) {
114
- continue ;
252
+ var placeholder , i ;
253
+
254
+ for ( i = 0 ; i < placeholders . length ; i ++ ) {
255
+ placeholder = placeholders [ i ] ;
256
+
257
+ if ( response . hasOwnProperty ( placeholder . name ) ) {
258
+ this . _replacePlaceholder ( placeholder , response [ placeholder . name ] ) ;
115
259
}
116
- this . _replacePlaceholder ( placeholder , response [ placeholder . name ] ) ;
117
260
}
118
261
}
119
262
} ) ;
120
263
}
121
264
} ) ;
122
-
123
- return $ . mage . pageCache ;
124
- } ) ;
265
+
266
+ domReady ( function ( ) {
267
+ $ ( 'body' )
268
+ . pageCache ( )
269
+ . msgBox ( )
270
+ . formKey ( ) ;
271
+ } ) ;
272
+
273
+ return {
274
+ 'pageCache' : $ . mage . pageCache ,
275
+ 'formKey' : $ . mage . formKey ,
276
+ 'msgBox' : $ . mage . msgBox
277
+ } ;
278
+
279
+ /**
280
+ * Helper. Generate random string
281
+ * TODO: Merge with mage/utils
282
+ * @param {String } chars - list of symbols
283
+ * @param {Number } length - length for need string
284
+ * @returns {String }
285
+ */
286
+ function generateRandomString ( chars , length ) {
287
+ var result = '' ;
288
+
289
+ while ( length -- ) {
290
+ result += chars [ Math . round ( Math . random ( ) * ( chars . length - 1 ) ) ] ;
291
+ }
292
+
293
+ return result ;
294
+ }
295
+ } ) ;
0 commit comments