util.js 18 KB

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