ariaNgCommonService.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').factory('ariaNgCommonService', ['$window', '$location', '$timeout', 'base64', 'moment', 'SweetAlert', 'ariaNgConstants', 'ariaNgLocalizationService', 'ariaNgNativeElectronService', function ($window, $location, $timeout, base64, moment, SweetAlert, ariaNgConstants, ariaNgLocalizationService, ariaNgNativeElectronService) {
  4. var getTimeOption = function (time) {
  5. var name = '';
  6. var value = time;
  7. if (time < 1000) {
  8. value = time;
  9. name = (value === 1 ? 'format.time.millisecond' : 'format.time.milliseconds');
  10. } else if (time < 1000 * 60) {
  11. value = time / 1000;
  12. name = (value === 1 ? 'format.time.second' : 'format.time.seconds');
  13. } else if (time < 1000 * 60 * 24) {
  14. value = time / 1000 / 60;
  15. name = (value === 1 ? 'format.time.minute' : 'format.time.minutes');
  16. } else {
  17. value = time / 1000 / 60 / 24;
  18. name = (value === 1 ? 'format.time.hour' : 'format.time.hours');
  19. }
  20. return {
  21. name: name,
  22. value: value,
  23. optionValue: time
  24. };
  25. };
  26. var showDialog = function (title, text, type, callback, options) {
  27. $timeout(function () {
  28. ariaNgNativeElectronService.updateTitleBarBackgroundColorWithSweetAlertOverlay();
  29. SweetAlert.swal({
  30. title: title,
  31. text: text,
  32. type: type,
  33. confirmButtonText: options && options.confirmButtonText || null
  34. }, function () {
  35. ariaNgNativeElectronService.updateTitleBarBackgroundColor();
  36. if (callback) {
  37. callback();
  38. }
  39. });
  40. }, 100);
  41. };
  42. var showConfirmDialog = function (title, text, type, callback, notClose, extendSettings) {
  43. var options = {
  44. title: title,
  45. text: text,
  46. type: type,
  47. showCancelButton: true,
  48. showLoaderOnConfirm: !!notClose,
  49. closeOnConfirm: !notClose,
  50. confirmButtonText: extendSettings && extendSettings.confirmButtonText || null,
  51. cancelButtonText: extendSettings && extendSettings.cancelButtonText || null
  52. };
  53. if (type === 'warning') {
  54. options.confirmButtonColor = '#F39C12';
  55. }
  56. ariaNgNativeElectronService.updateTitleBarBackgroundColorWithSweetAlertOverlay();
  57. SweetAlert.swal(options, function (isConfirm) {
  58. ariaNgNativeElectronService.updateTitleBarBackgroundColor();
  59. if (!isConfirm) {
  60. return;
  61. }
  62. if (callback) {
  63. callback();
  64. }
  65. });
  66. }
  67. return {
  68. getFullPageUrl: function () {
  69. return $window.location.protocol + '//'
  70. + $window.location.host
  71. + $window.location.pathname
  72. + ($window.location.search ? $window.location.search : '');
  73. },
  74. base64Encode: function (value) {
  75. return base64.encode(value);
  76. },
  77. base64Decode: function (value) {
  78. return base64.decode(value);
  79. },
  80. base64UrlEncode: function (value) {
  81. return base64.urlencode(value);
  82. },
  83. base64UrlDecode: function (value) {
  84. return base64.urldecode(value);
  85. },
  86. generateUniqueId: function () {
  87. var sourceId = ariaNgConstants.appPrefix + '_' + Math.round(new Date().getTime() / 1000) + '_' + Math.random();
  88. var hashedId = this.base64Encode(sourceId);
  89. return hashedId;
  90. },
  91. showDialog: function (title, text, type, callback, extendSettings) {
  92. if (!extendSettings) {
  93. extendSettings = {};
  94. }
  95. if (title) {
  96. title = ariaNgLocalizationService.getLocalizedText(title);
  97. }
  98. if (text) {
  99. text = ariaNgLocalizationService.getLocalizedText(text, extendSettings.textParams);
  100. }
  101. extendSettings.confirmButtonText = ariaNgLocalizationService.getLocalizedText('OK');
  102. showDialog(title, text, type, callback, extendSettings);
  103. },
  104. showInfo: function (title, text, callback, extendSettings) {
  105. this.showDialog(title, text, 'info', callback, extendSettings);
  106. },
  107. showError: function (text, callback, extendSettings) {
  108. this.showDialog('Error', text, 'error', callback, extendSettings);
  109. },
  110. showOperationSucceeded: function (text, callback) {
  111. this.showDialog('Operation Succeeded', text, 'success', callback);
  112. },
  113. confirm: function (title, text, type, callback, notClose, extendSettings) {
  114. if (!extendSettings) {
  115. extendSettings = {};
  116. }
  117. if (title) {
  118. title = ariaNgLocalizationService.getLocalizedText(title);
  119. }
  120. if (text) {
  121. text = ariaNgLocalizationService.getLocalizedText(text, extendSettings.textParams);
  122. }
  123. extendSettings.confirmButtonText = ariaNgLocalizationService.getLocalizedText('OK');
  124. extendSettings.cancelButtonText = ariaNgLocalizationService.getLocalizedText('Cancel');
  125. showConfirmDialog(title, text, type, callback, notClose, extendSettings);
  126. },
  127. closeAllDialogs: function () {
  128. SweetAlert.close();
  129. },
  130. getFileExtension: function (filePath) {
  131. if (!filePath || filePath.lastIndexOf('.') < 0) {
  132. return filePath;
  133. }
  134. return filePath.substring(filePath.lastIndexOf('.'));
  135. },
  136. parseUrlsFromOriginInput: function (s) {
  137. if (!s) {
  138. return [];
  139. }
  140. var lines = s.split('\n');
  141. var result = [];
  142. for (var i = 0; i < lines.length; i++) {
  143. var line = lines[i];
  144. if (line.match(/^(http|https|ftp|sftp):\/\/.+$/)) {
  145. result.push(line);
  146. } else if (line.match(/^magnet:\?.+$/)) {
  147. result.push(line);
  148. }
  149. }
  150. return result;
  151. },
  152. decodePercentEncodedString: function (s) {
  153. if (!s) {
  154. return s;
  155. }
  156. var ret = '';
  157. for (var i = 0; i < s.length; i++) {
  158. var ch = s.charAt(i);
  159. if (ch === '%' && i < s.length - 2) {
  160. var code = s.substring(i + 1, i + 3);
  161. ret += String.fromCharCode(parseInt(code, 16));
  162. i += 2;
  163. } else {
  164. ret += ch;
  165. }
  166. }
  167. return ret;
  168. },
  169. extendArray: function (sourceArray, targetArray, keyProperty) {
  170. if (!targetArray || !sourceArray || sourceArray.length !== targetArray.length) {
  171. return false;
  172. }
  173. for (var i = 0; i < targetArray.length; i++) {
  174. if (targetArray[i][keyProperty] === sourceArray[i][keyProperty]) {
  175. angular.extend(targetArray[i], sourceArray[i]);
  176. } else {
  177. return false;
  178. }
  179. }
  180. return true;
  181. },
  182. copyObjectTo: function (from, to) {
  183. if (!to) {
  184. return from;
  185. }
  186. for (var name in from) {
  187. if (!from.hasOwnProperty(name)) {
  188. continue;
  189. }
  190. var fromValue = from[name];
  191. var toValue = to[name];
  192. if (angular.isObject(fromValue) || angular.isArray(fromValue)) {
  193. to[name] = this.copyObjectTo(from[name], to[name]);
  194. } else {
  195. if (fromValue !== toValue) {
  196. to[name] = fromValue;
  197. }
  198. }
  199. }
  200. return to;
  201. },
  202. pushArrayTo: function (array, items) {
  203. if (!angular.isArray(array)) {
  204. array = [];
  205. }
  206. if (!angular.isArray(items) || items.length < 1) {
  207. return array;
  208. }
  209. for (var i = 0; i < items.length; i++) {
  210. array.push(items[i]);
  211. }
  212. return array;
  213. },
  214. combineArray: function () {
  215. var result = [];
  216. for (var i = 0; i < arguments.length; i++) {
  217. if (angular.isArray(arguments[i])) {
  218. this.pushArrayTo(result, arguments[i]);
  219. } else {
  220. result.push(arguments[i]);
  221. }
  222. }
  223. return result;
  224. },
  225. countArray: function (array, value) {
  226. if (!angular.isArray(array) || array.length < 1) {
  227. return 0;
  228. }
  229. var count = 0;
  230. for (var i = 0; i < array.length; i++) {
  231. count += (array[i] === value ? 1 : 0);
  232. }
  233. return count;
  234. },
  235. parseOrderType: function (value) {
  236. var values = value.split(':');
  237. var obj = {
  238. type: values[0],
  239. order: values[1],
  240. equals: function (obj) {
  241. if (angular.isUndefined(obj.order)) {
  242. return this.type === obj.type;
  243. } else {
  244. return this.type === obj.type && this.order === obj.order;
  245. }
  246. },
  247. getValue: function () {
  248. return this.type + ':' + this.order;
  249. }
  250. };
  251. Object.defineProperty(obj, 'reverse', {
  252. get: function () {
  253. return this.order === 'desc';
  254. },
  255. set: function (value) {
  256. this.order = (value ? 'desc' : 'asc');
  257. }
  258. });
  259. return obj;
  260. },
  261. getCurrentUnixTime: function () {
  262. return moment().format('X');
  263. },
  264. getLongTimeFromUnixTime: function (unixTime) {
  265. return moment(unixTime, 'X').format('HH:mm:ss');
  266. },
  267. isUnixTimeAfter: function (datetime, duration, unit) {
  268. return moment(datetime, 'X').isAfter(moment().add(duration, unit));
  269. },
  270. formatDateTime: function (datetime, format) {
  271. return moment(datetime).format(format);
  272. },
  273. getTimeOption: function (time) {
  274. return getTimeOption(time);
  275. },
  276. getTimeOptions: function (timeList, withDisabled) {
  277. var options = [];
  278. if (withDisabled) {
  279. options.push({
  280. name: 'Disabled',
  281. value: 0,
  282. optionValue: 0
  283. });
  284. }
  285. if (!angular.isArray(timeList) || timeList.length < 1) {
  286. return options;
  287. }
  288. for (var i = 0; i < timeList.length; i++) {
  289. var time = timeList[i];
  290. var option = getTimeOption(time);
  291. options.push(option);
  292. }
  293. return options;
  294. }
  295. };
  296. }]);
  297. }());