summaryrefslogtreecommitdiff
blob: 420e0cc75eea824e238c97ba42a0c48b46a85b07 (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
/*****************************************************************************************************/
/*                                                                                                   */
/*                                    	'ACTIVATE / DEACTIVATE PAGES'                                */
/*                                                                                                   */
/*****************************************************************************************************/

function WMP_EDIT_PAGES(){

    var JSObject = this;

    this.type = "wmp_editpages";

    this.form;
    this.DOMDoc;
    
    this.changingStatus = false;
	
	/*****************************************************************************************************/
    /*                                                                                                   */
    /*                              FUNCTION INIT - called from WMPJSInterface                           */
    /*                                                                                                   */
    /*****************************************************************************************************/
    this.init = function(){

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

        // save a reference to the FORM and remove the default submit action
        this.form = this.DOMDoc.getElementById(this.type+'_form');

        if (this.form == null){
            return;
        }

        this.initPages();
		
		 // custom list actions
        this.initListActions();
    }


	/*****************************************************************************************************/
    /*                                                                                                   */
    /*                                  FUNCTION INIT LIST ACTIONS                                		 */
    /*                                                                                                   */
    /*****************************************************************************************************/
    
    this.initListActions = function(){
    	
    	// attach edit actions for each feed
		jQuery( "ul.pages li div.row", this.DOMDoc ).on("click", JSObject.changeStatus);
    };


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

        // close button action for the inactive categories warning
        jQuery( "#" + JSObject.type + "_warning a.close-x", this.form ).on("click", function(){
            jQuery('#'+JSObject.type+'_warning', JSObject.DOMDoc).hide();
        });
    };
	
	/*****************************************************************************************************/
    /*                                                                                                   */
    /*                                  FUNCTION INIT VALIDATION                                         */
    /*                                                                                                   */
    /*****************************************************************************************************/
    this.changeStatus = function(){

		var pageId = jQuery(this).closest("li").attr("data-page-id");
        var Container = jQuery(this).closest("li");

        /*****************************************************************************************************/
	    /*                                                                                                   */
	    /*                                	CHANGE STATUS ITEM ACTIONS                                		 */
	    /*                                                                                                   */
	    /*****************************************************************************************************/
	    
		
		var isConfirmed = confirm("Are you sure you want to change the status for this page?");

		if (isConfirmed) {

			var currentStatus;
			
			var statusContainer = jQuery('.status',Container);
			
			if (statusContainer.hasClass("active") == false) {
				currentStatus = "active";	
			} else {
				currentStatus = "inactive";
			}
				
			if (JSObject.changingStatus == false) {
				
				WMPJSInterface.Preloader.start();
				
				jQuery.post(
					ajaxurl, 
					{
						'action': 'wmp_content_status',
						'id':   pageId,
						'status': currentStatus,
                        'type': 'page'
					}, 
					function(response){
						
						JSObject.changingStatus = false;
						WMPJSInterface.Preloader.remove(100);
						
						var response = Boolean(Number(String(response)));
					 
						if (response == true) {
						
							// change status class and text
							statusContainer.addClass(currentStatus);
							statusContainer.removeClass(currentStatus == 'active' ? 'inactive' : 'active');
							
							statusContainer.text(currentStatus);
							
							// success message								
							var message = 'The status of this page has been changed.';
							WMPJSInterface.Loader.display({message: message});
							
							// count remaining active categories
							var no_active_pages = jQuery( "li span.active.main-page", JSObject.form ).length;
							if (no_active_pages > 0){
								jQuery('#'+JSObject.type+'_warning', JSObject.DOMDoc).hide();
							} else {
								jQuery('#'+JSObject.type+'_warning', JSObject.DOMDoc).show();
							}
							
						} else {
						
							// error message
							var message = 'There was an error. Please reload the page and try again in few seconds or contact the plugin administrator if the problem persists.';
							WMPJSInterface.Loader.display({message: message});	
							
						}
					}
				);
			}
		}
    };
}