common.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. $(function()
  2. {
  3. Maze.Init();
  4. });
  5. //公共类库
  6. var Maze =
  7. {
  8. Init : function()
  9. {
  10. this.Page().Init();
  11. this.User().Init();
  12. //this.Modal().Init();
  13. this.Change();
  14. }
  15. ,Page : function()
  16. {
  17. return _Maze_Page;
  18. }
  19. ,User : function()
  20. {
  21. return _Maze_User;
  22. }
  23. ,Modal : function()
  24. {
  25. return _Maze_Modal;
  26. }
  27. ,Jump : function(url)
  28. {
  29. location.href = url;
  30. }
  31. ,BackRun : function(url)
  32. {
  33. $.post(url);
  34. }
  35. ,Change : function()
  36. {
  37. if($('.maze-change').length)
  38. {
  39. $('.maze-change').change(function()
  40. {
  41. Maze.Jump($(this).attr('maze-change')+$(this).val());
  42. });
  43. //$('.maze-change').val($('.maze-change').attr('maze-value'));
  44. }
  45. }
  46. ,Host : function(host)
  47. {
  48. var result = '';
  49. result = config.host.replace('www', host);
  50. result = result.replace('main', host);
  51. result = result + config.type;
  52. result = result.replace('??', '?');
  53. return result;
  54. }
  55. // 输出数据,提示框
  56. ,Out : function(content, callback, ele, title)
  57. {
  58. if(content && content.indexOf('登录') != -1 && $("#login_url").length)
  59. {
  60. var href = $("#login_url").val();
  61. location.href = href;
  62. return;
  63. }
  64. else
  65. {
  66. alert(content);
  67. if(ele && callback)
  68. {
  69. ele.unbind('click').bind('click', callback);
  70. }
  71. else if(callback)
  72. {
  73. callback();
  74. }
  75. }
  76. }
  77. // 将数据解析,并进行下一步操作
  78. ,Msg : function(result, error_callback, success_callback)
  79. {
  80. if(result.status == 2)
  81. {
  82. if(error_callback)
  83. {
  84. return error_callback(result.msg);
  85. }
  86. this.Out(result.msg);
  87. }
  88. else
  89. {
  90. if(success_callback)
  91. {
  92. return success_callback(result.msg);
  93. }
  94. if(result.msg.indexOf('http://') != -1)
  95. {
  96. this.Jump(result.msg);
  97. }
  98. else if(parseInt(result.msg) > 0)
  99. {
  100. this.Out('操作成功', function()
  101. {
  102. location.reload();
  103. });
  104. }
  105. else
  106. {
  107. this.Out(result.msg);
  108. }
  109. }
  110. }
  111. ,Confirm : function(callback)
  112. {
  113. if(confirm('确定进行此项操作吗?'))
  114. {
  115. callback();
  116. }
  117. }
  118. //上传组件加载
  119. ,Upload : function()
  120. {
  121. if($(".image_upload").length)
  122. {
  123. $(".image_upload").each(function(i)
  124. {
  125. loadUpload(i,$(this),$(this).attr('key'),config.upload + '.start', config.assets + 'image/');//三个参数说明1:第几个上传框2:文件对象3:图片的基本配置标题
  126. })
  127. }
  128. }
  129. ,Load : function(filename,filetype)
  130. {
  131. if(filetype == "js")
  132. {
  133. var fileref = document.createElement('script');
  134. fileref.setAttribute("type","text/javascript");
  135. fileref.setAttribute("src",filename + '.' + filetype);
  136. }
  137. else if(filetype == "css")
  138. {
  139. var fileref = document.createElement('link');
  140. fileref.setAttribute("rel","stylesheet");
  141. fileref.setAttribute("type","text/css");
  142. fileref.setAttribute("href",filename + '.' + filetype);
  143. }
  144. if(typeof fileref != "undefined")
  145. {
  146. document.getElementsByTagName("head")[0].appendChild(fileref);
  147. }
  148. }
  149. };
  150. //瀑布流分页
  151. var _Maze_Page =
  152. {
  153. name : '',
  154. Init : function()
  155. {
  156. if($(this.name).length)
  157. {
  158. var self = this;
  159. $(window).scroll(function()
  160. {
  161. if($(window).scrollTop() >= $(document).height() - $(window).height())
  162. {
  163. self.Start(self.name);
  164. }
  165. });
  166. }
  167. }
  168. ,Start : function(page)
  169. {
  170. page = page ? page : this.name;
  171. if($(page).length)
  172. {
  173. $(page).hide();
  174. var url = $(page).attr('link');
  175. if(url)
  176. {
  177. var key = $(page).attr('data-list');
  178. $.get(url, function(t)
  179. {
  180. t = '<div>' + t + '</div>';
  181. var c = $(t).find(key).html();
  182. if(c)
  183. {
  184. $(key).append(c);
  185. var l = $(t).find(page).attr('link');
  186. $(page).attr('link', l);
  187. }
  188. });
  189. }
  190. }
  191. }
  192. }
  193. //模态框
  194. var _Maze_Modal =
  195. {
  196. state : false,
  197. html : '',
  198. box : '',
  199. title : '',
  200. content : '',
  201. yes : '',
  202. no : '',
  203. setting : '',
  204. confirm : '',
  205. Init : function()
  206. {
  207. this.state = false;
  208. if(this.html)
  209. {
  210. $('body').append(this.html);
  211. if(this.box) this.box = $(this.box);
  212. if(this.title) this.title = $(this.title);
  213. if(this.content) this.content = $(this.content);
  214. if(this.yes) this.yes = $(this.yes);
  215. if(this.no) this.no = $(this.no);
  216. if(this.setting) this.setting = $(this.setting);
  217. if(this.confirm) this.confirm = $(this.confirm);
  218. this.state = true;
  219. }
  220. }
  221. ,Alert : function(content, callback, body)
  222. {
  223. if(this.state == false)
  224. {
  225. alert(content);
  226. return;
  227. }
  228. else
  229. {
  230. if(this.setting.length && this.setting.html())
  231. {
  232. this.confirm.hide();
  233. this.setting.show();
  234. }
  235. else
  236. {
  237. this.setting.hide();
  238. this.confirm.show();
  239. }
  240. this.title.html(content);
  241. if(body)
  242. {
  243. this.content.html(body).show();
  244. }
  245. else
  246. {
  247. this.content.html('').hide();
  248. }
  249. this.box.show();
  250. if(callback)
  251. {
  252. this.Bind(callback);
  253. }
  254. }
  255. }
  256. ,Confirm : function(content, callback, body)
  257. {
  258. if(this.state == false)
  259. {
  260. alert(content);
  261. return;
  262. }
  263. else
  264. {
  265. this.confirm.show();
  266. this.setting.hide();
  267. this.title.html(content);
  268. if(body)
  269. {
  270. this.content.html(body).show();
  271. }
  272. else
  273. {
  274. this.content.html('').hide();
  275. }
  276. this.box.show();
  277. if(callback)
  278. {
  279. this.Bind(callback);
  280. }
  281. }
  282. }
  283. ,Bind : function(callback)
  284. {
  285. if(this.yes.length && callback.yes)
  286. {
  287. this.yes.unbind('click').bind('click', callback.yes);
  288. }
  289. if(this.no.length)
  290. {
  291. if(callback.no)
  292. {
  293. this.no.unbind('click').bind('click', callback.no);
  294. }
  295. else
  296. {
  297. var self = this;
  298. this.no.unbind('click').bind('click', function()
  299. {
  300. self.Close();
  301. });
  302. }
  303. }
  304. }
  305. ,Close : function()
  306. {
  307. this.box.hide();
  308. }
  309. }
  310. //用户相关
  311. var _Maze_User =
  312. {
  313. config : [],
  314. uid : -1,
  315. name : '匿名用户',
  316. Init : function()
  317. {
  318. var self = this;
  319. if(this.config.click)
  320. {
  321. for(var i in this.config.click)
  322. {
  323. var e = this.config.click[i];
  324. var c = false;
  325. if(e.indexOf('|') != -1)
  326. {
  327. var t = e.split('|');
  328. e = t[0];
  329. c = true;
  330. }
  331. if($(e).length)
  332. {
  333. var index = i;
  334. var con = c;
  335. $(e).unbind('click').bind('click', function()
  336. {
  337. if(con == true)
  338. {
  339. var t1 = $(this);
  340. Maze.Confirm(function()
  341. {
  342. self.Save(index, t1);
  343. });
  344. }
  345. else
  346. {
  347. self.Save(index, $(this));
  348. }
  349. });
  350. }
  351. }
  352. }
  353. }
  354. //通用的保存数据功能
  355. ,Save : function(key, e)
  356. {
  357. var self = this;
  358. var config = self.config[key];
  359. var send = {};
  360. var callback = function(msg)
  361. {
  362. if(config.status && $(config.status).length)
  363. {
  364. $(config.status).html(msg).css('visibility','initial');
  365. }
  366. else
  367. {
  368. Maze.Out(msg);
  369. }
  370. }
  371. if(typeof(config.input) != 'undefined')
  372. {
  373. if(typeof(config.input) == 'string' && config.input == 'parent')
  374. {
  375. if(e.attr('data-send'))
  376. {
  377. send[e.attr('data-send')] = e.attr('data-' + e.attr('data-send'));
  378. }
  379. send.id = e.attr('data-id');
  380. send.value = e.parent().find('input').val();
  381. if(!send.id || !send.value)
  382. {
  383. callback(config.error);
  384. return;
  385. }
  386. }
  387. else if(typeof(config.input) == 'object')
  388. {
  389. for(var i in config.input)
  390. {
  391. send[i] = $(config.input[i]).val();
  392. if(!send[i])
  393. {
  394. callback(config.error);
  395. return;
  396. }
  397. }
  398. }
  399. }
  400. if(!send)
  401. {
  402. callback(config.error);
  403. return;
  404. }
  405. $.getJSON(config.url + '?callback=?', send, function(t)
  406. {
  407. Maze.Msg(t, callback);
  408. });
  409. }
  410. };