util.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. var iwbfuns;
  2. function GetFuns(ele) {
  3. var $table = typeof ele === 'string' ? $('#' + ele) : $(ele);
  4. return $table.iwbTable('getFuns');
  5. }
  6. function LoadTableBackFun(option) {
  7. if (!option) {
  8. option = { table: "table" };
  9. }
  10. option.table = option.table ? option.table : "table";
  11. var $table = typeof option.table === 'string' ? $('#' + option.table) : $(option.table);
  12. if ($table.length < 1) {
  13. console.log('没有发现表格:', option.table);
  14. }
  15. option.lang = option.lang ? option.lang : window.lang;
  16. $table.iwbTable(option);
  17. var funs = GetFuns($table);
  18. return funs;
  19. }
  20. function LoadTable(option) {
  21. if (!option) {
  22. option = { table: "table" };
  23. }
  24. option.table = option.table ? option.table : "table";
  25. var $table = typeof option.table === 'string' ? $('#' + option.table) : $(option.table);
  26. if ($table.length < 1) {
  27. console.log('没有发现表格:', option.table);
  28. }
  29. $table.iwbTable(option);
  30. window.iwbfuns = GetFuns($table);
  31. return $table;
  32. }
  33. function LoadTreeTable(option) {
  34. var $table = !option.table ? $('#table') : typeof option.table === 'string' ? $('#' + option.table) : $(option.table);
  35. if ($table.length < 1) {
  36. console.log('没有发现表格:', option.table);
  37. throw "没有发现表格";
  38. }
  39. $.metPageCss('/Content/Libs/bootstrap-table/extensions/treegrid/jquery.treegrid.min.css', "dy-css-treegrid");
  40. $.metPageJs('/Content/Libs/bootstrap-table/extensions/treegrid/bootstrap-table-treegrid.js', "dy-js-bootstrap-treegrid");
  41. $.metPageJs('/Content/Libs/bootstrap-table/extensions/treegrid/jquery.treegrid.min.js', "dy-js-treegrid");
  42. var defaultOption = {
  43. table: $table,
  44. rootNo: 0,
  45. idField: 'no',
  46. treeShowField: 'name',
  47. parentIdField: 'parentNo',
  48. level: "depth",
  49. customDataField: ['path'],
  50. height: $(window).height() - 200,
  51. onLoadSuccess: function () {
  52. $table.treegrid({
  53. treeColumn: 1,
  54. expanderExpandedClass: 'mr-2 iconfont icon-folderopen-fill',
  55. expanderCollapsedClass: 'mr-2 iconfont icon-folder-fill'
  56. });
  57. $(".level-2").closest("tr").treegrid("collapse");
  58. $("td span.treegrid-expander:not(.iconfont)").addClass("mr-2 iconfont icon-folder");
  59. abp.ui.clearBusy();
  60. $("td").off("dblclick").on("dblclick", function () { $($(this).closest("tr")).treegrid("toggle"); });
  61. }
  62. };
  63. option = $.extend({}, defaultOption, option);
  64. $table.iwbTable(option);
  65. $(".btn-toolbar .btn-group").find(".btn[data-type]").each(function (i, e) {
  66. var btnType = $(e).data("type").replace("_", "");
  67. if (btnType !== "btnRefresh") {
  68. var btnUrl = $(e).data("url") || "";
  69. var btnName = $(e).text();
  70. var btnClass = $(e).attr("class");
  71. var btnIcon = $(e).find("i").attr("class");
  72. actions.push({ type: btnType, name: btnName, "class": btnClass, icon: btnIcon, url: btnUrl });
  73. $(e).remove();
  74. } else {
  75. $(e).prop("disabled", false).attr("onclick", "Refresh('" + $(e).data("url") + "')");
  76. }
  77. });
  78. //$(".btn-toolbar").css("display", "block");
  79. var funs = GetFuns($table);
  80. var getCustomDataStr = function (row) {
  81. var str = "";
  82. if (option.customDataField && option.customDataField.length > 0) {
  83. for (var i = 0; i < option.customDataField.length; i++) {
  84. var key = option.customDataField[i];
  85. str += ',"' + key + '":"' + row[key] + '"';
  86. }
  87. }
  88. return str;
  89. };
  90. funs['btnCreate'] = function (url, id) {
  91. var row;
  92. if (typeof id === "string" && id !== "") {
  93. row = $table.bootstrapTable("getRowByUniqueId", id);
  94. }
  95. if (row) {
  96. var dataStr = '{"' + option.parentIdField + '":"' + row[option.idField] + '","' + option.level + '":"' + (row[option.level] + 1) + '"' + getCustomDataStr(row) + '}';
  97. console.log('create', dataStr);
  98. BtnCreate({
  99. table: $table,
  100. url: url,
  101. data: JSON.parse(dataStr),
  102. disabled: option.parentIdField
  103. });
  104. } else
  105. abp.message.warn(abp.localization.iwbZero('SelectRecordOperation'));
  106. };
  107. funs['btnUpdate'] = function (url, id) {
  108. console.log('update', id);
  109. var row;
  110. if (typeof id === "string" && id !== "") {
  111. row = $table.bootstrapTable("getRowByUniqueId", id);
  112. }
  113. if (row) {
  114. BtnUpdate({
  115. table: $table,
  116. url: url,
  117. data: row,
  118. disabled: option.parentIdField + ',' + option.idField
  119. }, row);
  120. } else
  121. abp.message.warn(abp.localization.iwbZero('SelectRecordOperation'));
  122. };
  123. funs['btnDelete'] = function (url, id) {
  124. console.log('delete', id);
  125. var row;
  126. if (typeof id === "string" && id !== "") {
  127. row = $table.bootstrapTable("getRowByUniqueId", id);
  128. }
  129. if (row) {
  130. BtnDelete({
  131. table: $table,
  132. url: url,
  133. data: row
  134. }, row);
  135. } else
  136. abp.message.warn(abp.localization.iwbZero('SelectRecordOperation'));
  137. };
  138. funs['btnMoveUp'] = function (url, id, that) {
  139. console.log("MoveUp", id);
  140. var row;
  141. if (typeof id === "string" && id !== "") {
  142. row = $table.bootstrapTable("getRowByUniqueId", id);
  143. }
  144. if (row) {
  145. var tr = $(that).closest("tr");
  146. var funId = tr.data("uniqueid"), parentNo = tr.data("parent");
  147. var prevs = tr.prevAll().filter("[data-parent='" + parentNo + "']");
  148. if (prevs.length > 0) {
  149. var prevId = prevs.first().data("uniqueid");
  150. //console.log(prevId);
  151. $.iwbAjax1({ url: url, data: { Id: funId, PrevId: prevId } });
  152. } else {
  153. abp.message.warn(abp.localization.iwbZero("RecordAtTop"));
  154. }
  155. } else {
  156. abp.message.warn(abp.localization.iwbZero('SelectRecordOperation'));
  157. }
  158. };
  159. funs['btnMoveDown'] = function (url, id, that) {
  160. console.log("MoveDown", id);
  161. var row;
  162. if (typeof id === "string" && id !== "") {
  163. row = $table.bootstrapTable("getRowByUniqueId", id);
  164. }
  165. if (row) {
  166. var tr = $(that).closest("tr");
  167. var funId = $(tr).data("uniqueid"), parentNo = $(tr).data("parent");
  168. var nexts = tr.nextAll().filter("[data-parent='" + parentNo + "']");
  169. if (nexts.length > 0) {
  170. var nextId = nexts.first().data("uniqueid");
  171. //console.log(nextId);
  172. $.iwbAjax1({ url: url, data: { Id: funId, NextId: nextId } });
  173. } else {
  174. abp.message.warn(abp.localization.iwbZero("RecordAtBottom"));
  175. }
  176. } else {
  177. abp.message.warn(abp.localization.iwbZero("SelectRecordOperation"));
  178. }
  179. };
  180. funs['Refresh'] = function (url) {
  181. console.log("Refresh");
  182. $.iwbAjax1({ url: url });
  183. };
  184. return funs;
  185. }
  186. function RefreshTable(ele, isForce) {
  187. ele = ele || "table";
  188. isForce = isForce === undefined;
  189. var $table = typeof ele === 'string' ? $('#' + ele) : $(ele);
  190. if ($table.length < 1) {
  191. console.log('没有发现表格:', ele);
  192. }
  193. try {
  194. $table.iwbTable('refresh', isForce);
  195. } catch (e) {
  196. console.log("RefreshTable", e);
  197. }
  198. }
  199. function BtnCreate(option) {
  200. option = option || {};
  201. option.table = option.table ? option.table : "table";
  202. var $table = typeof option.table === 'string' ? $('#' + option.table) : $(option.table);
  203. var url = option.url || $table.find('.btn-toolbar').find('.btn[data-type="_btnCreate"]').data('url') || $table.find('.btn-toolbar').find('.btn[data-type="btnCreate"]').data('url');
  204. option.type = 'post';
  205. $table.iwbTable('defaultCreate', url, option);
  206. }
  207. function BtnUpdate(option, row) {
  208. option = option || {};
  209. option.table = option.table ? option.table : "table";
  210. var $table = typeof option.table === 'string' ? $('#' + option.table) : $(option.table);
  211. var url = option.url || $table.find('.btn-toolbar').find('.btn[data-type="btnUpdate"]').data('url');
  212. row = row || option.row || $table.bootstrapTable("getSelections")[0];
  213. option.data = option.data || row;
  214. option.type = 'post';
  215. $table.iwbTable('defaultUpdate', url, option, row);
  216. }
  217. function BtnDelete(option, row) {
  218. option = option || {};
  219. option.table = option.table ? option.table : "table";
  220. var $table = typeof option.table === 'string' ? $('#' + option.table) : $(option.table);
  221. var url = option.url || $table.find('.btn-toolbar').find('.btn[data-type="btnDelete"]').data('url');
  222. row = row || option.row || $table.bootstrapTable("getSelections")[0];
  223. option.type = 'post';
  224. $table.iwbTable('defaultDelete', url, option, row);
  225. }
  226. function BtnConfirm(message, title, url, rowOrTableId, data) {
  227. var row;
  228. if (rowOrTableId) {
  229. if (typeof rowOrTableId === 'object' && !(rowOrTableId instanceof jQuery)) {
  230. row = rowOrTableId;
  231. } else {
  232. var $table = typeof rowOrTableId === 'string' ? $('#' + rowOrTableId) : $(rowOrTableId);
  233. row = $table.bootstrapTable("getSelections")[0];
  234. }
  235. } else {
  236. row = $("#table").bootstrapTable("getSelections")[0];
  237. }
  238. if (row) {
  239. data = data || { Id: row.id };
  240. MsgConfirm(message, title, function () {
  241. $.iwbAjax({ url: url, data: data, isValidate: false });
  242. });
  243. } else
  244. abp.message.warn(abp.localization.iwbZero('SelectRecordOperation'));
  245. }
  246. function MsgConfirm(message, title, callback, opt) {
  247. abp.message.confirm(message, title, function (isConfirmed) {
  248. if (isConfirmed && callback) {
  249. callback(opt);
  250. }
  251. });
  252. }
  253. function OpenModal(opt) {
  254. opt = opt || {};
  255. var ele = opt.modal ? opt.modal : "modal";
  256. var $modal = typeof ele === 'string' ? $('#' + ele) : $(ele);
  257. if ($modal.length < 1) {
  258. console.log('没有发现模态框:', ele);
  259. }
  260. try {
  261. $modal.iwbModal(opt);
  262. } catch (e) {
  263. console.log("OpenModal", e);
  264. }
  265. }
  266. function ShowModal(ele) {
  267. ele = ele || "modal";
  268. var $modal = typeof ele === 'string' ? $('#' + ele) : $(ele);
  269. if ($modal.length < 1) {
  270. console.log('没有发现模态框:', ele);
  271. }
  272. try {
  273. $modal.iwbModal('show');
  274. } catch (e) {
  275. console.log("ShowModal", e);
  276. }
  277. }
  278. var queryModalTarget = '';
  279. function ShowQueryModal(ele, target) {
  280. ele = ele || "modal";
  281. queryModalTarget = !target ? '' : target.indexOf('.') === 0 ? target : target.indexOf('#') === 0 ? target : '#' + target;
  282. var $modal = typeof ele === 'string' ? $('#' + ele) : $(ele);
  283. if ($modal.length < 1) {
  284. console.log('没有发现模态框:', ele);
  285. }
  286. try {
  287. $modal.iwbModal('show2');
  288. } catch (e) {
  289. console.log("ShowModal", e);
  290. }
  291. }
  292. function ClearSearchValue(target, clear, that) {
  293. var $target = $(!target ? $(that) : target.indexOf('.') === 0 ? target : target.indexOf('#') === 0 ? target : '#' + target);
  294. var arr = clear.split(",");
  295. arr.forEach(function (v) {
  296. if (v) {
  297. var vStr = v.indexOf('.') === 0 ? v : v.indexOf('#') === 0 ? v : '#' + v;
  298. $target.find(vStr).val('');
  299. }
  300. });
  301. }
  302. function FileUpload(that, opt) { $(that).iwbFileUpload(opt); }
  303. function FileClear(that) { $(that).iwbFileUpload('clearFile'); }
  304. function FileShow(that, opt) { $(that).iwbFileShow(opt); }
  305. function AddDynamicBox(that) {
  306. var $that = $(that).closest('.iwb-dynamic-box'), html = $that.clone().prop("outerHTML");
  307. html = html.replace('btn-add', 'btn-delete');
  308. html = html.replace('AddDynamicBox', 'DeleteDynamicBox');
  309. html = html.replace('增加', '删除');
  310. html = html.replace(new RegExp('data-select2-id="[0-9,a-z,A-Z,\-,_]*"', "g"), '');
  311. var $html = $(html).addClass('iwb-dynamic-box-delete').removeAttr('data-select2-id');
  312. $html.find('.form-control').each(function () {
  313. $(this).attr('id', new Date().getTime());
  314. });
  315. $html.find('.select2').remove();
  316. $html.find('input').val('');
  317. $html.find('select:not(.tree)').val('').select2();
  318. $html.find('select.tree').val('').select2tree();
  319. $that.parent().append($html);
  320. return $html;
  321. }
  322. function DeleteDynamicBox(that) {
  323. var $that = $(that).closest('.iwb-dynamic-box');
  324. $that.remove();
  325. }
  326. function OverlayScrollbar(that, opt) {
  327. var option = $.extend(true,
  328. {},
  329. {
  330. className: "os-theme-thin-dark",
  331. resize: "n", //[ "none", "both", "horizontal", "vertical" ]shorthand: [ "n", "b", "h", "v" ]
  332. normalizeRTL: true, //是否应规范化RTL滚动。
  333. sizeAutoCapable: true,
  334. clipAlways: true,
  335. paddingAbsolute: false,
  336. overflowBehavior: {
  337. x: "v-h", //[ "hidden", "scroll", "visible-hidden", "visible-scroll" ]shorthand: [ "h", "s", "v-h", "v-s" ]
  338. y: "s"
  339. },
  340. scrollbars: {
  341. dragScrolling: true,
  342. clickScrolling: true,
  343. visibility: "a", //[ "visible", "hidden", "auto" ]shorthand: [ "v", "h", "a" ]
  344. autoHide: "n", //[ "never", "scroll", "leave", "move" ]shorthand: [ "n", "s", "l", "m" ]
  345. autoHideDelay: 800
  346. }
  347. },
  348. opt || {});
  349. return $(that).overlayScrollbars(option);
  350. }
  351. /*ajax全局设置*/
  352. $.ajaxSetup({
  353. type: 'Post',
  354. timeout: 1000 * 60 * 5,
  355. //contentType: "application/x-www-form-urlencoded;charset=utf-8",
  356. contentType: "application/json",
  357. dataType: "json",
  358. error: function () {
  359. abp.ui.clearBusy();
  360. },
  361. complete: function (xmlHttpRequest, textStatus) {
  362. //console.log("Complete - " + textStatus, xmlHttpRequest);
  363. abp.ui.clearBusy();
  364. if (textStatus === "timeout") {
  365. //abp.message.error(abp.localization.iwbZero('OpTimeout'));
  366. console.log("Complete - 操作超时");
  367. } else if (textStatus !== "success") {
  368. console.log("Complete - " + textStatus, xmlHttpRequest.responseJSON);
  369. if (xmlHttpRequest.responseJSON) {
  370. //var json = xmlHttpRequest.responseJSON;
  371. } else if (xmlHttpRequest.responseText === "") {
  372. abp.message.error(abp.localization.iwbZero('OpServerError')).done(function () {
  373. top.location.reload();
  374. //top.location.href = "/Account/Login/?ReturnUrl=%2F";
  375. });
  376. }
  377. } else {
  378. var result = xmlHttpRequest.responseJSON;
  379. if (result && !result.success && result.error && result.error.message.indexOf('登陆超时') > -1) {
  380. top.location.reload();
  381. }
  382. }
  383. }
  384. });
  385. // ReSharper disable once NativeTypePrototypeExtending
  386. String.prototype.format = function (args) {
  387. var result = this;
  388. if (arguments.length > 0) {
  389. var reg;
  390. if (arguments.length === 1 && typeof args === "object") {
  391. for (var key in args) {
  392. if (args.hasOwnProperty(key)) {
  393. if (args[key] !== undefined) {
  394. reg = new RegExp("({" + key + "})", "g");
  395. result = result.replace(reg, args[key]);
  396. }
  397. }
  398. }
  399. }
  400. else {
  401. for (var i = 0; i < arguments.length; i++) {
  402. if (arguments[i] !== undefined) {
  403. reg = new RegExp("({[" + i + "]})", "g");
  404. result = result.replace(reg, arguments[i]);
  405. }
  406. }
  407. }
  408. }
  409. return result;
  410. };
  411. String.format = function (str) {
  412. var args = arguments, re = new RegExp("%([1-" + args.length + "])", "g");
  413. return String(str).replace(re, function ($1, $2) {
  414. return args[$2];
  415. }
  416. );
  417. };
  418. //调用方法很简单:
  419. //element.innerHTML = String.format('<a href="%1" onclick="alert(\’%2\’);">%3</a>', url, msg, text);
  420. // 对Date的扩展,将 Date 转化为指定格式的String
  421. // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
  422. // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  423. // 例子:
  424. // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
  425. // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
  426. // ReSharper disable once NativeTypePrototypeExtending
  427. Date.prototype.format = function (fmt) {
  428. var o = {
  429. "M+": this.getMonth() + 1, //月份
  430. "d+": this.getDate(), //日
  431. "h+": this.getHours(), //小时
  432. "m+": this.getMinutes(), //分
  433. "s+": this.getSeconds(), //秒
  434. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  435. "S": this.getMilliseconds() //毫秒
  436. };
  437. if (/(y+)/.test(fmt))
  438. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  439. for (var k in o) {
  440. if (o.hasOwnProperty(k))
  441. if (new RegExp("(" + k + ")").test(fmt))
  442. fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  443. }
  444. return fmt;
  445. };
  446. function DateFormatter(v, r, i, f) {
  447. if (!v) {
  448. return "";
  449. }
  450. var date = new Date(v);
  451. var str = date.format('yyyy-MM-dd');
  452. r[f] = str;
  453. return str;
  454. }
  455. function DateTimeFormatter(v, r, i, f) {
  456. if (!v) {
  457. return "";
  458. }
  459. var date = new Date(v);
  460. var str = date.format('yyyy-MM-dd hh:mm:ss');
  461. r[f] = str;
  462. return str;
  463. }
  464. $(function () {
  465. //Configure blockUI
  466. if ($.blockUI) {
  467. $.blockUI.defaults.baseZ = 2000;
  468. }
  469. $('.iwb-date-time').iwbDate({ language: window.lang });
  470. $('.iwb-date').iwbDate({ language: window.lang });
  471. });