main.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 (value.data.url) {
  199. this.Jump(value.data.url);
  200. return;
  201. }
  202. if($("#url").length) {
  203. url = $("#url").val();
  204. } else {
  205. location.reload()
  206. }
  207. if (url) {
  208. this.Jump(url);
  209. } else {
  210. this.Jump('');
  211. }
  212. }
  213. return;
  214. }
  215. ,Jump : function(url) {
  216. if (url) {
  217. location.href = url
  218. } else {
  219. location.reload()
  220. }
  221. }
  222. ,Cookie : function(name) {
  223. var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
  224. return r ? r[1] : undefined;
  225. }
  226. ,Load : function() {
  227. var self = this;
  228. $.ajax({
  229. url:self.url,
  230. type:'get',
  231. data:{},
  232. timeout:5000,
  233. success:function(data, textStatus){
  234. if (data) {
  235. if (data.indexOf('||') != -1) {
  236. var html = data.split('||');
  237. for(var i = 0;i < html.length; i++) {
  238. $("#loadMain_" + i).html(html[i]);
  239. }
  240. } else{
  241. $("#loadMain").html(data);
  242. }
  243. if ($('.video-js video').length && $('.box img').length) {
  244. var height = $('.video-js video').height();
  245. $('.box img').height(height).css('display','block');
  246. }
  247. self.CreateLoop();
  248. }
  249. }
  250. });
  251. }
  252. ,ShowChart : function(url) {
  253. this.url = url;
  254. this.ClearLoop();
  255. this.Load();
  256. document.getElementById('chart').scrollIntoView();
  257. }
  258. ,CreateLoop : function() {
  259. if (!this.loop) {
  260. this.loop = setInterval("Farm.Load()", 3000);
  261. }
  262. }
  263. ,ClearLoop : function() {
  264. if (this.loop) {
  265. clearInterval(this.loop);
  266. this.loop = false;
  267. }
  268. }
  269. ,Upload : function(id1, id2) {
  270. layui.use('upload', function() {
  271. var xsrf = Farm.Cookie("_xsrf");
  272. var uploadUrl = '/upload?_xsrf='+xsrf;
  273. var upload = layui.upload; //得到 upload 对象
  274. var config = {
  275. elem: id1
  276. ,url: uploadUrl
  277. ,done: function(res, index, upload){
  278. var id = $(id1).attr('id');
  279. $('#upload_' + id + '_show').attr('src',res.data.src + '?v' + Date.parse(new Date())).show();
  280. $('#update_' + id).val(res.data.src);
  281. }
  282. ,accept: 'images' //允许上传的文件类型
  283. ,size: 5000 //最大允许上传的文件大小
  284. }
  285. upload.render(config);
  286. if (id2) {
  287. var config = {
  288. elem: id2
  289. ,url: uploadUrl
  290. ,done: function(res, index, upload){
  291. var id = $(id2).attr('id');
  292. $('#upload_' + id + '_show').attr('src',res.data.src + '?v' + Date.parse(new Date())).show();
  293. $('#update_' + id).val(res.data.src);
  294. }
  295. ,accept: 'images' //允许上传的文件类型
  296. ,size: 5000 //最大允许上传的文件大小
  297. }
  298. upload.render(config);
  299. }
  300. });
  301. }
  302. }
  303. function msg(value) {
  304. Farm.Msg(value);
  305. }
  306. //瀑布流分页
  307. var _Dever_Page =
  308. {
  309. name : '.paging',
  310. Init : function()
  311. {
  312. if ($(this.name).length) {
  313. var self = this;
  314. $(window).scroll(function() {
  315. if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
  316. self.Start(self.name);
  317. }
  318. });
  319. }
  320. }
  321. ,Start : function(page)
  322. {
  323. page = page ? page : this.name;
  324. if ($(page).length) {
  325. $(page).hide();
  326. var url = $(page + ':last').attr('link');
  327. if (url) {
  328. $(".loading-more").show();
  329. var key = $(page).attr('dever-list');
  330. $.get(url, function(t) {
  331. t = '<div>' + t + '</div>';
  332. var c = $(t).find(key).html();
  333. if (c) {
  334. $(key).append(c);
  335. $(".loading-more").hide();
  336. var l = $(t).find(page).attr('link');
  337. $(page).attr('link', l);
  338. }
  339. });
  340. }
  341. }
  342. }
  343. ,Ajax : function(e)
  344. {
  345. var self = this;
  346. e.find('a').each(function() {
  347. var url = $(this).attr('href');
  348. $(this).attr('href', 'javascript:;');
  349. $(this).unbind('click').bind('click', function() {
  350. $.get(url.replace('callback', ''), function(t) {
  351. e.html(t);
  352. self.Ajax(e);
  353. });
  354. });
  355. });
  356. }
  357. }