summaryrefslogtreecommitdiff
blob: 7b3cd94fa04af7d30e82026a120f6686c5225084 (plain)
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
/*****************************************************************************************************/
/*                                                                                                   */
/*                                    	'EDIT COVER'					                             */
/*                                                                                                   */
/*****************************************************************************************************/

function WMP_EDIT_COVER(){

    var JSObject = this;

    this.type = "wmp_editcover";

    this.form;
    this.DOMDoc;

    this.send_btn;
    this.deletingCover = false;
	
	
	/*****************************************************************************************************/
    /*                                                                                                   */
    /*                              FUNCTION INIT - called from WMPJSInterface                           */
    /*                                                                                                   */
    /*****************************************************************************************************/
    this.init = function(){

	   // save a reference to WMPJSInterface Object
        WMPJSInterface = window.parent.WMPJSInterface;

        // save a reference to "SEND" Button
        this.send_btn = jQuery('#'+this.type+'_send_btn',this.DOMDoc).get(0);
       
        // save a reference to the FORM and remove the default submit action
        this.form = this.DOMDoc.getElementById(this.type+'_form');

        // add actions to send, cancel, ... buttons
        this.addButtonsActions();

        if (this.form == null){
            return;
        }
        
        // custom validation for FORM's inputs
        this.initValidation();
    }




    /*****************************************************************************************************/
    /*                                                                                                   */
    /*                                  FUNCTION INIT VALIDATION                                         */
    /*                                                                                                   */
    /*****************************************************************************************************/
    this.initValidation = function(){

        /*******************************************************/
		/*                    VALIDATION RULES                 */
		/*******************************************************/
		
        // this is the object that handles the form validations
	    this.validator = jQuery("#"+this.form.id, this.DOMDoc).validate({
	
            rules: {
                wmp_editcover_cover : {
    				accept		: "png|jpg|jpeg|gif"
    			}
            },
            
            messages: {
                wmp_editcover_cover : {
    				accept		: "Please add a png, gif or jpeg image."
    			}
            },
            
	        // the errorPlacement has to take the table layout into account
	        // all the errors must be handled by containers/divs with custom ids: Ex. "error_fullname_container"
	        errorPlacement: function(error, element) {
	            var split_name = element[0].id.split("_");
                var id = (split_name.length > 1) ? split_name[ split_name.length - 1] : split_name[0];
                var errorContainer = jQuery("#error_"+id+"_container",JSObject.DOMDoc);
	            error.appendTo( errorContainer );
	        },
            
            errorElement: 'span'
	    });
        
        /*******************************************************/
		/*                     INPUT "COVER"                   */
		/*******************************************************/
        
		// this is a hack for chrome and safari
		var $Cover = jQuery('#'+this.type+'_cover',this.DOMDoc);
        var $RemoveCoverLink = jQuery('#'+this.type+'_cover_removenew',this.DOMDoc);
        
		$Cover.bind("change",function(){ 
			$Cover.focus();
			$Cover.blur();		
            if (this.files[0])
                jQuery('#fakefilecover').val( this.files[0].name );
            $RemoveCoverLink.css("display", "block");
		});
        
        $RemoveCoverLink.bind("click",function(){ 
			jQuery('#fakefilecover').val(""); 
            
            $Cover.val("");
            jQuery(JSObject.form).validate().element( "#" + JSObject.type + "_cover" );
            
            $RemoveCoverLink.css("display", "none");
		});
        
		/*******************************************************/
		/*                     EDIT ICON LINK                  */
		/*******************************************************/
		
		// attach click functions for the edit cover link
		var $EditCoverLink = jQuery('.'+this.type+'_changecover',this.DOMDoc);
		if ($EditCoverLink.length > 0){
		
			$EditCoverLink.click(
				function(){
					
					// if the file field is hidden
					if (jQuery('.'+JSObject.type+'_uploadcover',JSObject.DOMDoc).css("display") == 'none') {
						
						// reset file field value
                        $Cover.val("");
                        jQuery(JSObject.form).validate().element( "#" + JSObject.type + "_cover" );
						jQuery('#fakefilecover').val("")
                        
                        // hide the 'remove new cover' link
                        $RemoveCoverLink.css("display", "none");
						
						// show upload cover field
						jQuery('.'+JSObject.type+'_uploadcover',JSObject.DOMDoc).show();
					
						// show cancel button
						if (jQuery('#'+JSObject.type+'_currentcover',JSObject.DOMDoc).css("background-image") != "none")
							jQuery('.'+JSObject.type+'_changecover_cancel',JSObject.DOMDoc).show();
						
						// hide current cover
						if (jQuery('.'+JSObject.type+'_covercontainer',JSObject.DOMDoc).css("display") == 'block')
							jQuery('.'+JSObject.type+'_covercontainer',JSObject.DOMDoc).hide();
					}
				}
			);
		}
		
		/*******************************************************/
		/*                 CANCEL EDIT ICON LINK               */
		/*******************************************************/
		
		// attach click functions for the cancel edit cover link
		var $CancelEditCoverLink = jQuery('.'+this.type+'_changecover_cancel a',this.DOMDoc);
		if ($CancelEditCoverLink.length > 0){
		
			$CancelEditCoverLink.click(
				function(){
					
					// if the file field is visible
					if (jQuery('.'+JSObject.type+'_uploadcover',JSObject.DOMDoc).css("display") == 'block') {
						
						// reset file field value
						$Cover.val("");
                        jQuery(JSObject.form).validate().element( "#" + JSObject.type + "_cover" );
						jQuery('#fakefilecover').val("")
						
						// hide upload cover field
						jQuery('.'+JSObject.type+'_uploadcover',JSObject.DOMDoc).hide();
					
						// hide cancel button
						jQuery(this).parent().hide();
						
						// display current logo (if it exists)
						if (jQuery('.'+JSObject.type+'_covercontainer',JSObject.DOMDoc).css("display") == 'none' &&
							jQuery('#'+JSObject.type+'_currentcover',JSObject.DOMDoc).css("background-image") != "none")
							
							jQuery('.'+JSObject.type+'_covercontainer',JSObject.DOMDoc).show();
					}
				}
			);
		}
		
		/*******************************************************/
		/*               DELETE ICON LINK               	   */
		/*******************************************************/
		
		// attach click functions for the delete cover link
		var $DeleteCoverLink = jQuery('.'+this.type+'_deletecover',this.DOMDoc);
		
		if ($DeleteCoverLink.length > 0){
		
			var href = $DeleteCoverLink.get(0).href;
			$DeleteCoverLink.get(0).href = "javascript:void(0);"
			
			$DeleteCoverLink.click(
				function(){		
				
					var isConfirmed = confirm("Are you sure you want to remove the app cover?");
	
					if (isConfirmed) {
						
                        jQuery.get(
                            ajaxurl, 
                            {
                                'action': 'wmp_theme_editimages',
                                'type':   'delete',
                                'source': 'cover'
                            }, 
                            function(responseJSON){
 
                                var JSON = eval ("("+responseJSON+")");
                                var response = Boolean(Number(String(JSON.status)));
        
								JSObject.deletingCover = false;
								
								if (response == true) {
								
									// remove image url
	  								jQuery('#'+JSObject.type+'_currentcover',JSObject.DOMDoc).css("background-image", "none");
	  								
	  								// trigger the display of the upload field
	  								$EditCoverLink.trigger("click");
	  								
									// success message								
									var message = 'The app cover has been removed.';
									WMPJSInterface.Loader.display({message: message});
	
								} else {
								
									// error message
									var message = 'There was an error. Please try again in few seconds.';
									WMPJSInterface.Loader.display({message: message});
									
								}
							}
						)
					}
				}
			);
		}
    }
    
    /*****************************************************************************************************/
    /*                                                                                                   */
    /*                                  FUNCTION DISPLAY NEW IMAGE                                       */
    /*                                                                                                   */
    /*****************************************************************************************************/
    this.displayImage = function(type, path){

       		// reset file field value
			jQuery('#'+JSObject.type+'_cover',JSObject.DOMDoc).val("");
			jQuery('#fakefilecover').val("")
            			
			// hide upload cover field
			jQuery('.'+JSObject.type+'_uploadcover',JSObject.DOMDoc).hide();
            
			// hide cancel button
			jQuery('.'+JSObject.type+'_changecover_cancel',JSObject.DOMDoc).hide();
            
       		// add new path in the background attribute
			jQuery('#'+JSObject.type+'_currentcover',JSObject.DOMDoc).css("background-image", "url("+path+")");

			// display image container
			jQuery('.'+JSObject.type+'_covercontainer',JSObject.DOMDoc).css("display", "block");
			
       

    }

	/*****************************************************************************************************/
    /*                                                                                                   */
    /*                                  FUNCTION ADD BUTTONS ACTIONS                                     */
    /*                                                                                                   */
    /*****************************************************************************************************/
    this.addButtonsActions = function(){

        /*******************************************************/
        /*                     SEND "BUTTON"                   */
        /*******************************************************/
        jQuery(this.send_btn).unbind("click");
        jQuery(this.send_btn).bind("click",function(){
            JSObject.disableButton(this);
            JSObject.validate();
        })
        JSObject.enableButton(this.send_btn);

    }


    /*****************************************************************************************************/
    /*                                                                                                   */
    /*                                 FUNCTION ENABLE BUTTON                                            */
    /*                                                                                                   */
    /*****************************************************************************************************/
    this.enableButton = function(btn){
        jQuery(btn).css('cursor','pointer');
        jQuery(btn).animate({opacity:1},100);
    }


    /*****************************************************************************************************/
    /*                                                                                                   */
    /*                                 FUNCTION DISABLE BUTTON                                           */
    /*                                                                                                   */
    /*****************************************************************************************************/
    this.disableButton = function(btn){
        jQuery(btn).unbind("click");
        jQuery(btn).animate({opacity:0.4},100);
        jQuery(btn).css('cursor','default');
    }


    /*****************************************************************************************************/
    /*                                                                                                   */
    /*                                 FUNCTION SCROLL TO FIRST ERROR                                    */
    /*                                                                                                   */
    /*****************************************************************************************************/
    this.scrollToError = function(yCoord){

        var container = jQuery('html,body', JSObject.DOMDoc);
        var scrollTop = parseInt(jQuery('html,body').scrollTop()) || parseInt(jQuery('body').scrollTop());
        var containerHeight = container.get(0).clientHeight;
        var top = parseInt(container.offset().top);

        if (yCoord < scrollTop){
            jQuery(container).animate({scrollTop: yCoord-20 }, 1000);
        }
        else if (yCoord > scrollTop + containerHeight){
            jQuery(container).animate({scrollTop: scrollTop + containerHeight }, 1000);
        }
    }
    

    /*****************************************************************************************************/
    /*                                                                                                   */
    /*                                 FUNCTION VALIDATE INFORMATION                                     */
    /*                                                                                                   */
    /*****************************************************************************************************/
    this.validate = function(){
        jQuery(this.form).validate().form();

        // y coordinates of error inputs
        var arr_errorsYCoord = [];

        // find the y coordinate for the errors
        for (var name in this.validator.invalid){
            var $input = jQuery(this.form[name]);
            arr_errorsYCoord.push($input.offset().top);
        }

        // if there are no errors from syntax point of view, then send data
        if (arr_errorsYCoord.length == 0){
            this.sendData();
        }
        //move container(div) scroll to the first error
        else{
            arr_errorsYCoord.sort(function(a, b){ return (a-b); });
            JSObject.scrollToError(arr_errorsYCoord[0]);

            // add actions to send, cancel, ... buttons. At this moment the buttons are disabled.
            JSObject.addButtonsActions();
        }
    }


    /*****************************************************************************************************/
	/*                                                                                                   */
	/*                       FUNCTION SUBMIT FORM  THROUGH an IFRAME as target                           */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.submitForm = function(){
		return WMPJSInterface.AjaxUpload.dosubmit(JSObject.form, {'onStart' : JSObject.startUploadingData, 'onComplete' : JSObject.completeUploadingData});
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION SEND DATA                                           */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.sendData = function(){
		
		jQuery("#"+this.form.id,this.DOMDoc).unbind("submit");
		jQuery("#"+this.form.id,this.DOMDoc).bind("submit",function(){JSObject.submitForm();});
		jQuery("#"+this.form.id,this.DOMDoc).submit();
		
		JSObject.disableButton(JSObject.send_btn);
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                FUNCTION START UPLOADING DATA                                      */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.startUploadingData = function(){

		WMPJSInterface.Preloader.start();
		
		//disable form elements
		setTimeout(function(){
						var aElems = JSObject.form.elements;
						nElems = aElems.length;
						
						for (j=0; j<nElems; j++) {
							aElems[j].disabled = true;
						}
					},300);
		
		return true;
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                              FUNCTION COMPLETE UPLOADING DATA                                     */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.completeUploadingData = function(responseJSON){
		
		jQuery("#"+JSObject.form.id,JSObject.DOMDoc).unbind("submit");
		jQuery("#"+JSObject.form.id,JSObject.DOMDoc).bind("submit",function(){return false;});
	
		// remove preloader
		WMPJSInterface.Preloader.remove(100);
		
		var JSON = eval ("("+responseJSON+")");
		var response = Boolean(Number(String(JSON.status)));
        
        if (JSON.uploaded_cover != undefined) 
            JSObject.displayImage("cover", JSON.uploaded_cover)

		if (response == true && JSON.messages.length == 0){
			
            // show message
            var message = 'Your app has been successfully modified!';
            WMPJSInterface.Loader.display({message: message});
            
		} else {
			
			// show messages
			if (JSON.messages.length == 0) {
				
				var message = 'There was an error. Please reload the page and try again.';
				WMPJSInterface.Loader.display({message: message});
				
			} else {
				
				for (i = 0; i < JSON.messages.length; i++ )
					WMPJSInterface.Loader.display({message: JSON.messages[i]});
			}
		}

		//enable form elements
		setTimeout(function(){
						var aElems = JSObject.form.elements;
						nElems = aElems.length;
						for (j=0; j<nElems; j++) {
							aElems[j].disabled = false;
						}
					},300);
		
		//enable buttons
		JSObject.addButtonsActions();
		
	}

}