lib.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. const CheckDecimal = x => {
  2. const realStringObj = obj && obj.toString();
  3. if (!jQuery.isArray(obj) && (realStringObj - parseFloat(realStringObj) + 1) >= 0) return false;
  4. const parts = x.toString().split(".");
  5. parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  6. return parts.join(".");
  7. }
  8. const CheckMail = email => {
  9. const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  10. const result = re.test(String(email).toLowerCase());
  11. // !result && alert('Email is invalid !!!', 'error');
  12. return result;
  13. }
  14. const CheckNum = input => !isNaN(input);
  15. const LenCheck = (input, length) => {
  16. return input.length <= length;
  17. }
  18. const CheckMinMax = (elm, min, max) => {
  19. const length = $(elm).val().length;
  20. if (length < min || length > max) {
  21. $(elm).empty();
  22. $(elm).focus();
  23. }
  24. }
  25. function isEmptyArr(arr) {
  26. if(Array.isArray(arr) && arr.length === 0) {
  27. return true;
  28. }
  29. return false;
  30. }
  31. const CheckSSN = ssn => {
  32. const ssnPattern = /^[0-9]{3}\-?[0-9]{2}\-?[0-9]{4}$/;
  33. return ssnPattern.test(ssn);
  34. }
  35. const CheckRequired = inputList => {
  36. inputList.forEach(item => {
  37. const value = item.val();
  38. if (value == null || value == '' || value == undefined) {
  39. alert(`${item.data('name')} is required !!!`, 'error');
  40. return false;
  41. }
  42. })
  43. };
  44. const ComfirmPasswd = (pass, confirmPass) => pass === confirmPass;
  45. const DisableInput = elm => $(elm).prop('disabled', function (i, v) {
  46. return !v;
  47. });
  48. const BlinkObject = elm => {
  49. $(elm).fadeOut('slow', function () {
  50. $(this).fadeIn('slow', function () {
  51. BlinkObject(this);
  52. });
  53. });
  54. }
  55. const InitCheckBox = (data, name) => {
  56. let html = '';
  57. data.forEach(item => {
  58. html += `<input type="checkbox" name="${name}" value="${item}"> ${item}`;
  59. });
  60. return html;
  61. }
  62. const InitRadio = (data, name) => {
  63. let html = '';
  64. data.forEach(item => {
  65. html += `<input type="radio" name="${name}" value="${item}"> ${item}`;
  66. });
  67. return html;
  68. }
  69. const InitSelect = (data, name) => `<select name="${name}">${ArrayToOption(data)}</select>`;
  70. const ArrayToOption = array => {
  71. let html = '';
  72. array.forEach(item => {
  73. html += `<option value="${item}">${item}</option>`
  74. });
  75. return html;
  76. }
  77. function commaCheck($this) {
  78. $($this).val(function (index, value) {
  79. value = value.replace(/,/g, '');
  80. return numberWithCommas(value);
  81. });
  82. }
  83. function minusComma(value) {
  84. let formNumber = "" + value;
  85. // 문자제거
  86. // value = formNumber.toString().replace(/[^\d]+/g, "");
  87. // 콤마제거
  88. value = formNumber.toString().replace(/,/g, "");
  89. return value;
  90. }
  91. function rand(min, max) {
  92. return Math.floor(Math.random() * (max - min)) + min;
  93. }
  94. function format_decimal(val, number) {
  95. let format = decimal_convert(number);
  96. return numeral(parseFloat(val).toFixed(number)).format(format);
  97. }
  98. function fill_zero(width, str) {
  99. return (str.length && width > 0) ? str + '.' + new Array(width + 1).join('0') : str;
  100. }
  101. function decimal_convert(point_name) {
  102. return fill_zero(parseInt(point_name), '0,0')
  103. }
  104. function remove_tag( html ) {
  105. return html.replace(/(<([^>]+)>)/gi, "");
  106. }
  107. function is_localhost() {
  108. if (location.hostname === 'localhost' || location.hostname === '127.0.0.1') {
  109. return true
  110. }
  111. return false
  112. }
  113. function get_clusterize_rows_in_block() {
  114. return 10
  115. // return is_localhost() ? 10 : 10
  116. }
  117. function remove_comma_and_arithmetic(a, b, type) {
  118. switch (type) {
  119. case 'plus':
  120. return Number(minusComma(a)) + Number(minusComma(b))
  121. case 'minus':
  122. return Number(minusComma(a)) - Number(minusComma(b))
  123. }
  124. }
  125. function format_conver_for(data, format, display_vars = undefined) {
  126. let result = data;
  127. switch (format) {
  128. case 'YY-MM-DD': case 'YY.MM.DD': case 'YYMMDD':
  129. case 'yy-mm-dd': case 'yy.mm.dd': case 'yymmdd':
  130. result = isEmpty(data) ? '' : moment(data).format(format.toUpperCase());
  131. break;
  132. case 'unixtime':
  133. result = isEmpty(data) ? '' : moment(new Date(data * 1000)).format('YYYY-MM-DD HH:mm');
  134. break;
  135. case 'unix-yyyy.mm.dd':
  136. result = isEmpty(data) ? '' : moment(new Date(data * 1000)).format('YYYY-MM-DD');
  137. break;
  138. case '$_ThumbNail':
  139. result = `<img src="${window.env['MEDIA_URL'] + data}" width="${display_vars['ListWidth']}" height="${display_vars['ListHeight']}" style="max-width: 100%;" onerror="this.src='/images/folder.jpg'"`;
  140. break;
  141. case '$_FileName':
  142. let path_list = data.split('/')
  143. result = path_list[path_list.length - 1];
  144. break;
  145. case 'check':
  146. if (data == 1 || data == true) {
  147. result = '√';
  148. } else {
  149. result = '';
  150. }
  151. break;
  152. case 'unique':
  153. result = data.split("'").join('');
  154. break;
  155. case 'date_month':
  156. result = isEmpty(data) ? '' : moment(data).format('YY.MM');
  157. break;
  158. case 'date_week':
  159. result = isEmpty(data) ? '' : `${moment(data).format('YY.MM')}-${moment(data).isoWeek()}`;
  160. break;
  161. case 'remove_tag':
  162. result = remove_tag(data);
  163. break;
  164. default:
  165. if (! isEmpty(format) && format.match(/[A-Za-z]+\s*\(\s*'(.*?)\'\s*\)\s*/)) {
  166. const func_name = format.replace(')', `, '${data}')`);
  167. try {
  168. result = eval(`format_func_${func_name}`);
  169. } catch (err) {
  170. result = 'Invalid';
  171. }
  172. }
  173. break;
  174. }
  175. return result;
  176. }
  177. function format_func_decimal(...argv) {
  178. const format = capitalize(camelCase(argv[0])) + 'Point';
  179. let data;
  180. if (window.User[format] == undefined) {
  181. return 'Invalid';
  182. }
  183. switch (argv.length) {
  184. case 2:
  185. data = format_decimal(argv[1], window.User[format]);
  186. break;
  187. case 3:
  188. data = format_decimal(argv[2], window.User[format])
  189. if (argv[1] === 'nz' && (isEmpty(data) || data == 0)) { data = 0; }
  190. else if (argv[1] === 'zn' && (isEmpty(data) || data == 0)) { data = ''; }
  191. break;
  192. }
  193. return data;
  194. }
  195. function format_func_status_update(value, data) {
  196. return { Field: 'Status', Value: format_func_status_rev(value, data), };
  197. }
  198. function format_func_status_rev(value, data) {
  199. const status = Object.values(window.CodeTitle['status'][value]).filter(status => status.Title == data)
  200. if (isEmptyArr(status)) { return data; }
  201. return _.first(status)['Code'];
  202. }
  203. function format_func_status(value, data) {
  204. if (window.CodeTitle['status'] && window.CodeTitle['status'][value][data]) {
  205. return window.CodeTitle['status'][value][data]['Title'];
  206. }
  207. return 'Invalid';
  208. }
  209. function format_func_sort_update(value, data) {
  210. return { Field: 'Sort', Value: format_func_sort_rev(value, data), };
  211. }
  212. function format_func_sort_rev(value, data) {
  213. const sort = Object.values(window.CodeTitle['sort'][value]).filter(sort => sort.Title == data)
  214. if (isEmptyArr(sort)) { return data; }
  215. return _.first(sort)['Code'];
  216. }
  217. function format_func_sort(value, data) {
  218. if (window.CodeTitle['sort'] && window.CodeTitle['sort'][value][data]) {
  219. return window.CodeTitle['sort'][value][data]['Title'];
  220. }
  221. return 'Invalid';
  222. }
  223. function format_func_deal_type_update(value, data) {
  224. return { Field: 'DealType', Value: format_func_deal_type_rev(value, data), };
  225. }
  226. function format_func_deal_type_rev(value, data) {
  227. const deal_type = Object.values(window.CodeTitle['deal-type'][value]).filter(deal_type => deal_type.Title == data)
  228. if (isEmptyArr(deal_type)) { return data; }
  229. return _.first(deal_type)['Code'];
  230. }
  231. function format_func_deal_type(value, data) {
  232. if (window.CodeTitle['deal-type'] && window.CodeTitle['deal-type'][value][data]) {
  233. return window.CodeTitle['deal-type'][value][data]['Title'];
  234. }
  235. return 'Invalid';
  236. }
  237. function check_dom_input_number(dom_array) {
  238. let arr = dom_array.filter(el => isNaN($(el).val()))
  239. arr.forEach(el => $(el).val('') );
  240. if (! isEmptyArr(arr)) {
  241. iziToast.error({
  242. title: 'Error',
  243. message: $('#please-enter-a-number').text(),
  244. });
  245. return true;
  246. }
  247. return false;
  248. }
  249. function show_iziToast_msg(page, callback = undefined) {
  250. if (page) {
  251. iziToast.success({
  252. title: 'Success',
  253. message: $('#action-completed').text(),
  254. });
  255. if (! isEmpty(callback)) {
  256. callback()
  257. }
  258. } else {
  259. iziToast.error({
  260. title: 'Error',
  261. message: page.data.body ?? $('#api-request-failed-please-check').text(),
  262. });
  263. }
  264. }
  265. function token_value(balance, unit_point, decimal_point) {
  266. if (decimal_point === -1) {
  267. return balance / (10 ** unit_point)
  268. }
  269. return format_decimal(balance / (10 ** unit_point), decimal_point)
  270. }
  271. function token_value_rev(balance, unit_point) {
  272. // return balance + '0'.repeat(unit_point);
  273. return String(balance * (10 ** unit_point))
  274. }
  275. function long_number_string(n){
  276. var str, str2= '', data= n.toExponential().replace('.','').split(/e/i);
  277. str= data[0], mag= Number(data[1]);
  278. if(mag>=0 && str.length> mag){
  279. mag+=1;
  280. if (isEmpty(str.substring(mag))) { return str.substring(0, mag) }
  281. return (str.substring(0, mag)+'.'+str.substring(mag));
  282. }
  283. if(mag<0){
  284. while(++mag) str2+= '0';
  285. return '0.'+str2+str;
  286. }
  287. mag= (mag-str.length)+1;
  288. while(mag> str2.length){
  289. str2+= '0';
  290. }
  291. return (str+str2);
  292. }
  293. // 현재 value 값이 소수점 n 자리 숫자라면 더 이상 입력 X
  294. function check_number_of_decimal_places(val, n) {
  295. // const _pattern1 = /^\d*[.]\d{16}$/
  296. const _pattern1 = new RegExp(`^\\d*[.]\\d{${n}}$`, 'g')
  297. if (_pattern1.test(val)) {
  298. return false
  299. }
  300. return true
  301. }