common.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. ;(function () {
  2. //全局ajax处理
  3. $.ajaxSetup({
  4. complete: function (jqXHR) {},
  5. data: {
  6. },
  7. error: function (jqXHR, textStatus, errorThrown) {
  8. //请求失败处理
  9. }
  10. });
  11. if ($.browser.msie) {
  12. //ie 都不缓存
  13. $.ajaxSetup({
  14. cache: false
  15. });
  16. }
  17. //不支持placeholder浏览器下对placeholder进行处理
  18. if (document.createElement('input').placeholder !== '') {
  19. $('[placeholder]').focus(function () {
  20. var input = $(this);
  21. if (input.val() == input.attr('placeholder')) {
  22. input.val('');
  23. input.removeClass('placeholder');
  24. }
  25. }).blur(function () {
  26. var input = $(this);
  27. if (input.val() == '' || input.val() == input.attr('placeholder')) {
  28. input.addClass('placeholder');
  29. input.val(input.attr('placeholder'));
  30. }
  31. }).blur().parents('form').submit(function () {
  32. $(this).find('[placeholder]').each(function () {
  33. var input = $(this);
  34. if (input.val() == input.attr('placeholder')) {
  35. input.val('');
  36. }
  37. });
  38. });
  39. }
  40. // 所有加了dialog类名的a链接,自动弹出它的href
  41. if ($('a.js-dialog').length) {
  42. Wind.use('artDialog', 'iframeTools', function() {
  43. $('.js-dialog').on('click', function(e) {
  44. e.preventDefault();
  45. var $this = $(this);
  46. art.dialog.open($(this).prop('href'), {
  47. close : function() {
  48. $this.focus(); // 关闭时让触发弹窗的元素获取焦点
  49. return true;
  50. },
  51. title : $this.prop('title')
  52. });
  53. }).attr('role', 'button');
  54. });
  55. }
  56. // 所有的ajax form提交,由于大多业务逻辑都是一样的,故统一处理
  57. var ajaxForm_list = $('form.js-ajax-form');
  58. if (ajaxForm_list.length) {
  59. Wind.use('ajaxForm', 'artDialog', function () {
  60. if ($.browser.msie) {
  61. //ie8及以下,表单中只有一个可见的input:text时,会整个页面会跳转提交
  62. ajaxForm_list.on('submit', function (e) {
  63. //表单中只有一个可见的input:text时,enter提交无效
  64. e.preventDefault();
  65. });
  66. }
  67. $('button.js-ajax-submit').on('click', function (e) {
  68. e.preventDefault();
  69. /*var btn = $(this).find('button.js-ajax-submit'),
  70. form = $(this);*/
  71. var btn = $(this),
  72. form = btn.parents('form.js-ajax-form');
  73. if(btn.data("loading")){
  74. return;
  75. }
  76. //批量操作 判断选项
  77. if (btn.data('subcheck')) {
  78. btn.parent().find('span').remove();
  79. if (form.find('input.js-check:checked').length) {
  80. var msg = btn.data('msg');
  81. if (msg) {
  82. art.dialog({
  83. id: 'warning',
  84. icon: 'warning',
  85. content: btn.data('msg'),
  86. cancelVal: '关闭',
  87. cancel: function () {
  88. //btn.data('subcheck', false);
  89. //btn.click();
  90. },
  91. ok: function () {
  92. btn.data('subcheck', false);
  93. btn.click();
  94. }
  95. });
  96. } else {
  97. btn.data('subcheck', false);
  98. btn.click();
  99. }
  100. } else {
  101. $('<span class="tips_error">请至少选择一项</span>').appendTo(btn.parent()).fadeIn('fast');
  102. }
  103. return false;
  104. }
  105. //ie处理placeholder提交问题
  106. if ($.browser.msie) {
  107. form.find('[placeholder]').each(function () {
  108. var input = $(this);
  109. if (input.val() == input.attr('placeholder')) {
  110. input.val('');
  111. }
  112. });
  113. }
  114. form.ajaxSubmit({
  115. url: btn.data('action') ? btn.data('action') : form.attr('action'), //按钮上是否自定义提交地址(多按钮情况)
  116. dataType: 'json',
  117. beforeSubmit: function (arr, $form, options) {
  118. btn.data("loading",true);
  119. var text = btn.text();
  120. //按钮文案、状态修改
  121. btn.text(text + '中...').prop('disabled', true).addClass('disabled');
  122. },
  123. success: function (data, statusText, xhr, $form) {
  124. var text = btn.text();
  125. //按钮文案、状态修改
  126. btn.removeClass('disabled').text(text.replace('中...', '')).parent().find('span').remove();
  127. if (data.state === 'success') {
  128. $('<span class="tips_success">' + data.info + '</span>').appendTo(btn.parent()).fadeIn('slow').delay(1000).fadeOut(function () {
  129. });
  130. } else if (data.state === 'fail') {
  131. var $verify_img=form.find(".verify_img");
  132. if($verify_img.length){
  133. $verify_img.attr("src",$verify_img.attr("src")+"&refresh="+Math.random());
  134. }
  135. var $verify_input=form.find("[name='verify']");
  136. $verify_input.val("");
  137. $('<span class="tips_error">' + data.info + '</span>').appendTo(btn.parent()).fadeIn('fast');
  138. btn.removeProp('disabled').removeClass('disabled');
  139. }
  140. if (data.referer) {
  141. //返回带跳转地址
  142. if(window.parent.art){
  143. //iframe弹出页
  144. window.parent.location.href = data.referer;
  145. }else{
  146. window.location.href = data.referer;
  147. }
  148. } else {
  149. if (data.state === 'success') {
  150. if(window.parent.art){
  151. reloadPage(window.parent);
  152. }else{
  153. //刷新当前页
  154. reloadPage(window);
  155. }
  156. }
  157. }
  158. },
  159. complete: function(){
  160. btn.data("loading",false);
  161. }
  162. });
  163. });
  164. });
  165. }
  166. //dialog弹窗内的关闭方法
  167. $('#js-dialog_close').on('click', function (e) {
  168. e.preventDefault();
  169. try{
  170. art.dialog.close();
  171. }catch(err){
  172. Wind.use('artDialog','iframeTools',function(){
  173. art.dialog.close();
  174. });
  175. };
  176. });
  177. //所有的删除操作,删除数据后刷新页面
  178. if ($('a.js-ajax-delete').length) {
  179. Wind.use('artDialog', function () {
  180. $('.js-ajax-delete').on('click', function (e) {
  181. e.preventDefault();
  182. var $_this = this,
  183. $this = $($_this),
  184. href = $this.prop('href'),
  185. msg = $this.data('msg');
  186. art.dialog({
  187. title: false,
  188. icon: 'question',
  189. content: '确定要删除吗?',
  190. follow: $_this,
  191. close: function () {
  192. $_this.focus();; //关闭时让触发弹窗的元素获取焦点
  193. return true;
  194. },
  195. okVal:"确定",
  196. ok: function () {
  197. $.getJSON(href).done(function (data) {
  198. if (data.state === 'success') {
  199. if (data.referer) {
  200. location.href = data.referer;
  201. } else {
  202. reloadPage(window);
  203. }
  204. } else if (data.state === 'fail') {
  205. //art.dialog.alert(data.info);
  206. //alert(data.info);//暂时处理方案
  207. art.dialog({
  208. content: data.info,
  209. icon: 'warning',
  210. ok: function () {
  211. this.title(data.info);
  212. return true;
  213. }
  214. });
  215. }
  216. });
  217. },
  218. cancelVal: '关闭',
  219. cancel: true
  220. });
  221. });
  222. });
  223. }
  224. if ($('a.js-ajax-dialog-btn').length) {
  225. Wind.use('artDialog', function () {
  226. $('.js-ajax-dialog-btn').on('click', function (e) {
  227. e.preventDefault();
  228. var $_this = this,
  229. $this = $($_this),
  230. href = $this.prop('href'),
  231. msg = $this.data('msg');
  232. if(!msg){
  233. msg="您确定要进行此操作吗?";
  234. }
  235. art.dialog({
  236. title: false,
  237. icon: 'question',
  238. content: msg,
  239. follow: $_this,
  240. close: function () {
  241. $_this.focus();; //关闭时让触发弹窗的元素获取焦点
  242. return true;
  243. },
  244. ok: function () {
  245. $.getJSON(href).done(function (data) {
  246. if (data.state === 'success') {
  247. if (data.referer) {
  248. location.href = data.referer;
  249. } else {
  250. reloadPage(window);
  251. }
  252. } else if (data.state === 'fail') {
  253. //art.dialog.alert(data.info);
  254. art.dialog({
  255. content: data.info,
  256. icon: 'warning',
  257. ok: function () {
  258. this.title(data.info);
  259. return true;
  260. }
  261. });
  262. }
  263. });
  264. },
  265. cancelVal: '关闭',
  266. cancel: true
  267. });
  268. });
  269. });
  270. }
  271. /*复选框全选(支持多个,纵横双控全选)。
  272. *实例:版块编辑-权限相关(双控),验证机制-验证策略(单控)
  273. *说明:
  274. * "js-check"的"data-xid"对应其左侧"js-check-all"的"data-checklist";
  275. * "js-check"的"data-yid"对应其上方"js-check-all"的"data-checklist";
  276. * 全选框的"data-direction"代表其控制的全选方向(x或y);
  277. * "js-check-wrap"同一块全选操作区域的父标签class,多个调用考虑
  278. */
  279. if ($('.js-check-wrap').length) {
  280. var total_check_all = $('input.js-check-all');
  281. //遍历所有全选框
  282. $.each(total_check_all, function () {
  283. var check_all = $(this),
  284. check_items;
  285. //分组各纵横项
  286. var check_all_direction = check_all.data('direction');
  287. check_items = $('input.js-check[data-' + check_all_direction + 'id="' + check_all.data('checklist') + '"]');
  288. //点击全选框
  289. check_all.change(function (e) {
  290. var check_wrap = check_all.parents('.js-check-wrap'); //当前操作区域所有复选框的父标签(重用考虑)
  291. if ($(this).attr('checked')) {
  292. //全选状态
  293. check_items.attr('checked', true);
  294. //所有项都被选中
  295. if (check_wrap.find('input.js-check').length === check_wrap.find('input.js-check:checked').length) {
  296. check_wrap.find(total_check_all).attr('checked', true);
  297. }
  298. } else {
  299. //非全选状态
  300. check_items.removeAttr('checked');
  301. check_wrap.find(total_check_all).removeAttr('checked');
  302. //另一方向的全选框取消全选状态
  303. var direction_invert = check_all_direction === 'x' ? 'y' : 'x';
  304. check_wrap.find($('input.js-check-all[data-direction="' + direction_invert + '"]')).removeAttr('checked');
  305. }
  306. });
  307. //点击非全选时判断是否全部勾选
  308. check_items.change(function () {
  309. if ($(this).attr('checked')) {
  310. if (check_items.filter(':checked').length === check_items.length) {
  311. //已选择和未选择的复选框数相等
  312. check_all.attr('checked', true);
  313. }
  314. } else {
  315. check_all.removeAttr('checked');
  316. }
  317. });
  318. });
  319. }
  320. //日期选择器
  321. var dateInput = $("input.js-date")
  322. if (dateInput.length) {
  323. Wind.use('datePicker', function () {
  324. dateInput.datePicker();
  325. });
  326. }
  327. //日期+时间选择器
  328. var dateTimeInput = $("input.js-datetime");
  329. if (dateTimeInput.length) {
  330. Wind.use('datePicker', function () {
  331. dateTimeInput.datePicker({
  332. time: true
  333. });
  334. });
  335. }
  336. //tab
  337. var tabs_nav = $('ul.js-tabs-nav');
  338. if (tabs_nav.length) {
  339. Wind.use('tabs', function () {
  340. tabs_nav.tabs('.js-tabs-content > div');
  341. });
  342. }
  343. })();
  344. //重新刷新页面,使用location.reload()有可能导致重新提交
  345. function reloadPage(win) {
  346. var location = win.location;
  347. location.href = location.pathname + location.search;
  348. }
  349. /**
  350. * 页面跳转
  351. * @param url
  352. */
  353. function redirect(url) {
  354. location.href = url;
  355. }
  356. /**
  357. * 读取cookie
  358. * @param name
  359. * @returns
  360. */
  361. function getCookie(name) {
  362. var nameEQ = name + "=";
  363. var ca = document.cookie.split(';');
  364. for (var i = 0; i < ca.length; i++) {
  365. var c = ca[i];
  366. while (c.charAt(0) == ' ') {
  367. c = c.substring(1, c.length);
  368. }
  369. if (c.indexOf(nameEQ) == 0) {
  370. return c.substring(nameEQ.length, c.length);
  371. }
  372. }
  373. return null;
  374. }
  375. // 设置cookie
  376. function setCookie(name, value, days) {
  377. var argc = setCookie.arguments.length;
  378. var argv = setCookie.arguments;
  379. var secure = (argc > 5) ? argv[5] : false;
  380. var expire = new Date();
  381. if(days==null || days==0) days=1;
  382. expire.setTime(expire.getTime() + 3600000*24*days);
  383. document.cookie = name + "=" + escape(value) + ("; path=/") + ((secure == true) ? "; secure" : "") + ";expires="+expire.toGMTString();
  384. }
  385. /**
  386. * 打开iframe式的窗口对话框
  387. * @param url
  388. * @param title
  389. * @param options
  390. */
  391. function open_iframe_dialog(url, title, options) {
  392. var params = {
  393. title : title,
  394. lock : true,
  395. opacity : 0,
  396. width : "95%"
  397. };
  398. params = options ? $.extend(params, options) : params;
  399. Wind.use('artDialog', 'iframeTools', function() {
  400. art.dialog.open(url, params);
  401. });
  402. }
  403. /**
  404. * 打开地图对话框
  405. *
  406. * @param url
  407. * @param title
  408. * @param options
  409. * @param callback
  410. */
  411. function open_map_dialog(url, title, options, callback) {
  412. var params = {
  413. title : title,
  414. lock : true,
  415. opacity : 0,
  416. width : "95%",
  417. height : 400,
  418. ok : function() {
  419. if (callback) {
  420. var d = this.iframe.contentWindow;
  421. var lng = $("#lng_input", d.document).val();
  422. var lat = $("#lat_input", d.document).val();
  423. var address = {};
  424. address.address = $("#address_input", d.document).val();
  425. address.province = $("#province_input", d.document).val();
  426. address.city = $("#city_input", d.document).val();
  427. address.district = $("#district_input", d.document).val();
  428. callback.apply(this, [ lng, lat, address ]);
  429. }
  430. }
  431. };
  432. params = options ? $.extend(params, options) : params;
  433. Wind.use('artDialog', 'iframeTools', function() {
  434. art.dialog.open(url, params);
  435. });
  436. }