1 /** 2 * @file Tabs 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 /* jshint -W107 */ 15 16 /* global document */ 17 /* global jQuery */ 18 19 /* global kocujILV12aHelper */ 20 21 /* global kocujILV12aBackendSettingsFormTabsVals */ 22 23 /** 24 * Tabs prototype constructor 25 * 26 * @constructs 27 * @namespace kocujILV12aCBackendSettingsFormTabs 28 * @public 29 * @return {void} 30 */ 31 function kocujILV12aCBackendSettingsFormTabs() { 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 = kocujILV12aBackendSettingsFormTabsVals; 42 if (vals.throwErrors === '1') { 43 self._valsThrowErrors = true; 44 } else { 45 self._valsThrowErrors = false; 46 } 47 self._valsPrefix = vals.prefix; 48 } 49 50 /** 51 * Tabs prototype 52 * 53 * @namespace kocujILV12aCBackendSettingsFormTabs 54 * @public 55 */ 56 kocujILV12aCBackendSettingsFormTabs.prototype = { 57 /** 58 * Object kocujILV12aHelper 59 * 60 * @private 61 * @type {Object} 62 */ 63 _objHelper : null, 64 65 /** 66 * Current script filename 67 * 68 * @private 69 * @type {string} 70 */ 71 _thisFilename : '', 72 73 /** 74 * Projects list 75 * 76 * @private 77 * @type {Array} 78 */ 79 _prj : [], 80 81 /** 82 * Script settings - throw errors (true) or not (false) 83 * 84 * @private 85 * @type {string} 86 */ 87 _valsThrowErrors : false, 88 89 /** 90 * Script settings - prefix 91 * 92 * @private 93 * @type {string} 94 */ 95 _valsPrefix : '', 96 97 /** 98 * Add project 99 * 100 * @public 101 * @param {string} projectId Project identifier 102 * @param {string} [projectName] Project name 103 * @return {void} 104 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.PROJECT_ALREADY_EXISTS if project identifier entered in projectId already exists 105 */ 106 addProject : function(projectId, projectName) { 107 'use strict'; 108 // parse arguments 109 var args = this._checkAddProject(projectId, projectName); 110 // add project 111 if (this._prj['prj_' + args.projectId] === undefined) { 112 this.addProjectIfNotExists(args.projectId, args.projectName); 113 } else { 114 this._throwError('PROJECT_ALREADY_EXISTS', args.projectId); 115 return; 116 } 117 }, 118 119 /** 120 * Add project if not exists 121 * 122 * @public 123 * @param {string} projectId Project identifier 124 * @param {string} [projectName] Project name 125 * @return {void} 126 */ 127 addProjectIfNotExists : function(projectId, projectName) { 128 'use strict'; 129 // parse arguments 130 var args = this._checkAddProject(projectId, projectName); 131 // add project 132 if (this._prj['prj_' + args.projectId] === undefined) { 133 this._prj['prj_' + args.projectId] = { 134 projectName : args.projectName 135 }; 136 } 137 }, 138 139 /** 140 * Get HTML selector for tab 141 * 142 * @public 143 * @param {string} projectId Project identifier 144 * @param {number} tabId Tab identifier 145 * @param {string} formId Form identifier 146 * @return {string} HTML selector for tab 147 */ 148 getHTMLSelectorTab : function(projectId, formId, tabId) { 149 'use strict'; 150 // parse arguments 151 projectId = this._parseProjectId(projectId); 152 formId = this._objHelper.initString(formId); 153 tabId = this._objHelper.initNumeric(tabId); 154 // exit 155 return '#' + this._getHTMLNameTab(projectId, formId, tabId); 156 }, 157 158 /** 159 * Get HTML selector for tab div 160 * 161 * @public 162 * @param {string} projectId Project identifier 163 * @param {string} formId Form identifier 164 * @param {number} tabId Tab identifier 165 * @return {string} HTML selector for tab div 166 */ 167 getHTMLSelectorTabDiv : function(projectId, formId, tabId) { 168 'use strict'; 169 // parse arguments 170 projectId = this._parseProjectId(projectId); 171 formId = this._objHelper.initString(formId); 172 tabId = this._objHelper.initNumeric(tabId); 173 // exit 174 return '#' + this._getHTMLNameTabDiv(projectId, formId, tabId); 175 }, 176 177 /** 178 * Process form tabs 179 * 180 * @public 181 * @param {string} projectId Project identifier 182 * @param {string} formId Form identifier 183 * @param {number} tabsCount Tabs count 184 * @return {void} 185 */ 186 process : function(projectId, formId, tabsCount) { 187 'use strict'; 188 // get this object 189 var self = this; 190 (function($) { 191 // parse parameters 192 projectId = self._parseProjectId(projectId); 193 formId = self._objHelper.initString(formId); 194 tabsCount = self._objHelper.initNumeric(tabsCount); 195 // activate first tab 196 self._eventTabClick(projectId, formId, 0, tabsCount); 197 // add events 198 for (var z=0; z<tabsCount; z++) { 199 $(self.getHTMLSelectorTab(projectId, formId, z)).bind('click', { 200 projectId : projectId, 201 formId : formId, 202 pos : z, 203 tabsCount : tabsCount 204 }, function(event) { 205 event.preventDefault(); 206 self._eventTabClick(event.data.projectId, event.data.formId, event.data.pos, event.data.tabsCount); 207 }); 208 } 209 }(jQuery)); 210 }, 211 212 /** 213 * Get HTML prefix 214 * 215 * @private 216 * @param {string} projectId Project identifier 217 * @return {string} HTML prefix 218 */ 219 _getHTMLPrefix : function(projectId) { 220 'use strict'; 221 // exit 222 return this._valsPrefix + '_' + projectId + '__'; 223 }, 224 225 /** 226 * Get HTML tab name 227 * 228 * @private 229 * @param {string} projectId Project identifier 230 * @param {string} formId Form identifier 231 * @param {number} tabId Tab identifier 232 * @return {string} HTML tab name 233 */ 234 _getHTMLNameTab : function(projectId, formId, tabId) { 235 'use strict'; 236 // exit 237 return this._getHTMLPrefix(projectId) + 'form__' + formId + '__tab__' + tabId; 238 }, 239 240 /** 241 * Get HTML tab div name 242 * 243 * @private 244 * @param {string} projectId Project identifier 245 * @param {string} formId Form identifier 246 * @param {number} tabId Tab identifier 247 * @return {string} HTML tab div name 248 */ 249 _getHTMLNameTabDiv : function(projectId, formId, tabId) { 250 'use strict'; 251 // exit 252 return this._getHTMLPrefix(projectId) + 'form__' + formId + '__tab_div__' + tabId; 253 }, 254 255 /** 256 * Parse project identifier 257 * 258 * @private 259 * @param {string} projectId Project identifier 260 * @return {string} Parsed project identifier 261 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.EMPTY_PROJECT_ID if project identifier entered in projectId is empty 262 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.PROJECT_DOES_NOT_EXIST if project identifier entered in projectId does not exist 263 */ 264 _parseProjectId : function(projectId) { 265 'use strict'; 266 // parse project identifier 267 projectId = this._objHelper.initString(projectId); 268 if (projectId === '') { 269 this._throwError('EMPTY_PROJECT_ID'); 270 return; 271 } 272 // check if project exists 273 if (this._prj['prj_' + projectId] === undefined) { 274 this._throwError('PROJECT_DOES_NOT_EXIST', projectId); 275 return; 276 } 277 // exit 278 return projectId; 279 }, 280 281 /** 282 * Check arguments for adding project 283 * 284 * @private 285 * @param {string} projectId Project identifier 286 * @param {string} [projectName] Project name 287 * @return {Object} Parsed arguments for adding project 288 * @throws {kocujILV12aCException} kocujILV12aExceptionCode.EMPTY_PROJECT_ID if project identifier entered in projectId is empty 289 */ 290 _checkAddProject : function(projectId, projectName) { 291 'use strict'; 292 // parse arguments 293 projectId = this._objHelper.initString(projectId); 294 if (projectId === '') { 295 this._throwError('EMPTY_PROJECT_ID'); 296 return; 297 } 298 projectName = this._objHelper.initString(projectName); 299 // exit 300 return { 301 projectId : projectId, 302 projectName : projectName 303 }; 304 }, 305 306 /** 307 * Throw an error if debugging is enabled 308 * 309 * @private 310 * @param {string} codeString Error code in string format 311 * @param {string} [param] Parameter for error information 312 * @return {void} 313 */ 314 _throwError : function(codeString, param) { 315 'use strict'; 316 // parse arguments 317 codeString = this._objHelper.initString(codeString); 318 if (codeString === '') { 319 return; 320 } 321 param = this._objHelper.initString(param); 322 // throw an error 323 if (this._valsThrowErrors) { 324 /* jshint evil: true */ 325 eval('throw new kocujILV12aCException(kocujILV12aExceptionCode.' + codeString + ', this._thisFilename, param);'); 326 } 327 }, 328 329 /** 330 * Event for clicking on tab 331 * 332 * @private 333 * @param {string} projectId Project identifier 334 * @param {string} formId Form identifier 335 * @param {number} tabId Tab identifier 336 * @param {number} tabsCount Tabs count 337 * @return {void} 338 */ 339 _eventTabClick : function(projectId, formId, tabId, tabsCount) { 340 'use strict'; 341 // get this object 342 var self = this; 343 (function($) { 344 // change all tabs to no selected 345 for (var z=0; z<tabsCount; z++) { 346 if (z !== tabId) { 347 $(self.getHTMLSelectorTab(projectId, formId, z)).attr('class', 'nav-tab'); 348 $(self.getHTMLSelectorTabDiv(projectId, formId, z)).css('visibility', 'hidden'); 349 $(self.getHTMLSelectorTabDiv(projectId, formId, z)).css('position', 'absolute'); 350 } 351 } 352 // set selected tab as active 353 $(self.getHTMLSelectorTab(projectId, formId, tabId)).attr('class', 'nav-tab nav-tab-active'); 354 $(self.getHTMLSelectorTabDiv(projectId, formId, tabId)).css('visibility', 'visible'); 355 $(self.getHTMLSelectorTabDiv(projectId, formId, tabId)).css('position', 'static'); 356 }(jQuery)); 357 } 358 }; 359 360 // initialize 361 var kocujILV12aBackendSettingsFormTabs = new kocujILV12aCBackendSettingsFormTabs(); 362