common.js 8.6 KB

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