main.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. var Farm =
  2. {
  3. loop : false,
  4. url : '',
  5. Page : function(title, url)
  6. {
  7. var e = $('#layer');
  8. e.find('form').attr('action', url);
  9. if (title) {
  10. e.find('#layer_title').html(title);
  11. }
  12. $.get(url, function(t){
  13. e.find('#layer_content').html(t);
  14. e.show()
  15. })
  16. }
  17. ,Delete : function(obj, id, url)
  18. {
  19. var self = this;
  20. this.Confirm("确定要删除吗?" , function() {
  21. data = {};
  22. data.id = id;
  23. self.Request('delete', url, data, function(msg)
  24. {
  25. $(obj).parents("dl").remove();
  26. //self.Notice('已删除');
  27. })
  28. });
  29. }
  30. ,Cancel : function(obj, id, url)
  31. {
  32. var self = this;
  33. this.Confirm("确定要进行此项操作吗?" , function() {
  34. data = {};
  35. data.id = id;
  36. self.Request('delete', url, data, function(msg)
  37. {
  38. location.reload();
  39. })
  40. });
  41. }
  42. ,Recovery : function(obj, id, url)
  43. {
  44. var self = this;
  45. this.Confirm("确定要恢复吗?" , function() {
  46. data = {};
  47. data.id = id;
  48. data.state = true;
  49. self.Request('delete', url, data, function(msg)
  50. {
  51. location.reload();
  52. })
  53. });
  54. }
  55. ,Switch : function(obj, url, id)
  56. {
  57. var self = this;
  58. var e = $(obj);
  59. var c = e.attr('class');
  60. if ($('.loading').length) {
  61. return;
  62. }
  63. if (e.attr('class').indexOf('loading') > -1) {
  64. return;
  65. }
  66. if (c.indexOf('disabled') != -1) {
  67. return;
  68. }
  69. var value = e.attr('data-status');
  70. data = {};
  71. data.id = id;
  72. data.value = value;
  73. if (value == 1) {
  74. e.attr('data-status', 2);
  75. } else {
  76. e.attr('data-status', 1);
  77. }
  78. self.ClearLoop();
  79. e.addClass('loading');
  80. self.Request('post', url, data, function(msg)
  81. {
  82. self.Load();
  83. self.CreateLoop();
  84. /*
  85. e.removeClass('loading');
  86. msg = eval('(' + msg + ')');
  87. if (msg.status == 2) {
  88. alert(msg.msg);
  89. //this.Alert(msg.msg);
  90. return;
  91. }
  92. if (value == 1) {
  93. //开启
  94. e.addClass('open');
  95. } else {
  96. //关闭
  97. e.removeClass('open');
  98. }
  99. */
  100. })
  101. }
  102. ,Button : function(obj, url, id, value, disabledId)
  103. {
  104. var self = this;
  105. var e = $(obj);
  106. var c = e.attr('class');
  107. var p = e.parent();
  108. if ($('.loading').length) {
  109. return;
  110. }
  111. if (p.find('.loading').length) {
  112. return;
  113. }
  114. if (c.indexOf('disabled') != -1) {
  115. return;
  116. }
  117. /*
  118. var name = e.html();
  119. var old = p.find('.open')
  120. var oldDisabled = p.find('.disabled')
  121. old.removeClass('open');
  122. e.addClass('open');
  123. if (disabledId) {
  124. $(disabledId).addClass('disabled');
  125. } else {
  126. oldDisabled.removeClass('disabled');
  127. }
  128. */
  129. data = {};
  130. data.id = id;
  131. data.value = value;
  132. self.ClearLoop();
  133. e.addClass('loading');
  134. self.Request('post', url, data, function(msg)
  135. {
  136. self.Load();
  137. self.CreateLoop();
  138. /*
  139. e.removeClass('loading');
  140. self.CreateLoop();
  141. msg = eval('(' + msg + ')');
  142. if (msg.status == 2) {
  143. alert(msg.msg);
  144. //this.Alert(msg.msg);
  145. old.addClass('open');
  146. e.removeClass('open');
  147. if (disabledId) {
  148. $(disabledId).removeClass('disabled');
  149. } else {
  150. oldDisabled.addClass('disabled');
  151. }
  152. return;
  153. }
  154. */
  155. })
  156. }
  157. ,Request : function(type, url, data, callback)
  158. {
  159. data._xsrf = this.Cookie("_xsrf");
  160. $.ajax({
  161. type:type,
  162. url:url,
  163. data:data,
  164. success:function(msg){
  165. callback(msg)
  166. }
  167. });
  168. }
  169. ,Confirm : function(msg, func) {
  170. var e = $('#msg');
  171. e.find('#msg_content').html(msg);
  172. e.find('.controls a').eq(0).unbind('click').bind('click', function()
  173. {
  174. func();
  175. })
  176. e.show();
  177. }
  178. ,Alert : function(msg, title) {
  179. var e = $('#msg');
  180. if (title) {
  181. e.find('#msg_title').html(title);
  182. }
  183. e.find('#msg_content').html(msg);
  184. e.find('.controls a').eq(0).unbind('click');
  185. e.show();
  186. }
  187. ,Msg : function(value) {
  188. if (!value.data) {
  189. value.data = value.msg;
  190. }
  191. var url = '';
  192. if (value.data != 'reload' && value.status == 2) {
  193. this.Alert(value.msg);
  194. return;
  195. } else if(value.data == 'reload') {
  196. this.Jump('');
  197. } else {
  198. if($("#url").length) {
  199. url = $("#url").val();
  200. } else {
  201. location.reload()
  202. }
  203. if (url) {
  204. this.Jump(url);
  205. } else {
  206. this.Jump('');
  207. }
  208. }
  209. return;
  210. }
  211. ,Jump : function(url) {
  212. if (url) {
  213. location.href = url
  214. } else {
  215. location.reload()
  216. }
  217. }
  218. ,Cookie : function(name) {
  219. var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
  220. return r ? r[1] : undefined;
  221. }
  222. ,Load : function() {
  223. var self = this;
  224. $.ajax({
  225. url:self.url,
  226. type:'get',
  227. data:{},
  228. timeout:5000,
  229. success:function(data, textStatus){
  230. if (data) {
  231. if (data.indexOf('||') != -1) {
  232. var html = data.split('||');
  233. for(var i = 0;i < html.length; i++) {
  234. $("#loadMain_" + i).html(html[i]);
  235. }
  236. } else{
  237. $("#loadMain").html(data);
  238. }
  239. if ($('.video-js video').length && $('.box img').length) {
  240. var height = $('.video-js video').height();
  241. $('.box img').height(height).css('display','block');
  242. }
  243. self.CreateLoop();
  244. }
  245. }
  246. });
  247. }
  248. ,ShowChart : function(url) {
  249. this.url = url;
  250. this.ClearLoop();
  251. this.Load();
  252. document.getElementById('chart').scrollIntoView();
  253. }
  254. ,CreateLoop : function() {
  255. if (!this.loop) {
  256. this.loop = setInterval("Farm.Load()", 3000);
  257. }
  258. }
  259. ,ClearLoop : function() {
  260. if (this.loop) {
  261. clearInterval(this.loop);
  262. this.loop = false;
  263. }
  264. }
  265. ,Upload : function(id1, id2) {
  266. layui.use('upload', function() {
  267. var xsrf = Farm.Cookie("_xsrf");
  268. var uploadUrl = '/upload?_xsrf='+xsrf;
  269. var upload = layui.upload; //得到 upload 对象
  270. var config = {
  271. elem: id1
  272. ,url: uploadUrl
  273. ,done: function(res, index, upload){
  274. var id = $(id1).attr('id');
  275. $('#upload_' + id + '_show').attr('src',res.data.src + '?v' + Date.parse(new Date())).show();
  276. $('#update_' + id).val(res.data.src);
  277. }
  278. ,accept: 'images' //允许上传的文件类型
  279. ,size: 5000 //最大允许上传的文件大小
  280. }
  281. upload.render(config);
  282. if (id2) {
  283. var config = {
  284. elem: id2
  285. ,url: uploadUrl
  286. ,done: function(res, index, upload){
  287. var id = $(id2).attr('id');
  288. $('#upload_' + id + '_show').attr('src',res.data.src + '?v' + Date.parse(new Date())).show();
  289. $('#update_' + id).val(res.data.src);
  290. }
  291. ,accept: 'images' //允许上传的文件类型
  292. ,size: 5000 //最大允许上传的文件大小
  293. }
  294. upload.render(config);
  295. }
  296. });
  297. }
  298. }
  299. function msg(value) {
  300. Farm.Msg(value);
  301. }
  302. //瀑布流分页
  303. var _Dever_Page =
  304. {
  305. name : '.paging',
  306. Init : function()
  307. {
  308. if ($(this.name).length) {
  309. var self = this;
  310. $(window).scroll(function() {
  311. if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
  312. self.Start(self.name);
  313. }
  314. });
  315. }
  316. }
  317. ,Start : function(page)
  318. {
  319. page = page ? page : this.name;
  320. if ($(page).length) {
  321. $(page).hide();
  322. var url = $(page + ':last').attr('link');
  323. if (url) {
  324. $(".loading-more").show();
  325. var key = $(page).attr('dever-list');
  326. $.get(url, function(t) {
  327. t = '<div>' + t + '</div>';
  328. var c = $(t).find(key).html();
  329. if (c) {
  330. $(key).append(c);
  331. $(".loading-more").hide();
  332. var l = $(t).find(page).attr('link');
  333. $(page).attr('link', l);
  334. }
  335. });
  336. }
  337. }
  338. }
  339. ,Ajax : function(e)
  340. {
  341. var self = this;
  342. e.find('a').each(function() {
  343. var url = $(this).attr('href');
  344. $(this).attr('href', 'javascript:;');
  345. $(this).unbind('click').bind('click', function() {
  346. $.get(url.replace('callback', ''), function(t) {
  347. e.html(t);
  348. self.Ajax(e);
  349. });
  350. });
  351. });
  352. }
  353. }