$('.layer').click(function(event) { $(this).hide(); }); $('.layer article').click(function(event) { event.stopPropagation(); }); $(function() { Maze.Init(); }); //公共类库 var Maze = { Init : function() { this.Page().Init(); this.User().Init(); //this.Modal().Init(); this.Change(); } ,Page : function() { return _Maze_Page; } ,User : function() { return _Maze_User; } ,Modal : function() { return _Maze_Modal; } ,Jump : function(url) { location.href = url; } ,BackRun : function(url) { $.post(url); } ,Change : function() { if($('.maze-change').length) { $('.maze-change').change(function() { Maze.Jump($(this).attr('maze-change')+$(this).val()); }); //$('.maze-change').val($('.maze-change').attr('maze-value')); } } ,Host : function(host) { var result = ''; result = config.host.replace('www', host); result = result.replace('front', host); result = result + config.type; result = result.replace('??', '?'); return result; } // 输出数据,提示框 ,Out : function(content, callback, ele, title) { if(content && content.indexOf('登录') != -1 && $("#login_url").length) { var href = $("#login_url").val(); location.href = href; return; } else { alert(content); if(ele && callback) { ele.unbind('click').bind('click', callback); } else if(callback) { callback(); } } } // 将数据解析,并进行下一步操作 ,Msg : function(result, error_callback, success_callback) { if(result.status == 2) { if(error_callback) { return error_callback(result.msg); } this.Out(result.msg); } else { if(success_callback) { return success_callback(result.msg); } if(result.msg.indexOf('http://') != -1) { this.Jump(result.msg); } else if(parseInt(result.msg) > 0) { this.Out('操作成功', function() { location.reload(); }); } else { this.Out(result.msg); } } } ,Confirm : function(callback) { if(confirm('确定进行此项操作吗?')) { callback(); } } //上传组件加载 ,Upload : function() { if($(".image_upload").length) { $(".image_upload").each(function(i) { loadUpload(i,$(this),$(this).attr('key'),config.upload + '.start', config.assets + 'image/');//三个参数说明1:第几个上传框2:文件对象3:图片的基本配置标题 }) } } ,Load : function(filename,filetype) { if(filetype == "js") { var fileref = document.createElement('script'); fileref.setAttribute("type","text/javascript"); fileref.setAttribute("src",filename + '.' + filetype); } else if(filetype == "css") { var fileref = document.createElement('link'); fileref.setAttribute("rel","stylesheet"); fileref.setAttribute("type","text/css"); fileref.setAttribute("href",filename + '.' + filetype); } if(typeof fileref != "undefined") { document.getElementsByTagName("head")[0].appendChild(fileref); } } }; //瀑布流分页 var _Maze_Page = { name : '', Init : function() { if($(this.name).length) { var self = this; $(window).scroll(function() { if($(window).scrollTop() >= $(document).height() - $(window).height()) { self.Start(self.name); } }); } } ,Start : function(page) { page = page ? page : this.name; if($(page).length) { $(page).hide(); var url = $(page).attr('link'); if(url) { var key = $(page).attr('data-list'); $.get(url, function(t) { t = '
' + t + '
'; var c = $(t).find(key).html(); if(c) { $(key).append(c); var l = $(t).find(page).attr('link'); $(page).attr('link', l); } }); } } } } //模态框 var _Maze_Modal = { state : false, html : '', box : '', title : '', content : '', yes : '', no : '', setting : '', confirm : '', Init : function() { this.state = false; if(this.html) { $('body').append(this.html); if(this.box) this.box = $(this.box); if(this.title) this.title = $(this.title); if(this.content) this.content = $(this.content); if(this.yes) this.yes = $(this.yes); if(this.no) this.no = $(this.no); if(this.setting) this.setting = $(this.setting); if(this.confirm) this.confirm = $(this.confirm); this.state = true; } } ,Alert : function(content, callback, body) { if(this.state == false) { var self = this; if(confirm(content)) { if(callback) { callback.yes(); } } return; } else { if(this.setting.length && this.setting.html()) { this.confirm.hide(); this.setting.show(); } else { this.setting.hide(); this.confirm.show(); } this.title.html(content); if(body) { this.content.html(body).show(); } else { this.content.html('').hide(); } this.box.show(); if(callback) { this.Bind(callback); } } } ,Confirm : function(content, callback, body) { if(this.state == false) { var self = this; if(confirm(content)) { if(callback) { callback.yes(); } } return; } else { this.confirm.show(); this.setting.hide(); this.title.html(content); if(body) { this.content.html(body).show(); } else { this.content.html('').hide(); } this.box.show(); if(callback) { this.Bind(callback); } } } ,Bind : function(callback) { if(this.yes.length && callback.yes) { this.yes.unbind('click').bind('click', callback.yes); } if(this.no.length) { if(callback.no) { this.no.unbind('click').bind('click', callback.no); } else { var self = this; this.no.unbind('click').bind('click', function() { self.Close(); }); } } } ,Close : function() { this.box.hide(); } } //用户相关 var _Maze_User = { config : [], uid : -1, name : '匿名用户', Init : function() { var self = this; if(this.config.click) { for(var i in this.config.click) { var e = this.config.click[i]; var c = false; if(e.indexOf('|') != -1) { var t = e.split('|'); e = t[0]; c = true; } if($(e).length) { var index = i; var con = c; $(e).unbind('click').bind('click', function() { if(self.config[index].start) { self.config[index].start.call(); } if(con == true) { var t1 = $(this); Maze.Confirm(function() { self.Save(index, t1); }); } else { self.Save(index, $(this)); } }); } } } } //通用的保存数据功能 ,Save : function(key, e) { var self = this; var config = self.config[key]; var send = {}; var callback = function(msg) { if(config.status && $(config.status).length) { $(config.status).html(msg).css('visibility','initial'); } else { Maze.Out(msg); } } if(typeof(config.input) != 'undefined') { if(typeof(config.input) == 'string' && config.input == 'parent') { if(e.attr('data-send')) { send[e.attr('data-send')] = e.attr('data-' + e.attr('data-send')); } send.id = e.attr('data-id'); send.value = e.parent().find('input').val(); if(!send.id || !send.value) { callback(config.error); return; } } else if(typeof(config.input) == 'object') { for(var i in config.input) { var option = false; var input = config.input[i]; if(input.indexOf('|') != -1) { var t = input.split('|'); input = t[0]; option = true; } var m = true; var el = $(input); if(el.length) { send[i] = el.val(); } else { send[i] = input; } //e.get(0).tagName == 'TEXTAREA' if(option == false) { if(send[i] && el.attr('dever-match')) { var r = new RegExp(el.attr('dever-match')); m = r.test(send[i]); } if(send[i] && el.attr('dever-value')) { m = $(el.attr('dever-value')).val() == send[i]; } if(!send[i] || m != true) { var error = el.attr('dever-error') ? el.attr('dever-error') : config.error; callback(error); return; } } } } } if(!send) { callback(config.error); return; } $.getJSON(config.url + '?callback=?&project=front', send, function(t) { Maze.Msg(t, callback); }); } };