1 /**
  2  * @file Review 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 /* global window */
 18 
 19 /* global kocujILV12aHelper */
 20 
 21 /* global kocujILV12aBackendReviewMessageVals */
 22 
 23 /**
 24  * Review message prototype constructor
 25  *
 26  * @constructs
 27  * @namespace kocujILV12aCBackendReviewMessage
 28  * @public
 29  * @return {void}
 30  */
 31 function kocujILV12aCBackendReviewMessage() {
 32 	'use strict';
 33 	/* jshint validthis: true */
 34 	// get this object
 35 	var self = this;
 36 	// initialize objects
 37 	self._objHelper = kocujILV12aHelper;
 38 	// get current script filename
 39 	self._thisFilename = document.scripts[document.scripts.length-1].src;
 40 	// get settings
 41 	var vals = kocujILV12aBackendReviewMessageVals;
 42 	if (vals.throwErrors === '1') {
 43 		self._valsThrowErrors = true;
 44 	} else {
 45 		self._valsThrowErrors = false;
 46 	}
 47 	self._valsPrefix = vals.prefix;
 48 	self._valsSecurity = vals.security;
 49 }
 50 
 51 /**
 52  * Review message prototype
 53  *
 54  * @namespace kocujILV12aCBackendReviewMessage
 55  * @public
 56  */
 57 kocujILV12aCBackendReviewMessage.prototype = {
 58 	/**
 59 	 * Object kocujILV12aHelper
 60 	 *
 61 	 * @private
 62 	 * @type {Object}
 63 	 */
 64 	_objHelper : null,
 65 
 66 	/**
 67 	 * Current script filename
 68 	 *
 69 	 * @private
 70 	 * @type {string}
 71 	 */
 72 	_thisFilename : '',
 73 
 74 	/**
 75 	 * Projects list
 76 	 *
 77 	 * @private
 78 	 * @type {Array}
 79 	 */
 80 	_prj : [],
 81 
 82 	/**
 83 	 * Script settings - throw errors (true) or not (false)
 84 	 *
 85 	 * @private
 86 	 * @type {string}
 87 	 */
 88 	_valsThrowErrors : false,
 89 
 90 	/**
 91 	 * Script settings - prefix
 92 	 *
 93 	 * @private
 94 	 * @type {string}
 95 	 */
 96 	_valsPrefix : '',
 97 
 98 	/**
 99 	 * Script settings - security string
100 	 *
101 	 * @private
102 	 * @type {string}
103 	 */
104 	_valsSecurity : '',
105 
106 	/**
107 	 * Add project
108 	 *
109 	 * @public
110 	 * @param {string} projectId Project identifier
111 	 * @param {string} [voteUrl] URL for voting for
112 	 * @param {string} [facebookUrl] URL for tell others on Facebook
113 	 * @param {string} [twitterUrl] URL for tell others on Twitter
114 	 * @param {string} [translationUrl] URL for project translation
115 	 * @return {void}
116 	 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.PROJECT_ALREADY_EXISTS if project identifier entered in projectId already exists
117 	 */
118 	addProject : function(projectId, voteUrl, facebookUrl, twitterUrl, translationUrl) {
119 		'use strict';
120 		// parse arguments
121 		var args = this._checkAddProject(projectId, voteUrl, facebookUrl, twitterUrl, translationUrl);
122 		// add project
123 		if (this._prj['prj_' + args.projectId] === undefined) {
124 			this.addProjectIfNotExists(args.projectId, args.voteUrl, args.facebookUrl, args.twitterUrl, args.translationUrl);
125 		} else {
126 			this._throwError('PROJECT_ALREADY_EXISTS', args.projectId);
127 			return;
128 		}
129 	},
130 
131 	/**
132 	 * Add project if not exists
133 	 *
134 	 * @public
135 	 * @param {string} projectId Project identifier
136 	 * @param {string} [voteUrl] URL for voting for
137 	 * @param {string} [facebookUrl] URL for tell others on Facebook
138 	 * @param {string} [twitterUrl] URL for tell others on Twitter
139 	 * @param {string} [translationUrl] URL for project translation
140 	 * @return {void}
141 	 */
142 	addProjectIfNotExists : function(projectId, voteUrl, facebookUrl, twitterUrl, translationUrl) {
143 		'use strict';
144 		// parse arguments
145 		var args = this._checkAddProject(projectId, voteUrl, facebookUrl, twitterUrl, translationUrl);
146 		// add project
147 		if (this._prj['prj_' + args.projectId] === undefined) {
148 			this._prj['prj_' + args.projectId] = {
149 				voteUrl        : args.voteUrl,
150 				facebookUrl    : args.facebookUrl,
151 				twitterUrl     : args.twitterUrl,
152 				translationUrl : args.translationUrl
153 			};
154 		}
155 	},
156 
157 	/**
158 	 * Get HTML selector for message review
159 	 *
160 	 * @public
161 	 * @param {string} projectId Project identifier
162 	 * @return {string} HTML selector for message review
163 	 */
164 	getHTMLSelectorMessageReview : function(projectId) {
165 		'use strict';
166 		// parse arguments
167 		projectId = this._parseProjectId(projectId);
168 		// exit
169 		return '#' + this._getHTMLNameMessageReview(projectId);
170 	},
171 
172 	/**
173 	 * Get HTML selector for message review by voting
174 	 *
175 	 * @public
176 	 * @param {string} projectId Project identifier
177 	 * @return {string} HTML selector for message review by voting
178 	 */
179 	getHTMLSelectorReviewMessageVote : function(projectId) {
180 		'use strict';
181 		// parse arguments
182 		projectId = this._parseProjectId(projectId);
183 		// exit
184 		return '#' + this._getHTMLNameReviewMessageVote(projectId);
185 	},
186 
187 	/**
188 	 * Get HTML selector for message review by Facebook
189 	 *
190 	 * @public
191 	 * @param {string} projectId Project identifier
192 	 * @return {string} HTML selector for message review by Facebook
193 	 */
194 	getHTMLSelectorReviewMessageFacebook : function(projectId) {
195 		'use strict';
196 		// parse arguments
197 		projectId = this._parseProjectId(projectId);
198 		// exit
199 		return '#' + this._getHTMLNameReviewMessageFacebook(projectId);
200 	},
201 
202 	/**
203 	 * Get HTML selector for message review by Twitter
204 	 *
205 	 * @public
206 	 * @param {string} projectId Project identifier
207 	 * @return {string} HTML selector for message review by Twitter
208 	 */
209 	getHTMLSelectorReviewMessageTwitter : function(projectId) {
210 		'use strict';
211 		// parse arguments
212 		projectId = this._parseProjectId(projectId);
213 		// exit
214 		return '#' + this._getHTMLNameReviewMessageTwitter(projectId);
215 	},
216 
217 	/**
218 	 * Get HTML selector for message review about translation
219 	 *
220 	 * @public
221 	 * @param {string} projectId Project identifier
222 	 * @return {string} HTML selector for message review about translation
223 	 */
224 	getHTMLSelectorReviewMessageTranslation : function(projectId) {
225 		'use strict';
226 		// parse arguments
227 		projectId = this._parseProjectId(projectId);
228 		// exit
229 		return '#' + this._getHTMLNameReviewMessageTranslation(projectId);
230 	},
231 
232 	/**
233 	 * Set review message events
234 	 *
235 	 * @public
236 	 * @param {string} projectId Project identifier
237 	 * @return {void}
238 	 */
239 	setEvents : function(projectId) {
240 		'use strict';
241 		// get this object
242 		var self = this;
243 		(function($) {
244 			// parse arguments
245 			projectId = self._parseProjectId(projectId);
246 			// add events
247 			if (self._prj['prj_' + projectId].voteUrl !== '') {
248 				self._setEvent(projectId, self.getHTMLSelectorReviewMessageVote(projectId), 'vote', self._prj['prj_' + projectId].voteUrl);
249 			}
250 			if (self._prj['prj_' + projectId].facebookUrl !== '') {
251 				self._setEvent(projectId, self.getHTMLSelectorReviewMessageFacebook(projectId), 'facebook', self._prj['prj_' + projectId].facebookUrl);
252 			}
253 			if (self._prj['prj_' + projectId].twitterUrl !== '') {
254 				self._setEvent(projectId, self.getHTMLSelectorReviewMessageTwitter(projectId), 'twitter', self._prj['prj_' + projectId].twitterUrl);
255 			}
256 			if (self._prj['prj_' + projectId].translationUrl !== '') {
257 				self._setEvent(projectId, self.getHTMLSelectorReviewMessageTranslation(projectId), 'translation', self._prj['prj_' + projectId].translationUrl);
258 			}
259 		}(jQuery));
260 	},
261 
262 	/**
263 	 * Get event name prefix
264 	 *
265 	 * @private
266 	 * @param {string} projectId Project identifier
267 	 * @return {string} Event name prefix
268 	 */
269 	_getEventNamePrefix : function(projectId) {
270 		'use strict';
271 		// exit
272 		return this._valsPrefix + '_' + projectId + '__';
273 	},
274 
275 	/**
276 	 * Get HTML prefix
277 	 *
278 	 * @private
279 	 * @param {string} projectId Project identifier
280 	 * @return {string} HTML prefix
281 	 */
282 	_getHTMLPrefix : function(projectId) {
283 		'use strict';
284 		// exit
285 		return this._valsPrefix + '_' + projectId + '__';
286 	},
287 
288 	/**
289 	 * Get HTML prefix for message review
290 	 *
291 	 * @private
292 	 * @param {string} projectId Project identifier
293 	 * @return {string} HTML prefix for message review
294 	 */
295 	_getHTMLNameMessageReview : function(projectId) {
296 		'use strict';
297 		// exit
298 		return this._getHTMLPrefix(projectId) + 'message_review';
299 	},
300 
301 	/**
302 	 * Get HTML prefix for message review by voting
303 	 *
304 	 * @private
305 	 * @param {string} projectId Project identifier
306 	 * @return {string} HTML prefix for message review by voting
307 	 */
308 	_getHTMLNameReviewMessageVote : function(projectId) {
309 		'use strict';
310 		// exit
311 		return this._getHTMLPrefix(projectId) + 'review_message_vote';
312 	},
313 
314 	/**
315 	 * Get HTML prefix for message review by Facebook
316 	 *
317 	 * @private
318 	 * @param {string} projectId Project identifier
319 	 * @return {string} HTML prefix for message review by Facebook
320 	 */
321 	_getHTMLNameReviewMessageFacebook : function(projectId) {
322 		'use strict';
323 		// exit
324 		return this._getHTMLPrefix(projectId) + 'review_message_facebook';
325 	},
326 
327 	/**
328 	 * Get HTML prefix for message review by Twitter
329 	 *
330 	 * @private
331 	 * @param {string} projectId Project identifier
332 	 * @return {string} HTML prefix for message review by Twitter
333 	 */
334 	_getHTMLNameReviewMessageTwitter : function(projectId) {
335 		'use strict';
336 		// exit
337 		return this._getHTMLPrefix(projectId) + 'review_message_twitter';
338 	},
339 
340 	/**
341 	 * Get HTML prefix for message review about translation
342 	 *
343 	 * @private
344 	 * @param {string} projectId Project identifier
345 	 * @return {string} HTML prefix for message review about translation
346 	 */
347 	_getHTMLNameReviewMessageTranslation : function(projectId) {
348 		'use strict';
349 		// exit
350 		return this._getHTMLPrefix(projectId) + 'review_message_translation';
351 	},
352 
353 	/**
354 	 * Parse project identifier
355 	 *
356 	 * @private
357 	 * @param {string} projectId Project identifier
358 	 * @return {string} Parsed project identifier
359 	 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.EMPTY_PROJECT_ID if project identifier entered in projectId is empty
360 	 */
361 	_parseProjectId : function(projectId) {
362 		'use strict';
363 		// parse project identifier
364 		projectId = this._objHelper.initString(projectId);
365 		if (projectId === '') {
366 			this._throwError('EMPTY_PROJECT_ID');
367 			return;
368 		}
369 		// check if project exists
370 		if (this._prj['prj_' + projectId] === undefined) {
371 			this._throwError('PROJECT_DOES_NOT_EXIST', projectId);
372 			return;
373 		}
374 		// exit
375 		return projectId;
376 	},
377 
378 	/**
379 	 * Check arguments for adding project
380 	 *
381 	 * @private
382 	 * @param {string} projectId Project identifier
383 	 * @param {string} [voteUrl] URL for voting for
384 	 * @param {string} [facebookUrl] URL for tell others on Facebook
385 	 * @param {string} [twitterUrl] URL for tell others on Twitter
386 	 * @param {string} [translationUrl] URL for project translation
387 	 * @return {Object} Parsed arguments for adding project
388 	 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.EMPTY_PROJECT_ID if project identifier entered in projectId is empty
389 	 */
390 	_checkAddProject : function(projectId, voteUrl, facebookUrl, twitterUrl, translationUrl) {
391 		'use strict';
392 		// parse arguments
393 		projectId = this._objHelper.initString(projectId);
394 		if (projectId === '') {
395 			this._throwError('EMPTY_PROJECT_ID');
396 			return;
397 		}
398 		voteUrl = this._objHelper.initString(voteUrl);
399 		facebookUrl = this._objHelper.initString(facebookUrl);
400 		twitterUrl = this._objHelper.initString(twitterUrl);
401 		translationUrl = this._objHelper.initString(translationUrl);
402 		// exit
403 		return {
404 			projectId      : projectId,
405 			voteUrl        : voteUrl,
406 			facebookUrl    : facebookUrl,
407 			twitterUrl     : twitterUrl,
408 			translationUrl : translationUrl
409 		};
410 	},
411 
412 	/**
413 	 * Throw an error if debugging is enabled
414 	 *
415 	 * @private
416 	 * @param {string} codeString Error code in string format
417 	 * @param {string} [param] Parameter for error information
418 	 * @return {void}
419 	 */
420 	_throwError : function(codeString, param) {
421 		'use strict';
422 		// parse arguments
423 		codeString = this._objHelper.initString(codeString);
424 		if (codeString === '') {
425 			return;
426 		}
427 		param = this._objHelper.initString(param);
428 		// throw an error
429 		if (this._valsThrowErrors) {
430 			/* jshint evil: true */
431 			eval('throw new kocujILV12aCException(kocujILV12aExceptionCode.' + codeString + ', this._thisFilename, param);');
432 		}
433 	},
434 
435 	/**
436 	 * Set event
437 	 *
438 	 * @private
439 	 * @param {string} projectId Project identifier
440 	 * @param {string} elementPath DOM path to element inside the review message box
441 	 * @param {string} eventNameSuffix Suffix for event name
442 	 * @param {string} url URL opened in new window after click event
443 	 * @return {void}
444 	 */
445 	_setEvent : function(projectId, elementPath, eventNameSuffix, url) {
446 		'use strict';
447 		// get this object
448 		var self = this;
449 		(function($) {
450 			// parse arguments
451 			projectId = self._parseProjectId(projectId);
452 			elementPath = self._objHelper.initString(elementPath);
453 			eventNameSuffix = self._objHelper.initString(eventNameSuffix);
454 			url = self._objHelper.initString(url);
455 			// get selectors
456 			var selectorMessageReviewButton = $(self.getHTMLSelectorMessageReview(projectId) + ' ' + elementPath);
457 			// set event
458 			if (selectorMessageReviewButton.length > 0) {
459 				selectorMessageReviewButton.bind('click.' + self._getEventNamePrefix(projectId) + 'review_message_' + eventNameSuffix, {
460 					projectId : projectId,
461 					url       : url
462 				}, function(event) {
463 					event.preventDefault();
464 					window.open(event.data.url, '_blank');
465 				});
466 			}
467 		}(jQuery));
468 	}
469 };
470 
471 // initialize
472 var kocujILV12aBackendReviewMessage = new kocujILV12aCBackendReviewMessage();
473