1 /**
  2  * @file Image upload
  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 kocujILV12aHelper */
 19 /* global kocujILV12aDataHelper */
 20 
 21 /* global kocujILV12aBackendSettingsFieldsImageVals */
 22 
 23 /**
 24  * Image upload prototype constructor
 25  *
 26  * @constructs
 27  * @namespace kocujILV12aCBackendSettingsFieldsImage
 28  * @public
 29  * @return {void}
 30  */
 31 function kocujILV12aCBackendSettingsFieldsImage() {
 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 = kocujILV12aBackendSettingsFieldsImageVals;
 42 	if (vals.throwErrors === '1') {
 43 		self._valsThrowErrors = true;
 44 	} else {
 45 		self._valsThrowErrors = false;
 46 	}
 47 	self._valsTextWindowTitle = vals.textWindowTitle;
 48 	self._valsTextWindowButtonLabel = vals.textWindowButtonLabel;
 49 }
 50 
 51 /**
 52  * Image upload prototype
 53  *
 54  * @namespace kocujILV12aCBackendSettingsFieldsImage
 55  * @public
 56  */
 57 kocujILV12aCBackendSettingsFieldsImage.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 - text for window title
 92 	 *
 93 	 * @private
 94 	 * @type {string}
 95 	 */
 96 	_valsTextWindowTitle : '',
 97 
 98 	/**
 99 	 * Script settings - text for window button label
100 	 *
101 	 * @private
102 	 * @type {string}
103 	 */
104 	_valsTextWindowButtonLabel : '',
105 
106 	/**
107 	 * Add project
108 	 *
109 	 * @public
110 	 * @param {string} projectId Project identifier
111 	 * @param {string} [projectName] Project name
112 	 * @return {void}
113 	 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.PROJECT_ALREADY_EXISTS if project identifier entered in projectId already exists
114 	 */
115 	addProject : function(projectId, projectName) {
116 		'use strict';
117 		// parse arguments
118 		var args = this._checkAddProject(projectId, projectName);
119 		// add project
120 		if (this._prj['prj_' + args.projectId] === undefined) {
121 			this.addProjectIfNotExists(args.projectId, args.projectName);
122 		} else {
123 			this._throwError('PROJECT_ALREADY_EXISTS', args.projectId);
124 			return;
125 		}
126 	},
127 
128 	/**
129 	 * Add project if not exists
130 	 *
131 	 * @public
132 	 * @param {string} projectId Project identifier
133 	 * @param {string} [projectName] Project name
134 	 * @return {void}
135 	 */
136 	addProjectIfNotExists : function(projectId, projectName) {
137 		'use strict';
138 		// parse arguments
139 		var args = this._checkAddProject(projectId, projectName);
140 		// add project
141 		if (this._prj['prj_' + args.projectId] === undefined) {
142 			this._prj['prj_' + args.projectId] = {
143 				projectName   : args.projectName,
144 				fileFrame     : null,
145 				wpMediaPostId : 0,
146 				setToPostId   : 0,
147 			};
148 		}
149 	},
150 
151 	/**
152 	 * Get HTML selector for image container
153 	 *
154 	 * @public
155 	 * @param {string} projectId Project identifier
156 	 * @param {string} fieldHtmlId HTML field identifier
157 	 * @return {string} HTML selector for image container
158 	 */
159 	getHTMLSelectorImageContainer : function(projectId, fieldHtmlId) {
160 		'use strict';
161 		// parse arguments
162 		projectId = this._parseProjectId(projectId);
163 		fieldHtmlId = this._objHelper.initString(fieldHtmlId);
164 		// exit
165 		return '#' + this._getHTMLNameImageContainer(projectId, fieldHtmlId);
166 	},
167 
168 	/**
169 	 * Get HTML selector for uploading image button
170 	 *
171 	 * @public
172 	 * @param {string} projectId Project identifier
173 	 * @param {string} fieldHtmlId HTML field identifier
174 	 * @return {string} HTML selector for uploading image button
175 	 */
176 	getHTMLSelectorUploadImageButton : function(projectId, fieldHtmlId) {
177 		'use strict';
178 		// parse arguments
179 		projectId = this._parseProjectId(projectId);
180 		fieldHtmlId = this._objHelper.initString(fieldHtmlId);
181 		// exit
182 		return '#' + this._getHTMLNameUploadImageButton(projectId, fieldHtmlId);
183 	},
184 
185 	/**
186 	 * Get HTML selector for image identifier
187 	 *
188 	 * @public
189 	 * @param {string} projectId Project identifier
190 	 * @param {string} fieldHtmlId HTML field identifier
191 	 * @return {string} HTML selector for image identifier
192 	 */
193 	getHTMLSelectorImageId : function(projectId, fieldHtmlId) {
194 		'use strict';
195 		// parse arguments
196 		projectId = this._parseProjectId(projectId);
197 		fieldHtmlId = this._objHelper.initString(fieldHtmlId);
198 		// exit
199 		return '#' + this._getHTMLNameImageId(projectId, fieldHtmlId);
200 	},
201 
202 	/**
203 	 * Process image uploading
204 	 *
205 	 * @public
206 	 * @param {string} projectId Project identifier
207 	 * @param {string} fieldHtmlId HTML field identifier
208 	 * @return {void}
209 	 */
210 	process : function(projectId, fieldHtmlId) {
211 		'use strict';
212 		// get this object
213 		var self = this;
214 		(function($) {
215 			// parse parameters
216 			projectId = self._parseProjectId(projectId);
217 			fieldHtmlId = self._objHelper.initString(fieldHtmlId);
218 			// initialize values
219 			self._prj['prj_' + projectId].wpMediaPostId = wp.media.model.settings.post.id;
220 			// add upload event
221 			$(self.getHTMLSelectorUploadImageButton(projectId, fieldHtmlId)).bind('click', {
222 				self        : self,
223 				projectId   : projectId,
224 				fieldHtmlId : fieldHtmlId
225 			}, function(event) {
226 				// disable default event
227 				event.preventDefault();
228 				// optionally reopen media window
229 				if (event.data.self._prj['prj_' + event.data.projectId].fileFrame !== null) {
230 					event.data.self._prj['prj_' + event.data.projectId].fileFrame.uploader.uploader.param('post_id', $(self.getHTMLSelectorImageId(event.data.projectId, event.data.fieldHtmlId)).val());
231 					event.data.self._prj['prj_' + event.data.projectId].fileFrame.open();
232 					return;
233 				}
234 				// prepare media window
235 				event.data.self._prj['prj_' + event.data.projectId].fileFrame = wp.media.frames.file_frame = wp.media({
236 					title    : event.data.self._valsTextWindowTitle,
237 					button   : {
238 						text : event.data.self._valsTextWindowButtonLabel
239 					},
240 					library  : {
241 						type : 'image'
242 					},
243 					multiple : false
244 				});
245 				// add event
246 				event.data.self._prj['prj_' + event.data.projectId].fileFrame.on('select', function() {
247 					// get selected image
248 					var attachment = event.data.self._prj['prj_' + event.data.projectId].fileFrame.state().get('selection').first().toJSON();
249 					// set image
250 					$(event.data.self.getHTMLSelectorImageContainer(event.data.projectId, event.data.fieldHtmlId)).attr('src', attachment.url).css('width', 'auto');
251 					$(event.data.self.getHTMLSelectorImageId(event.data.projectId, event.data.fieldHtmlId)).val(attachment.id);
252 					wp.media.model.settings.post.id = event.data.self._prj['prj_' + event.data.projectId].wpMediaPostId;
253 				});
254 				// open media window
255 				event.data.self._prj['prj_' + event.data.projectId].fileFrame.open();
256 			});
257 		}(jQuery));
258 	},
259 
260 	/**
261 	 * Get HTML image container name
262 	 *
263 	 * @private
264 	 * @param {string} projectId Project identifier
265 	 * @param {string} fieldHtmlId HTML field identifier
266 	 * @param {number} number Number of element in array
267 	 * @return {string} HTML image container name
268 	 */
269 	_getHTMLNameImageContainer : function(projectId, fieldHtmlId) {
270 		'use strict';
271 		// exit
272 		return fieldHtmlId + '__image_preview';
273 	},
274 
275 	/**
276 	 * Get HTML uploading image button name
277 	 *
278 	 * @private
279 	 * @param {string} projectId Project identifier
280 	 * @param {string} fieldHtmlId HTML field identifier
281 	 * @param {number} number Number of element in array
282 	 * @return {string} HTML uploading image button name
283 	 */
284 	_getHTMLNameUploadImageButton : function(projectId, fieldHtmlId) {
285 		'use strict';
286 		// exit
287 		return fieldHtmlId + '__image_upload';
288 	},
289 
290 	/**
291 	 * Get HTML image identifier name
292 	 *
293 	 * @private
294 	 * @param {string} projectId Project identifier
295 	 * @param {string} fieldHtmlId HTML field identifier
296 	 * @param {number} number Number of element in array
297 	 * @return {string} HTML image identifier name
298 	 */
299 	_getHTMLNameImageId : function(projectId, fieldHtmlId) {
300 		'use strict';
301 		// exit
302 		return fieldHtmlId + '__image_hidden';
303 	},
304 
305 	/**
306 	 * Parse project identifier
307 	 *
308 	 * @private
309 	 * @param {string} projectId Project identifier
310 	 * @return {string} Parsed project identifier
311 	 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.EMPTY_PROJECT_ID if project identifier entered in projectId is empty
312 	 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.PROJECT_DOES_NOT_EXIST if project identifier entered in projectId does not exist
313 	 */
314 	_parseProjectId : function(projectId) {
315 		'use strict';
316 		// parse project identifier
317 		projectId = this._objHelper.initString(projectId);
318 		if (projectId === '') {
319 			this._throwError('EMPTY_PROJECT_ID');
320 			return;
321 		}
322 		// check if project exists
323 		if (this._prj['prj_' + projectId] === undefined) {
324 			this._throwError('PROJECT_DOES_NOT_EXIST', projectId);
325 			return;
326 		}
327 		// exit
328 		return projectId;
329 	},
330 
331 	/**
332 	 * Check arguments for adding project
333 	 *
334 	 * @private
335 	 * @param {string} projectId Project identifier
336 	 * @param {string} [projectName] Project name
337 	 * @return {Object} Parsed arguments for adding project
338 	 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.EMPTY_PROJECT_ID if project identifier entered in projectId is empty
339 	 */
340 	_checkAddProject : function(projectId, projectName) {
341 		'use strict';
342 		// parse arguments
343 		projectId = this._objHelper.initString(projectId);
344 		if (projectId === '') {
345 			this._throwError('EMPTY_PROJECT_ID');
346 			return;
347 		}
348 		projectName = this._objHelper.initString(projectName);
349 		// exit
350 		return {
351 			projectId   : projectId,
352 			projectName : projectName
353 		};
354 	},
355 
356 	/**
357 	 * Throw an error if debugging is enabled
358 	 *
359 	 * @private
360 	 * @param {string} codeString Error code in string format
361 	 * @param {string} [param] Parameter for error information
362 	 * @return {void}
363 	 */
364 	_throwError : function(codeString, param) {
365 		'use strict';
366 		// parse arguments
367 		codeString = this._objHelper.initString(codeString);
368 		if (codeString === '') {
369 			return;
370 		}
371 		param = this._objHelper.initString(param);
372 		// throw an error
373 		if (this._valsThrowErrors) {
374 			/* jshint evil: true */
375 			eval('throw new kocujILV12aCException(kocujILV12aExceptionCode.' + codeString + ', this._thisFilename, param);');
376 		}
377 	}
378 };
379 
380 // initialize
381 var kocujILV12aBackendSettingsFieldsImage = new kocujILV12aCBackendSettingsFieldsImage();
382