util.js 18 KB

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