PageRenderTime 23ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/assets/js/helper.js

https://bitbucket.org/anjanigupta1204/tracarta_website
JavaScript | 449 lines | 386 code | 27 blank | 36 comment | 106 complexity | 51762f15b7edb3e8c2456e041dc4e09a MD5 | raw file
  1. angular.module('tracartaApp.helperFactory', [])
  2. .factory('helperFactory', function (httpServices, $base64, $location) {
  3. var helper = {};
  4. /**
  5. * @method return site url
  6. * @param {string} path
  7. */
  8. helper.getUrl = function (path = false) {
  9. var _url = BASE_URL + '/';
  10. if (path) {
  11. _url += path;
  12. }
  13. return _url;
  14. }
  15. helper.fieldExists = function (val, arr) {
  16. var found = false;
  17. for (var i = 0; i < arr.length; i++) {
  18. if (arr[i].field == val) {
  19. found = true;
  20. break;
  21. }
  22. }
  23. return found;
  24. }
  25. helper.replaceFieldValue = function (key, val, arr) {
  26. for (var i = 0; i < arr.length; i++) {
  27. if (arr[i].field == key) {
  28. arr[i].value = val
  29. }
  30. }
  31. return arr;
  32. }
  33. helper.orderExists = function (val, arr) {
  34. var found = false;
  35. for (var i = 0; i < arr.length; i++) {
  36. if (typeof arr[i].order != 'undefined') {
  37. found = true;
  38. break;
  39. }
  40. }
  41. return found;
  42. }
  43. helper.replaceOrderValue = function (val, arr, dir) {
  44. for (var i = 0; i < arr.length; i++) {
  45. if (typeof arr[i].order != 'undefined') {
  46. arr[i].order = val;
  47. arr[i].dir = dir;
  48. break;
  49. }
  50. }
  51. return arr;
  52. }
  53. helper.getParameterByName = function (name, url) {
  54. if (!url) url = window.location.href;
  55. name = name.replace(/[\[\]]/g, "\\$&");
  56. var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
  57. results = regex.exec(url);
  58. if (!results) return null;
  59. if (!results[2]) return '';
  60. return decodeURIComponent(results[2].replace(/\+/g, " "));
  61. }
  62. /**
  63. * @method check not empty object
  64. */
  65. helper.isEmpty = function (obj) {
  66. for (var key in obj) {
  67. if (obj.hasOwnProperty(key))
  68. return false;
  69. }
  70. return true;
  71. }
  72. /**
  73. * @method Get Image upload error messages messages
  74. */
  75. helper.getImageMessages = function (file, ht = false, wd = false, sz = false) {
  76. var height = (ht) ? ht : 1200;
  77. var width = (wd) ? wd : 1600;
  78. var size = (sz) ? sz : 2;
  79. var sizeInKb = size * 1024 * 1024;
  80. if (file.type == 'image/jpeg' || file.type == 'image/png' || file.type == 'image/jpg' || file.type == 'image/gif') {
  81. if (file.size > sizeInKb) {
  82. console.log('Image should not be more than ' + size + 'MB');
  83. return false;
  84. } else if (file.$ngfHeight && file.$ngfHeight > height) {
  85. console.log('Image Height should be less than ' + height + 'px');
  86. return false;
  87. } else if (file.$ngfWidth && file.$ngfWidth > width) {
  88. console.log('Image width should be less than ' + width + 'px');
  89. return false;
  90. } else {
  91. return true;
  92. }
  93. } else {
  94. if (!file.$ngfWidth) {
  95. console.log('Only images (jpg/png/gif) are allowed');
  96. return false;
  97. } else {
  98. console.log('Only images (jpg/png/gif) are allowed');
  99. }
  100. }
  101. }
  102. /**Get request listing data */
  103. helper.getTabData = function (url, params = [], loading = true, scope) {
  104. httpServices.getRequest(url, params).then(function (data) {
  105. if (typeof (data.tab_content) != 'undefined') {
  106. scope.tabContent = data.tab_content;
  107. scope.columns = scope.tabContent.columns;
  108. scope.additional_data = data.additional_data;
  109. scope.values = scope.tabContent.data;
  110. scope.pagination = scope.tabContent.pagination;
  111. scope.showLoading = false;
  112. }
  113. }).catch(function (error) {
  114. console.error('Error: ', error);
  115. });
  116. }
  117. /**
  118. * @method Search Filters
  119. */
  120. helper.searchby = function (dbReference, data, $index, filterIndex = false, operator = false, scope) {
  121. scope.d = data;
  122. scope.dbReference = dbReference;
  123. scope.in = $index;
  124. if (filterIndex) {
  125. scope.filterIndex = filterIndex;
  126. scope.d = data + ';' + filterIndex;
  127. }
  128. clearTimeout(scope.typingTimer);
  129. if (operator) {
  130. scope.operator = '=';
  131. }
  132. scope.typingTimer = setTimeout(helper.doneTyping(scope), scope.doneTypingInterval);
  133. }
  134. /**
  135. * @method hit search data after typing completed
  136. */
  137. helper.doneTyping = function (scope) {
  138. if (!helper.fieldExists(scope.dbReference, scope.globalParams)) {
  139. scope.globalParams.push({
  140. 'field': scope.dbReference,
  141. 'value': scope.d,
  142. 'operator': scope.operator
  143. });
  144. } else {
  145. scope.globalParams = helper.replaceFieldValue(scope.dbReference, scope.d, scope.globalParams)
  146. }
  147. scope.modelFilter.name[scope.in] = scope.d.split(";")[0];
  148. var url = helper.getUrl(scope.dropdown.drop);
  149. /* AJAX call */
  150. helper.getTabData(url, helper.getQueryFilters(scope), false, scope);
  151. scope.operator = 'LIKE';
  152. }
  153. /**
  154. * @method Sorting data
  155. */
  156. helper.sortBy = function (data, filterIndex = false, scope) {
  157. if (filterIndex) {
  158. data = data + ';' + filterIndex
  159. }
  160. if (!helper.orderExists(data, scope.globalParams)) {
  161. scope.globalParams.push({ 'order': data, 'dir': scope.dir });
  162. } else {
  163. scope.globalParams = helper.replaceOrderValue(data, scope.globalParams, scope.dir);
  164. }
  165. scope.dir = !scope.dir;
  166. var url = helper.getUrl(scope.dropdown.drop);
  167. helper.getTabData(url, helper.getQueryFilters(scope), false, scope);
  168. }
  169. /**
  170. * @method global query filters
  171. */
  172. helper.getQueryFilters = function (scope) {
  173. var filters = {};
  174. var order = {}
  175. var page = 0;
  176. var params = {};
  177. if (scope.globalParams.length) {
  178. var i = 0;
  179. var j = 0;
  180. angular.forEach(scope.globalParams, function (val, key) {
  181. if (typeof val.field != 'undefined') {
  182. filters[i] = val;
  183. i++;
  184. } else if (typeof val.order != 'undefined') {
  185. order[j] = val;
  186. j++;
  187. } else if (typeof val.page != 'undefined') {
  188. page = val;
  189. }
  190. });
  191. params = { 'key': $base64.encode(JSON.stringify({ 'filter': filters, 'order': order, page: page })) };
  192. if (page > 0) {
  193. params.page = page;
  194. }
  195. }
  196. return params;
  197. }
  198. /**
  199. * @method reloadTab
  200. */
  201. helper.reloadTab = function (contentClass, key = sessionStorage.activeTab, scope) {
  202. scope.showGrid = true;
  203. scope.formData = {};
  204. scope.messages = {};
  205. scope.globalParams = [];
  206. scope.modelFilter = [];
  207. scope.dropdown.drop = null;
  208. scope.tabContent = null;
  209. scope.columns = null;
  210. scope.additional_data = null;
  211. scope.values = null;
  212. scope.pagination = null;
  213. sessionStorage.currentTabDataClass = contentClass
  214. sessionStorage.activeTab = key;
  215. if (contentClass) {
  216. scope.dropdown.drop = contentClass
  217. var url = helper.getUrl(scope.dropdown.drop);
  218. if (scope.dropdown.drop != null) {
  219. helper.getTabData(url, undefined, undefined, scope);
  220. }
  221. }
  222. }
  223. helper.initializeTabs = function (scope) {
  224. var curUrl = $location.absUrl().split('?')[0];
  225. if (typeof sessionStorage.currentUrl != 'undefined' && curUrl != sessionStorage.currentUrl) {
  226. sessionStorage.clear();
  227. }
  228. if (typeof sessionStorage.currentUrl == 'undefined') {
  229. sessionStorage.currentUrl = curUrl;
  230. }
  231. if (typeof sessionStorage.activeTab == 'undefined') {
  232. sessionStorage.activeTab = 0;
  233. }
  234. scope.activeTab = sessionStorage.activeTab;
  235. jQuery(window).load(function () {
  236. var tabIndex = scope.activeTab;
  237. var __init = jQuery('#tab-' + tabIndex + '').find('.panel-body').attr('data-tab-init');
  238. scope.dropdown.drop = __init;
  239. if (typeof sessionStorage.currentTabDataClass == 'undefined') {
  240. sessionStorage.currentTabDataClass = __init
  241. } else {
  242. scope.dropdown.drop = sessionStorage.currentTabDataClass
  243. }
  244. var url = helper.getUrl(scope.dropdown.drop);
  245. if (scope.dropdown.drop != null && typeof scope.dropdown.drop != 'undefined') {
  246. helper.getTabData(url, undefined, undefined, scope);
  247. }
  248. })
  249. jQuery(document).on('click', '.pagination li a', function (e) {
  250. e.preventDefault();
  251. var url = jQuery(this).attr('href');
  252. var page = helper.getParameterByName('page', url);
  253. scope.globalParams.push({ 'page': page })
  254. helper.getTabData(url, scope.getQueryFilters(), undefined, scope);
  255. /* AJAX call */
  256. });
  257. }
  258. helper.timeDifference = function (startDate, endDate) {
  259. var startDate = startDate;
  260. var endDate = endDate;
  261. var seconds = (new Date(endDate).getTime() - new Date(startDate).getTime()) / 1000;
  262. var hours = Math.floor(seconds / 3600);
  263. var minutes = Math.floor(seconds % 3600 / 60);
  264. var timeDiff = hours + 'h ' + minutes + 'm';
  265. return timeDiff;
  266. }
  267. helper.initTravellers = function (scope) {
  268. scope.travellers = {
  269. adults: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
  270. children: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
  271. infants: ['0', '1', '2', '3']
  272. }
  273. scope.flightCabin = ['Economy', 'Premium Economy', 'Business'];
  274. scope.flightCabinSel = 0;
  275. scope.totalPassangers = {
  276. adult: 1,
  277. children: 0,
  278. infants: 0
  279. }
  280. scope.noOfPassengers = this.sum(scope.totalPassangers);
  281. }
  282. /**
  283. *
  284. * @method sum of all values in an object
  285. */
  286. helper.sum = function (obj) {
  287. var sum = 0;
  288. for (var el in obj) {
  289. if (obj.hasOwnProperty(el)) {
  290. sum += parseFloat(obj[el]);
  291. }
  292. }
  293. return sum;
  294. }
  295. helper.totalFare = function (val1, val2) {
  296. return (val1 + val2);
  297. }
  298. helper.sumOfObject = function (object, __key) {
  299. var totalFare = {};
  300. if (!this.isEmpty(object)) {
  301. angular.forEach(object, function (val, key) {
  302. totalFare[key] = val[__key]
  303. });
  304. return this.sum(totalFare)
  305. }
  306. }
  307. helper.selectTravellers = function (e, index, opt, val, scope) {
  308. e.stopPropagation();
  309. switch (opt) {
  310. case 'adults':
  311. if ((parseInt(val) + scope.noOfPassengers - scope.totalPassangers.adult) > 9 && parseInt(val) >= scope.totalPassangers.adult) {
  312. return false;
  313. $('.errorpopup').popover('show');
  314. }
  315. scope.totalPassangers.adult = parseInt(scope.travellers.adults[index]);
  316. if (scope.totalPassangers.adult < scope.totalPassangers.infants) {
  317. scope.totalPassangers.infants = 0;
  318. }
  319. break;
  320. case 'children':
  321. if ((parseInt(val) + scope.noOfPassengers - scope.totalPassangers.children) > 9 && parseInt(val) >= scope.totalPassangers.children) {
  322. return false;
  323. }
  324. scope.totalPassangers.children = parseInt(scope.travellers.children[index]);
  325. break;
  326. case 'infants':
  327. if ((parseInt(val) + scope.noOfPassengers - scope.totalPassangers.infants) > 9 && parseInt(val) >= scope.totalPassangers.infants) {
  328. return false;
  329. }
  330. if (scope.totalPassangers.adult < parseInt(val)) {
  331. return false;
  332. }
  333. scope.totalPassangers.infants = parseInt(scope.travellers.infants[index]);
  334. break;
  335. }
  336. scope.noOfPassengers = this.sum(scope.totalPassangers);
  337. };
  338. return helper;
  339. }).filter('capitalize', function () {
  340. return function (input) {
  341. if (input != null && input.length > 1) {
  342. if (input.indexOf(' ') !== -1) {
  343. var inputPieces,
  344. i;
  345. input = input.toLowerCase();
  346. inputPieces = input.split(' ');
  347. for (i = 0; i < inputPieces.length; i++) {
  348. inputPieces[i] = capitalizeString(inputPieces[i]);
  349. }
  350. return inputPieces.toString().replace(/,/g, ' ');
  351. }
  352. else {
  353. input = input.toLowerCase();
  354. return capitalizeString(input);
  355. }
  356. } else {
  357. return input;
  358. }
  359. function capitalizeString(inputString) {
  360. return inputString.substring(0, 1).toUpperCase() + inputString.substring(1);
  361. }
  362. };
  363. }).filter('checkExistence', function () {
  364. return function (input) {
  365. if (input == null) {
  366. return 'N.A';
  367. } else {
  368. return input;
  369. }
  370. }
  371. })
  372. .filter('to_trusted', ['$sce', function ($sce) {
  373. return function (input) {
  374. if (input != null && input.length > 1) {
  375. return $sce.trustAsHtml(input);
  376. } else {
  377. return input;
  378. }
  379. };
  380. }]).filter('setIsActiveLabel', function () {
  381. return function (input) {
  382. if (input == 'is_active') {
  383. return 'Status';
  384. } else {
  385. return input
  386. }
  387. }
  388. }).filter('setStatusLabel', function () {
  389. return function (input, key = 'is_active') {
  390. if (key == 'is_active') {
  391. return (input > 0) ? 'Enabled' : 'Disabled'
  392. } else {
  393. return input
  394. }
  395. }
  396. }).filter('mediaUrl', function (helperFactory) {
  397. return function (input) {
  398. return helperFactory.getUrl('media/' + input);
  399. }
  400. }).filter('dateToISO', function () {
  401. return function (input) {
  402. return new Date(input).toISOString();
  403. };
  404. }).filter('myDateFilter', function () {
  405. return function (input) {
  406. console.log(input)
  407. // set minutes to seconds
  408. var seconds = input * 60
  409. // calculate (and subtract) whole days
  410. var days = Math.floor(seconds / 86400);
  411. seconds -= days * 86400;
  412. // calculate (and subtract) whole hours
  413. var hours = Math.floor(seconds / 3600) % 24;
  414. seconds -= hours * 3600;
  415. // calculate (and subtract) whole minutes
  416. var minutes = Math.floor(seconds / 60) % 60;
  417. var __days = (days) ? days + 'd ' : '';
  418. var __hrs = (hours) ? hours + 'h ' : '';
  419. var __mins = (minutes) ? minutes + 'm ' : '';
  420. return __days + __hrs + __mins;
  421. }
  422. });