1 /**
  2  * @file 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 kocujILV12aBackendMessageVals */
 24 
 25 /**
 26  * Message prototype constructor
 27  *
 28  * @constructs
 29  * @namespace kocujILV12aCBackendMessage
 30  * @public
 31  * @return {void}
 32  */
 33 function kocujILV12aCBackendMessage() {
 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 = kocujILV12aBackendMessageVals;
 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  * Message prototype
 56  *
 57  * @namespace kocujILV12aCBackendMessage
 58  * @public
 59  */
 60 kocujILV12aCBackendMessage.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] = true;
152 		}
153 		this._objAllJsAjax.addProjectIfNotExists(args.projectId);
154 	},
155 
156 	/**
157 	 * Get HTML selector for message
158 	 *
159 	 * @public
160 	 * @param {string} projectId Project identifier
161 	 * @param {string} messageId Message identifier
162 	 * @return {string} HTML selector for message
163 	 */
164 	getHTMLSelectorMessage : function(projectId, messageId) {
165 		'use strict';
166 		// parse arguments
167 		projectId = this._parseProjectId(projectId);
168 		messageId = this._objHelper.initString(messageId);
169 		// exit
170 		return '#' + this._getHTMLNameMessage(projectId, messageId);
171 	},
172 
173 	/**
174 	 * Add close button for message
175 	 *
176 	 * @public
177 	 * @param {string} projectId Project identifier
178 	 * @param {string} messageId Message identifier
179 	 * @return {void}
180 	 */
181 	addMessageCloseButton : function(projectId, messageId) {
182 		'use strict';
183 		// get this object
184 		var self = this;
185 		(function($) {
186 			// parse arguments
187 			projectId = self._parseProjectId(projectId);
188 			messageId = self._objHelper.initString(messageId);
189 			// add close button event
190 			$(self.getHTMLSelectorMessage(projectId, messageId) + ' .notice-dismiss').click(function() {
191 				// close message
192 				$(self.getHTMLSelectorMessage(projectId, messageId)).hide();
193 				// save information that message has been closed
194 				self._objAllJsAjax.sendPost(projectId, 'message_close', ajaxurl, 'text', {
195 					action    : self._valsPrefix + '_' + projectId + '__message_close',
196 					security  : self._valsSecurity,
197 					messageId : messageId
198 				});
199 			});
200 		}(jQuery));
201 	},
202 
203 	/**
204 	 * Get HTML prefix
205 	 *
206 	 * @private
207 	 * @param {string} projectId Project identifier
208 	 * @return {string} HTML prefix
209 	 */
210 	_getHTMLPrefix : function(projectId) {
211 		'use strict';
212 		// exit
213 		return this._valsPrefix + '_' + projectId + '__';
214 	},
215 
216 	/**
217 	 * Get HTML prefix for message
218 	 *
219 	 * @private
220 	 * @param {string} projectId Project identifier
221 	 * @param {string} messageId Message identifier
222 	 * @return {string} HTML prefix for message review
223 	 */
224 	_getHTMLNameMessage : function(projectId, messageId) {
225 		'use strict';
226 		// exit
227 		return this._getHTMLPrefix(projectId) + 'message_' + messageId;
228 	},
229 
230 	/**
231 	 * Parse project identifier
232 	 *
233 	 * @private
234 	 * @param {string} projectId Project identifier
235 	 * @return {string} Parsed project identifier
236 	 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.EMPTY_PROJECT_ID if project identifier entered in projectId is empty
237 	 */
238 	_parseProjectId : function(projectId) {
239 		'use strict';
240 		// parse project identifier
241 		projectId = this._objHelper.initString(projectId);
242 		if (projectId === '') {
243 			this._throwError('EMPTY_PROJECT_ID');
244 			return;
245 		}
246 		// check if project exists
247 		if (this._prj['prj_' + projectId] === undefined) {
248 			this._throwError('PROJECT_DOES_NOT_EXIST', projectId);
249 			return;
250 		}
251 		// exit
252 		return projectId;
253 	},
254 
255 	/**
256 	 * Check arguments for adding project
257 	 *
258 	 * @private
259 	 * @param {string} projectId Project identifier
260 	 * @return {Object} Parsed arguments for adding project
261 	 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.EMPTY_PROJECT_ID if project identifier entered in projectId is empty
262 	 */
263 	_checkAddProject : function(projectId) {
264 		'use strict';
265 		// parse arguments
266 		projectId = this._objHelper.initString(projectId);
267 		if (projectId === '') {
268 			this._throwError('EMPTY_PROJECT_ID');
269 			return;
270 		}
271 		// exit
272 		return {
273 			projectId : projectId,
274 		};
275 	},
276 
277 	/**
278 	 * Throw an error if debugging is enabled
279 	 *
280 	 * @private
281 	 * @param {string} codeString Error code in string format
282 	 * @param {string} [param] Parameter for error information
283 	 * @return {void}
284 	 */
285 	_throwError : function(codeString, param) {
286 		'use strict';
287 		// parse arguments
288 		codeString = this._objHelper.initString(codeString);
289 		if (codeString === '') {
290 			return;
291 		}
292 		param = this._objHelper.initString(param);
293 		// throw an error
294 		if (this._valsThrowErrors) {
295 			/* jshint evil: true */
296 			eval('throw new kocujILV12aCException(kocujILV12aExceptionCode.' + codeString + ', this._thisFilename, param);');
297 		}
298 	}
299 };
300 
301 // initialize
302 var kocujILV12aBackendMessage = new kocujILV12aCBackendMessage();
303