1 /**
  2  * @file Update message
  3  *
  4  * @author Dominik Kocuj
  5  * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2 or later
  6  * @copyright Copyright (c) 2016-2018 kocuj.pl
  7  */
  8 
  9 (function() {})(); // empty function for correct minify with comments
 10 //'use strict'; // for jshint uncomment this and comment line above
 11 
 12 /* jshint strict: true */
 13 /* jshint -W034 */
 14 
 15 /* global document */
 16 /* global jQuery */
 17 
 18 /* global ajaxurl */
 19 
 20 /* global kocujILV12aHelper */
 21 /* global kocujILV12aAllJsAjax */
 22 
 23 /* global kocujILV12aBackendUpdateMessageVals */
 24 
 25 /**
 26  * Update message prototype constructor
 27  *
 28  * @constructs
 29  * @namespace kocujILV12aCBackendReviewMessage
 30  * @public
 31  * @return {void}
 32  */
 33 function kocujILV12aCBackendUpdateMessage() {
 34 	'use strict';
 35 	/* jshint validthis: true */
 36 	// get this object
 37 	var self = this;
 38 	// initialize objects
 39 	self._objHelper = kocujILV12aHelper;
 40 	self._objAllJsAjax = kocujILV12aAllJsAjax;
 41 	// get current script filename
 42 	self._thisFilename = document.scripts[document.scripts.length-1].src;
 43 	// get settings
 44 	var vals = kocujILV12aBackendUpdateMessageVals;
 45 	if (vals.throwErrors === '1') {
 46 		self._valsThrowErrors = true;
 47 	} else {
 48 		self._valsThrowErrors = false;
 49 	}
 50 	self._valsPrefix = vals.prefix;
 51 	self._valsSecurity = vals.security;
 52 }
 53 
 54 /**
 55  * Update message prototype
 56  *
 57  * @namespace kocujILV12aCBackendReviewMessage
 58  * @public
 59  */
 60 kocujILV12aCBackendUpdateMessage.prototype = {
 61 	/**
 62 	 * Object kocujILV12aHelper
 63 	 *
 64 	 * @private
 65 	 * @type {Object}
 66 	 */
 67 	_objHelper : null,
 68 
 69 	/**
 70 	 * Object kocujILV12aAllJsAjax
 71 	 *
 72 	 * @private
 73 	 * @type {Object}
 74 	 */
 75 	_objAllJsAjax : null,
 76 
 77 	/**
 78 	 * Current script filename
 79 	 *
 80 	 * @private
 81 	 * @type {string}
 82 	 */
 83 	_thisFilename : '',
 84 
 85 	/**
 86 	 * Projects list
 87 	 *
 88 	 * @private
 89 	 * @type {Array}
 90 	 */
 91 	_prj : [],
 92 
 93 	/**
 94 	 * Script settings - throw errors (true) or not (false)
 95 	 *
 96 	 * @private
 97 	 * @type {string}
 98 	 */
 99 	_valsThrowErrors : false,
100 
101 	/**
102 	 * Script settings - prefix
103 	 *
104 	 * @private
105 	 * @type {string}
106 	 */
107 	_valsPrefix : '',
108 
109 	/**
110 	 * Script settings - security string
111 	 *
112 	 * @private
113 	 * @type {string}
114 	 */
115 	_valsSecurity : '',
116 
117 	/**
118 	 * Add project
119 	 *
120 	 * @public
121 	 * @param {string} projectId Project identifier
122 	 * @return {void}
123 	 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.PROJECT_ALREADY_EXISTS if project identifier entered in projectId already exists
124 	 */
125 	addProject : function(projectId) {
126 		'use strict';
127 		// parse arguments
128 		var args = this._checkAddProject(projectId);
129 		// add project
130 		if (this._prj['prj_' + args.projectId] === undefined) {
131 			this.addProjectIfNotExists(args.projectId);
132 		} else {
133 			this._throwError('PROJECT_ALREADY_EXISTS', args.projectId);
134 			return;
135 		}
136 	},
137 
138 	/**
139 	 * Add project if not exists
140 	 *
141 	 * @public
142 	 * @param {string} projectId Project identifier
143 	 * @return {void}
144 	 */
145 	addProjectIfNotExists : function(projectId) {
146 		'use strict';
147 		// parse arguments
148 		var args = this._checkAddProject(projectId);
149 		// add project
150 		if (this._prj['prj_' + args.projectId] === undefined) {
151 			this._prj['prj_' + args.projectId] = [];
152 		}
153 		this._objAllJsAjax.addProjectIfNotExists(args.projectId);
154 	},
155 
156 	/**
157 	 * Close window
158 	 *
159 	 * @public
160 	 * @param {string} projectId Project identifier
161 	 * @return {void}
162 	 */
163 	sendCloseEvent : function(projectId) {
164 		'use strict';
165 		// get this object
166 		var self = this;
167 		(function($) {
168 			// parse arguments
169 			projectId = self._parseProjectId(projectId);
170 			// call AJAX
171 			self._objAllJsAjax.sendPost(projectId, 'update_message_close', ajaxurl, 'text', {
172 				action   : self._valsPrefix + '_' + projectId + '__update_message_close',
173 				security : self._valsSecurity
174 			}, {});
175 		}(jQuery));
176 	},
177 
178 	/**
179 	 * Parse project identifier
180 	 *
181 	 * @private
182 	 * @param {string} projectId Project identifier
183 	 * @return {string} Parsed project identifier
184 	 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.EMPTY_PROJECT_ID if project identifier entered in projectId is empty
185 	 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.PROJECT_DOES_NOT_EXIST if project identifier entered in projectId does not exist
186 	 */
187 	_parseProjectId : function(projectId) {
188 		'use strict';
189 		// parse project identifier
190 		projectId = this._objHelper.initString(projectId);
191 		if (projectId === '') {
192 			this._throwError('EMPTY_PROJECT_ID');
193 			return;
194 		}
195 		// check if project exists
196 		if (this._prj['prj_' + projectId] === undefined) {
197 			this._throwError('PROJECT_DOES_NOT_EXIST', projectId);
198 			return;
199 		}
200 		// exit
201 		return projectId;
202 	},
203 
204 	/**
205 	 * Check arguments for adding project
206 	 *
207 	 * @private
208 	 * @param {string} projectId Project identifier
209 	 * @return {Object} Parsed arguments for adding project
210 	 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.EMPTY_PROJECT_ID if project identifier entered in projectId is empty
211 	 */
212 	_checkAddProject : function(projectId) {
213 		'use strict';
214 		// parse arguments
215 		projectId = this._objHelper.initString(projectId);
216 		if (projectId === '') {
217 			this._throwError('EMPTY_PROJECT_ID');
218 			return;
219 		}
220 		// exit
221 		return {
222 			projectId : projectId
223 		};
224 	},
225 
226 	/**
227 	 * Throw an error if debugging is enabled
228 	 *
229 	 * @private
230 	 * @param {string} codeString Error code in string format
231 	 * @param {string} [param] Parameter for error information
232 	 * @return {void}
233 	 */
234 	_throwError : function(codeString, param) {
235 		'use strict';
236 		// parse arguments
237 		codeString = this._objHelper.initString(codeString);
238 		if (codeString === '') {
239 			return;
240 		}
241 		param = this._objHelper.initString(param);
242 		// throw an error
243 		if (this._valsThrowErrors) {
244 			/* jshint evil: true */
245 			eval('throw new kocujILV12aCException(kocujILV12aExceptionCode.' + codeString + ', this._thisFilename, param);');
246 		}
247 	}
248 };
249 
250 // initialize
251 var kocujILV12aBackendUpdateMessage = new kocujILV12aCBackendUpdateMessage();
252