frontend.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. _this = $($_this);
  47. art.dialog.open($(this).prop('href'), {
  48. close: function () {
  49. $_this.focus(); //关闭时让触发弹窗的元素获取焦点
  50. return true;
  51. },
  52. title: _this.prop('title')
  53. });
  54. }).attr('role', 'button');
  55. });
  56. }
  57. //所有的ajax form提交,由于大多业务逻辑都是一样的,故统一处理
  58. var ajaxForm_list = $('form.js-ajax-form');
  59. if (ajaxForm_list.length) {
  60. Wind.use('ajaxForm', 'noty', function () {
  61. if ($.browser.msie) {
  62. //ie8及以下,表单中只有一个可见的input:text时,会整个页面会跳转提交
  63. ajaxForm_list.on('submit', function (e) {
  64. //表单中只有一个可见的input:text时,enter提交无效
  65. e.preventDefault();
  66. });
  67. }
  68. $('button.js-ajax-submit').on('click', function (e) {
  69. e.preventDefault();
  70. /*var btn = $(this).find('button.js-ajax-submit'),
  71. form = $(this);*/
  72. var btn = $(this),
  73. form = btn.parents('form.js-ajax-form');
  74. if(btn.data("loading")){
  75. return;
  76. }
  77. //批量操作 判断选项
  78. if (btn.data('subcheck')) {
  79. btn.parent().find('span').remove();
  80. if (form.find('input.js-check:checked').length) {
  81. var msg = btn.data('msg');
  82. if (msg) {
  83. noty({
  84. text: msg,
  85. type:'confirm',
  86. layout:"center",
  87. timeout: false,
  88. modal: true,
  89. buttons: [
  90. {
  91. addClass: 'btn btn-primary',
  92. text: '确定',
  93. onClick: function($noty){
  94. $noty.close();
  95. btn.data('subcheck', false);
  96. btn.click();
  97. }
  98. },
  99. {
  100. addClass: 'btn btn-danger',
  101. text: '取消',
  102. onClick: function($noty) {
  103. $noty.close();
  104. }
  105. }
  106. ]
  107. });
  108. } else {
  109. btn.data('subcheck', false);
  110. btn.click();
  111. }
  112. } else {
  113. noty({text:"请至少选择一项",
  114. type:'error',
  115. layout:'center'
  116. });
  117. }
  118. return false;
  119. }
  120. //ie处理placeholder提交问题
  121. if ($.browser.msie) {
  122. form.find('[placeholder]').each(function () {
  123. var input = $(this);
  124. if (input.val() == input.attr('placeholder')) {
  125. input.val('');
  126. }
  127. });
  128. }
  129. form.ajaxSubmit({
  130. url: btn.data('action') ? btn.data('action') : form.attr('action'), //按钮上是否自定义提交地址(多按钮情况)
  131. dataType: 'json',
  132. beforeSubmit: function (arr, $form, options) {
  133. btn.data("loading",true);
  134. var text = btn.text();
  135. //按钮文案、状态修改
  136. btn.text(text + '中...').prop('disabled', true).addClass('disabled');
  137. },
  138. success: function (data, statusText, xhr, $form) {
  139. var text = btn.text();
  140. //按钮文案、状态修改
  141. btn.removeClass('disabled').text(text.replace('中...', '')).parent().find('span').remove();
  142. if (data.state === 'success') {
  143. noty({text: data.info,
  144. type:'success',
  145. layout:'center'
  146. });
  147. } else if (data.state === 'fail') {
  148. var $verify_img=form.find(".verify_img");
  149. if($verify_img.length){
  150. $verify_img.attr("src",$verify_img.attr("src")+"&refresh="+Math.random());
  151. }
  152. var $verify_input=form.find("[name='verify']");
  153. $verify_input.val("");
  154. noty({text: data.info,
  155. type:'error',
  156. layout:'center'
  157. });
  158. btn.removeProp('disabled').removeClass('disabled');
  159. }
  160. if (data.referer) {
  161. //返回带跳转地址
  162. var wait=btn.data("wait");
  163. if(!wait){
  164. wait=1500;
  165. }
  166. if(window.parent.art){
  167. //iframe弹出页
  168. if(wait){
  169. setTimeout(function(){
  170. window.parent.location.href = data.referer;
  171. },wait);
  172. }else{
  173. window.parent.location.href = data.referer;
  174. }
  175. }else{
  176. if(wait){
  177. setTimeout(function(){
  178. window.location.href = data.referer;
  179. },wait);
  180. }else{
  181. window.location.href = data.referer;
  182. }
  183. }
  184. } else {
  185. if (data.state === 'success') {
  186. var wait=btn.data("wait");
  187. if(window.parent.art){
  188. if(wait){
  189. setTimeout(function(){
  190. reloadPage(window.parent);
  191. },wait);
  192. }else{
  193. reloadPage(window.parent);
  194. }
  195. }else{
  196. //刷新当前页
  197. if(wait){
  198. setTimeout(function(){
  199. reloadPage(window);
  200. },wait);
  201. }else{
  202. reloadPage(window);
  203. }
  204. }
  205. }
  206. }
  207. },
  208. complete: function(){
  209. btn.data("loading",false);
  210. }
  211. });
  212. });
  213. });
  214. }
  215. //dialog弹窗内的关闭方法
  216. $('#js-dialog_close').on('click', function (e) {
  217. e.preventDefault();
  218. try{
  219. art.dialog.close();
  220. }catch(err){
  221. Wind.use('artDialog','iframeTools',function(){
  222. art.dialog.close();
  223. });
  224. };
  225. });
  226. //所有的删除操作,删除数据后刷新页面
  227. if ($('a.js-ajax-delete').length) {
  228. Wind.use('noty', function () {
  229. $('.js-ajax-delete').on('click', function (e) {
  230. e.preventDefault();
  231. var $_this = this,
  232. $this = $($_this),
  233. href = $this.prop('href'),
  234. msg = $this.data('msg');
  235. noty({
  236. text: msg,
  237. type:'confirm',
  238. layout:"center",
  239. timeout: false,
  240. modal: true,
  241. buttons: [
  242. {
  243. addClass: 'btn btn-primary',
  244. text: '确定',
  245. onClick: function($noty){
  246. $noty.close();
  247. $.getJSON(href).done(function (data) {
  248. if (data.state === 'success') {
  249. if (data.referer) {
  250. location.href = data.referer;
  251. } else {
  252. reloadPage(window);
  253. }
  254. } else if (data.state === 'fail') {
  255. //art.dialog.alert(data.info);
  256. //alert(data.info);//暂时处理方案
  257. art.dialog({
  258. content: data.info,
  259. icon: 'warning',
  260. ok: function () {
  261. this.title(data.info);
  262. return true;
  263. }
  264. });
  265. }
  266. });
  267. }
  268. },
  269. {
  270. addClass: 'btn btn-danger',
  271. text: '取消',
  272. onClick: function($noty) {
  273. $noty.close();
  274. }
  275. }
  276. ]
  277. });
  278. });
  279. });
  280. }
  281. if ($('a.js-ajax-dialog-btn').length) {
  282. Wind.use('noty', function () {
  283. $('.js-ajax-dialog-btn').on('click', function (e) {
  284. e.preventDefault();
  285. var $_this = this,
  286. $this = $($_this),
  287. href = $this.prop('href'),
  288. msg = $this.data('msg');
  289. noty({
  290. text: msg,
  291. type:'confirm',
  292. layout:"center",
  293. timeout: false,
  294. modal: true,
  295. buttons: [
  296. {
  297. addClass: 'btn btn-primary',
  298. text: '确定',
  299. onClick: function($noty){
  300. $noty.close();
  301. $.getJSON(href).done(function (data) {
  302. if (data.state === 'success') {
  303. if (data.referer) {
  304. location.href = data.referer;
  305. } else {
  306. reloadPage(window);
  307. }
  308. } else if (data.state === 'fail') {
  309. art.dialog.alert(data.info);
  310. }
  311. });
  312. }
  313. },
  314. {
  315. addClass: 'btn btn-danger',
  316. text: '取消',
  317. onClick: function($noty) {
  318. $noty.close();
  319. }
  320. }
  321. ]
  322. });
  323. });
  324. });
  325. }
  326. //所有的请求刷新操作
  327. var ajax_refresh = $('a.J_ajax_refresh'),
  328. refresh_lock = false;
  329. if (ajax_refresh.length) {
  330. ajax_refresh.on('click', function (e) {
  331. e.preventDefault();
  332. if (refresh_lock) {
  333. return false;
  334. }
  335. refresh_lock = true;
  336. $.post(this.href, function (data) {
  337. refresh_lock = false;
  338. if (data.state === 'success') {
  339. if (data.referer) {
  340. location.href = data.referer;
  341. } else {
  342. reloadPage(window);
  343. }
  344. } else if (data.state === 'fail') {
  345. Wind.art.dialog.alert(data.info);
  346. }
  347. }, 'json');
  348. });
  349. }
  350. //日期选择器
  351. var dateInput = $("input.js-date")
  352. if (dateInput.length) {
  353. Wind.use('datePicker', function () {
  354. dateInput.datePicker();
  355. });
  356. }
  357. //日期+时间选择器
  358. var dateTimeInput = $("input.js-datetime");
  359. if (dateTimeInput.length) {
  360. Wind.use('datePicker', function () {
  361. dateTimeInput.datePicker({
  362. time: true
  363. });
  364. });
  365. }
  366. //赞,拍等,有数量操作的按钮
  367. var $js_count_btn=$('a.js-count-btn');
  368. if ($js_count_btn.length) {
  369. Wind.use('noty', function () {
  370. $js_count_btn.on('click', function (e) {
  371. e.preventDefault();
  372. var $this = $(this),
  373. href = $this.prop('href');
  374. $.post(href,{},function(data){
  375. if (data.state === 'success') {
  376. var $count=$this.find(".count");
  377. var count=parseInt($count.text());
  378. $count.text(count+1);
  379. if(data.info){
  380. noty({text: data.info,
  381. type:'success',
  382. layout:'center'
  383. });
  384. }
  385. } else if (data.state === 'fail') {
  386. noty({text: data.info,
  387. type:'error',
  388. layout:'center'
  389. });
  390. }
  391. },"json");
  392. });
  393. });
  394. }
  395. //
  396. var $J_action_btn=$('a.J_action_btn');
  397. if ($J_action_btn.length) {
  398. Wind.use('noty', function () {
  399. $J_action_btn.on('click', function (e) {
  400. e.preventDefault();
  401. var $this = $(this),
  402. href = $this.prop('href');
  403. $.post(href,{},function(data){
  404. if (data.state === 'success') {
  405. if(data.info){
  406. noty({text: data.info,
  407. type:'success',
  408. layout:'center'
  409. });
  410. }
  411. } else if (data.state === 'fail') {
  412. noty({text: data.info,
  413. type:'error',
  414. layout:'center'
  415. });
  416. }
  417. },"json");
  418. });
  419. });
  420. }
  421. var $js_favorite_btn=$('a.js-favorite-btn');
  422. if ($js_favorite_btn.length) {
  423. Wind.use('noty', function () {
  424. $js_favorite_btn.on('click', function (e) {
  425. e.preventDefault();
  426. var $this = $(this),
  427. href = $this.prop('href'),
  428. url = $this.data("url"),
  429. key = $this.data("key"),
  430. title = $this.data("title"),
  431. description = $this.data("description");
  432. $.post(href,{url:url,key:key,title:title,description:description},function(data){
  433. if (data.state === 'success') {
  434. if(data.info){
  435. noty({text: data.info,
  436. type:'success',
  437. layout:'center'
  438. });
  439. }
  440. } else if (data.state === 'fail') {
  441. noty({text: data.info,
  442. type:'error',
  443. layout:'center'
  444. });
  445. }
  446. },"json");
  447. });
  448. });
  449. }
  450. var $comment_form=$(".comment-area .comment-form");
  451. if($comment_form.length){
  452. Wind.use("ajaxForm",function(){
  453. $(".js-ajax-submit",$comment_form).on("click",function(e){
  454. var btn=$(this),
  455. form=btn.parents(".comment-form");
  456. e.preventDefault();
  457. var url= btn.data('action') ? btn.data('action') : form.attr('action');
  458. var data=form.serialize()+"&url="+encodeURIComponent(location.href);
  459. $.ajax({
  460. url:url,
  461. dataType: 'json',
  462. type: "POST",
  463. beforeSend:function(){
  464. var text = btn.text();
  465. //按钮文案、状态修改
  466. btn.text(text + '中...').prop('disabled', true).addClass('disabled');
  467. },
  468. data:data,
  469. success: function (data, textStatus, jqXHR) {
  470. var text = btn.text();
  471. //按钮文案、状态修改
  472. btn.removeClass('disabled').text(text.replace('中...', '')).parent().find('span').remove();
  473. btn.removeProp('disabled').removeClass('disabled');
  474. if (data.state === 'success') {
  475. $('<span class="tips_success">' + data.info + '</span>').appendTo(btn.parent()).fadeIn('slow').delay(1000).fadeOut(function () {
  476. });
  477. } else if (data.state === 'fail') {
  478. $('<span class="tips_error">' + data.info + '</span>').appendTo(btn.parent()).fadeIn('fast');
  479. btn.removeProp('disabled').removeClass('disabled');
  480. }
  481. if (data.state === 'success') {
  482. var $comments=form.siblings(".comments");
  483. var comment_tpl=btn.parents(".comment-area").find(".comment-tpl").html();
  484. var $comment_tpl=$(comment_tpl);
  485. $comment_tpl.attr("data-id",data.data.id);
  486. var $comment_postbox=form.find(".comment-postbox");
  487. var comment_content=$comment_postbox.val();
  488. $comment_tpl.find(".comment-content .content").html(comment_content);
  489. $comments.append($comment_tpl);
  490. $comment_postbox.val("");
  491. }
  492. }
  493. });
  494. return false;
  495. });
  496. });
  497. }
  498. })();
  499. function comment_reply(obj){
  500. $(".comments .comment-reply-submit").hide();
  501. var $this=$(obj);
  502. var $comment_body=$this.parents(".comments > .comment> .comment-body");
  503. var commentid=$this.parents(".comment").data("id");
  504. var $comment_reply_submit=$comment_body.find(".comment-reply-submit");
  505. if($comment_reply_submit.length){
  506. $comment_reply_submit.show();
  507. }else{
  508. var comment_reply_box_tpl=$comment_body.parents(".comment-area").find(".comment-reply-box-tpl").html();
  509. $comment_reply_submit=$(comment_reply_box_tpl);
  510. $comment_body.append($comment_reply_submit);
  511. }
  512. $comment_reply_submit.find(".textbox").focus();
  513. $comment_reply_submit.data("replyid",commentid);
  514. }
  515. function comment_submit(obj){
  516. Wind.use('noty', function () {
  517. var $this=$(obj);
  518. var $comment_reply_submit=$this.parents(".comment-reply-submit");
  519. var $reply_textbox=$comment_reply_submit.find(".textbox");
  520. var reply_content=$reply_textbox.val();
  521. if(reply_content==''){
  522. $reply_textbox.focus();
  523. return;
  524. }
  525. var $comment_body=$this.parents(".comments > .comment> .comment-body");
  526. var comment_tpl=$comment_body.parents(".comment-area").find(".comment-tpl").html();
  527. var $comment_tpl=$(comment_tpl);
  528. var replyid=$comment_reply_submit.data('replyid');
  529. var $comment=$(".comments [data-id='"+replyid+"']");
  530. var username=$comment.data("username");
  531. var comment_content="回复 "+username+":"+reply_content;
  532. $comment_tpl.find(".comment-content .content").html(comment_content);
  533. $comment_reply_submit.before($comment_tpl);
  534. var $comment_form=$this.parents(".comment-area").find(".comment-form");
  535. var comment_url=$comment_form.attr("action");
  536. var post_table=$comment_form.find("[name='post_table']").val();
  537. var post_id=$comment_form.find("[name='post_id']").val();
  538. var uid=$comment.data("uid");
  539. $.post(comment_url,
  540. {
  541. post_table:post_table,
  542. post_id:post_id,
  543. to_uid:uid,
  544. parentid:replyid,
  545. content:reply_content,
  546. url:encodeURIComponent(location.href)
  547. },function(data){
  548. if(data.status==0){
  549. noty({text: data.info,
  550. type:'error',
  551. layout:'center'
  552. });
  553. $comment_tpl.remove();
  554. }
  555. if(data.status==1){
  556. $comment_tpl.attr("data-id",data.data.id);
  557. $reply_textbox.val('');
  558. }
  559. },'json');
  560. $comment_reply_submit.hide();
  561. });
  562. }
  563. //重新刷新页面,使用location.reload()有可能导致重新提交
  564. function reloadPage(win) {
  565. if(win){
  566. }else{
  567. win=window;
  568. }
  569. var location = win.location;
  570. location.href = location.pathname + location.search;
  571. }
  572. //页面跳转
  573. function redirect(url) {
  574. location.href = url;
  575. }
  576. //读取cookie
  577. function getCookie(name) {
  578. var nameEQ = name + "=";
  579. var ca = document.cookie.split(';');
  580. for (var i = 0; i < ca.length; i++) {
  581. var c = ca[i];
  582. while (c.charAt(0) == ' ') {
  583. c = c.substring(1, c.length);
  584. }
  585. if (c.indexOf(nameEQ) == 0) {
  586. return c.substring(nameEQ.length, c.length);
  587. }
  588. };
  589. return null;
  590. }
  591. //设置cookie
  592. function setCookie(name, value, days) {
  593. var argc = setCookie.arguments.length;
  594. var argv = setCookie.arguments;
  595. var secure = (argc > 5) ? argv[5] : false;
  596. var expire = new Date();
  597. if(days==null || days==0) days=1;
  598. expire.setTime(expire.getTime() + 3600000*24*days);
  599. document.cookie = name + "=" + escape(value) + ("; path=/") + ((secure == true) ? "; secure" : "") + ";expires="+expire.toGMTString();
  600. }
  601. //浮出提示_居中
  602. function resultTip(options) {
  603. var cls = (options.error ? 'warning' : 'success');
  604. var pop = $('<div style="left:50%;top:30%;" class="pop_showmsg_wrap"><span class="pop_showmsg"><span class="' + cls + '">' + options.msg + '</span></span></div>');
  605. pop.appendTo($('body')).fadeIn(function () {
  606. pop.css({
  607. marginLeft: -pop.innerWidth() / 2
  608. }); //水平居中
  609. }).delay(1500).fadeOut(function () {
  610. pop.remove();
  611. //回调
  612. if (options.callback) {
  613. options.callback();
  614. }
  615. });
  616. }
  617. //弹窗居中定位 非ie6 fixed定位
  618. function popPos(wrap) {
  619. var ie6 = false,
  620. pos = 'fixed',
  621. top,
  622. win_height = $(window).height(),
  623. wrap_height = wrap.outerHeight();
  624. if ($.browser.msie && $.browser.version < 7) {
  625. ie6 = true;
  626. pos = 'absolute';
  627. }
  628. if (win_height < wrap_height) {
  629. top = 0;
  630. } else {
  631. top = ($(window).height() - wrap.outerHeight()) / 2;
  632. }
  633. wrap.css({
  634. position: pos,
  635. top: top + (ie6 ? $(document).scrollTop() : 0),
  636. left: ($(window).width() - wrap.innerWidth()) / 2
  637. }).show();
  638. }
  639. //新窗口打开
  640. function openwinx(url,name,w,h) {
  641. if(!w) w=screen.width;
  642. if(!h) h=screen.height;
  643. //window.open(url,name,"top=100,left=400,width=" + w + ",height=" + h + ",toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no");
  644. window.open(url,name);
  645. }
  646. //询问
  647. function confirmurl(url, message) {
  648. Wind.use("artDialog", "iframeTools", function () {
  649. art.dialog.confirm(message, function () {
  650. location.href = url;
  651. }, function () {
  652. art.dialog.tips('你取消了操作');
  653. });
  654. });
  655. }
  656. function open_iframe_dialog(url,title,options){
  657. var params={
  658. title: title,
  659. lock:true,
  660. opacity:0,
  661. width:"95%"
  662. };
  663. params=options?$.extend(params,options):params;
  664. Wind.use('artDialog','iframeTools', function () {
  665. art.dialog.open(url, params);
  666. });
  667. }