12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226 |
- /* ================================================================================
- * dever.js v1.0
- * http://git.shemic.com/dever/script
- * ================================================================================
- * Copyright 2017-2018 Dever(dever.cc)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ================================================================================
- */
- //<filter><script src="../script/lib/jquery/jquery.min.js"></script><script src="../script/dever/core.js"></script></filter>
- $(function()
- {
- Dever.Init();
- });
- //公共类库
- var Dever =
- {
- countdown : -1,
- modal : false,
- Init : function()
- {
- this.Import();
- this.Page().Init();
- this.User().Init();
- this.Editor().Init();
- this.Upload().Init();
- this.Change();
- this.LoadPage();
- this.Extend();
- //this.Target();
- //this.Pjax();
- this.AutoComplete();
- this.Emoji().Init();
- }
- ,AutoComplete : function()
- {
- if ($("input").length) {
- $("input").each(function()
- {
- //$(this).attr('autocomplete', 'new-password');
- $(this).attr('autocomplete', 'off');
- /*
- $(this).attr('readonly', true);
- $(this).focus(function()
- {
- $(this).attr('readonly', false);
- });
- */
- });
- }
- }
-
- ,Pjax : function()
- {
- $(document).pjax('a', '.container');
- //$.pjax.reload('.container');
- }
- ,Emoji : function()
- {
- return _Dever_Emoji;
- }
- ,Page : function()
- {
- return _Dever_Page;
- }
- ,Template : function()
- {
- return _Dever_Template;
- }
-
- ,Editor : function()
- {
- return _Dever_Editor;
- }
- ,User : function()
- {
- return _Dever_User;
- }
-
- ,Modal : function()
- {
- return new _Dever_Modal();
- }
- ,Upload : function()
- {
- return _Dever_Upload;
- }
-
- ,Proxy : function(uri)
- {
- return config.proxy + 'proxy_method=' + encodeURIComponent(uri);
- }
- ,Jump : function(url)
- {
- location.href = url;
- }
-
- ,BackRun : function(url)
- {
- $.post(url);
- }
- ,Reload : function(e)
- {
- e.attr('src', e.attr('src'));
- }
-
- ,Target : function()
- {
- var link = config.host;
- $("a").each(function() {
- if (!$(this).attr('onclick')) {
- var url = $(this).attr('href');
-
- if (url && url.indexOf(link) == -1) {
- //$(this).attr('target', '_blank');
- }
- }
- })
- }
-
- ,Time : function(e, time, text)
- {
- var self = this;
- if (this.countdown < 0) {
- this.countdown = time;
- }
- if (this.countdown == 0) {
- e.attr("disabled", false);
- e.html(text);
- self.Init();
- this.countdown = time;
- } else {
- e.attr("disabled", true);
- e.html(this.countdown + 's后' + text);
- this.countdown--;
- e.unbind('click');
- setTimeout(function() {
- self.Time(e, time, text)
- },1000)
- }
- }
- ,Import : function()
- {
- var self = this;
- var include = $("include");
- var count = include.length-1;
- if (include.length) {
- include.each(function(i){
- var dom = $(this);
- var system = dom.attr('system');
- var path = dom.attr('path');
- var name = dom.attr('file');
- var file = '';
- if(path) file = path;
- if(system) file = file + '/' + system;
-
- file += name + '.html';
- dom.load(file, function(){
- $("html").find("include").eq(i).get(0).outerHTML = dom.html();
- if (i == count) {
- self.Template().Init();
- }
- });
- })
- } else {
- self.Template().Init();
- }
- }
-
- ,LoadPage : function()
- {
- if ($('.dever-loadpage').length) {
- $('.dever-loadpage').each(function() {
- var url = $(this).attr('dever-url');
- var self = $(this);
- $.getJSON(Dever.CallUrl(url), function(t) {
- self.html(t.msg);
- _Dever_Page.Ajax(self);
- });
- });
- }
- }
-
- ,CallUrl : function(url)
- {
- if (url.indexOf('callback') == -1) {
- if (url.indexOf('=') != -1) {
- url += '&json=1&callback=?';
- } else {
- url += '?json=1&callback=?';
- }
- }
-
- return url;
- }
- ,Change : function()
- {
- if ($('.dever-change').length) {
- $('.dever-change').change(function() {
- Dever.Jump($(this).attr('dever-change')+$(this).val());
- });
- //$('.dever-change').val($('.dever-change').attr('dever-value'));
- }
- }
- ,Host : function(host, uri, state)
- {
- var result = '';
-
- uri = uri ? uri : '';
-
- if (state) {
- result = config.host.replace('www', host);
- } else {
- result = config.host;
- }
- result = result.replace('main', host);
-
- if (config.proxy) {
- return this.Proxy(host + '/' + uri);
- }
- return result + uri;
- }
- // 输出数据,提示框
- ,Out : function(content, callback, title)
- {
- if (content && content.indexOf('登录') != -1 && $("#login_url").length) {
- var href = $("#login_url").val();
- location.href = href;
- return;
- } else {
- if (callback) {
- callback = {'yes':callback, 'no':callback};
- }
- if (this.modal) {
- this.modal.Alert(content, callback, title);
- } else {
- this.Modal().Alert(content, callback, title);
- }
- }
- }
- // 将数据解析,并进行下一步操作
- ,Msg : function(result, error_callback, success_callback)
- {
- if (result.status == 2) {
- if (result.data) {
- if (result.data.element) {
- if (result.data.attr) {
- $(result.data.element).attr(result.data.attr, result.data.value);
- } else {
- $(result.data.element).val(result.data.value);
- }
- } else if (result.data.url) {
- location.href = result.data.url;
- }
- }
- if (error_callback) {
- return error_callback(result.msg);
- }
- this.Out(result.msg);
- } else {
- if (typeof(result.data) != 'object') {
- result.msg = result.data;
- }
- if (success_callback) {
- return success_callback(result.msg);
- }
- if (result.msg.indexOf('http://') != -1) {
- var self = this;
- if (result.msg.indexOf('||') != -1) {
- var temp = result.msg.split('||');
- result.msg = temp[0];
- result.data = temp[1];
- this.Out(result.data, function() {
- self.Jump(result.msg);
- });
- } else {
- self.Jump(result.msg);
- }
-
- } else if (parseInt(result.msg) > 0) {
- this.Out('操作成功', function() {
- location.reload();
- });
- } else {
- this.Out(result.msg);
- }
- }
- }
- ,Confirm : function(content, callback, title)
- {
- if (this.modal) {
- this.modal.Confirm(content, {'yes':callback}, title);
- } else {
- this.Modal().Confirm(content, {'yes':callback}, title);
- }
- }
-
- ,Extend : function()
- {
- Array.prototype.indexIn = function(val) {
- for (var i = 0; i < this.length; i++) {
- if (this[i] == val) return i;
- }
- return -1;
- };
- Array.prototype.remove = function(val) {
- var index = this.indexIn(val);
- if (index > -1) {
- this.splice(index, 1);
- }
- };
- $("select option").each(function() {
- if ($(this).attr('select') && $(this).attr('select') == 'true') {
- $(this).attr('selected', 'true');
- } else {
- $(this).removeAttr('selected');
- }
- });
- }
- ,Ucfirst : function(str)
- {
- var str = str.toLowerCase();
- var strarr = str.split(' ');
- var result = '';
- for (var i in strarr){
- result += strarr[i].substring(0,1).toUpperCase()+strarr[i].substring(1)+' ';
- }
- return result;
- }
- };
- //瀑布流分页
- var _Dever_Page =
- {
- name : '',
- loading : '',
- button : '',
- state :false,
- Init : function()
- {
- if ($(this.name).length) {
- var self = this;
- if (navigator.userAgent.match(/mobile/i)) {
- $(window).scroll(function() {
- if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
- self.Start(self.name, self.loading);
- }
- });
- }
- if (self.button && $(self.button).length) {
- $(self.button).click(function() {
- self.Start();
- })
- }
- }
- }
- ,Start : function(page, loading)
- {
- page = page ? page : this.name;
- loading = loading ? loading : this.loading;
-
- if ($(page).length) {
- $(page).hide();
- var url = $(page).attr('dever-page-link');
- if (url) {
- var key = $(page).attr('dever-page-list');
- if ($(loading).length) {
- $(loading).show();
- }
- var self = this;
- if (self.state) {
- return;
- }
- self.state = true;
- $.get(url, function(t) {
- self.state = false;
- t = '<div>' + t + '</div>';
- var c = $(t).find(key).html();
- if ($(loading).length) {
- $(loading).hide();
- }
- if (self.button && $(self.button).length) {
- var button = $(t).find(self.button).attr('dever-show');
- if (button == 2) {
- $(self.button).hide();
- }
- }
- if (c) {
- $(key).append(c);
- var l = $(t).find(page).attr('dever-page-link');
- $(page).attr('dever-page-link', l);
- //Dever.Init();
- var callback = $(page).attr('dever-page-call');
- if (callback) {
- eval(callback);
- }
- }
- });
- }
- }
- }
-
- ,Ajax : function(e)
- {
- var self = this;
- e.find('a').each(function() {
- var url = $(this).attr('href');
-
- $(this).attr('href', 'javascript:;');
-
- $(this).unbind('click').bind('click', function() {
- $.get(url.replace('callback', ''), function(t) {
- e.html(t);
- self.Ajax(e);
- });
- });
- });
- }
- }
- //模态框 非静态类
- var _Dever_Modal = function()
- {
- var self = this;
- self.state = false;
- self.html = '';
- self.box = '';
- self.title = '';
- self.content = '';
- self.yes = '';
- self.no = '';
- self.setting = '';
- self.confirm = '';
- self.open = false;
- self.Init = function()
- {
- this.state = false;
- if (this.html) {
- $('body').append(this.html);
- }
- if (this.box && this.content) {
- this.box = $(this.box);
- if (this.box.length) {
- this.box.hide();
- this.content = $(this.content);
- if(this.title) this.title = $(this.title);
- 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);
- if(!this.timeout) this.timeout = 3000;
- this.state = true;
- }
- }
- };
-
- self.Alert = function(content, callback, title)
- {
- if (self.open == true) {
- return;
- }
- self.open = true;
- if (this.state == false) {
- alert(content);
- if (callback) {
- callback.yes();
- }
- } else {
- if (this.setting.length && this.setting.html()) {
- if (this.confirm) this.confirm.hide();
- this.setting.show();
- } else {
- if (this.setting) this.setting.hide();
- if (this.confirm) this.confirm.show();
- }
- this.content.html(content);
- if (title) {
- this.title.html(title);
- }
- this.box.show();
- if (this.yes && callback.yes) {
- this.Bind(callback);
- } else {
- this.TimeOut(callback);
- }
- }
- };
-
- self.Confirm = function(content, callback, title)
- {
- if (self.open == true) {
- return;
- }
- self.open = true;
- if (this.state == false) {
- if (confirm(content)) {
- callback.yes();
- }
- } else {
- if (this.confirm) this.confirm.show();
- if (this.setting) this.setting.hide();
-
- this.content.html(content);
- if (title) {
- this.title.html(title);
- }
- this.box.show();
- if (callback.yes) {
- this.Bind(callback);
- }
- }
- }
-
- self.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();
- });
- }
- }
- };
- self.TimeOut = function(callback)
- {
- var self = this;
- setTimeout(function()
- {
- if (callback && callback.yes) {
- callback.yes();
- } else {
- self.Close();
- }
-
- }, self.timeout);
- }
-
- self.Close = function()
- {
- this.open = false;
- this.box.hide();
- }
- return self;
- };
- //用户相关 静态类
- var _Dever_User =
- {
- config : [],
- uid : -1,
- name : '匿名用户',
- //按钮不可点击的样式
- disable : '',
- Init : function()
- {
- var self = this;
- if (this.config.click) {
- for (var i in this.config.click) {
- var e = self.config.click[i];
- var c = 1;
- if (e.indexOf('|') != -1) {
- var t = e.split('|');
- e = t[0];
- c = 2;
- }
- var e = $(e);
- if (e.length) {
- e.attr('data-i', i);
- e.attr('data-c', c);
- if (self.disable) {
- e.addClass(self.disable);
- self.DisableCheck(e);
- } else {
- self.Bind(e);
- }
- }
- }
- }
- }
- ,Bind : function(e)
- {
- var self = this;
- e.unbind('click').bind('click', function() {
- if ($(this).attr('data-c') == 2) {
- Dever.Confirm(function() {
- self.Save($(this), config);
- });
- } else {
- self.Save($(this), config);
- }
- });
- }
- ,DisableCheck : function(e)
- {
- var self = this;
- var key = e.attr('data-i');
- var config = self.config[key];
- var callback = function(msg,state) {
- e.addClass(self.disable);
- }
- var blur = function() {
- var send = self.Check(e, callback, config);
- if (!send) {
- e.addClass(self.disable);
- return;
- }
- e.removeClass(self.disable);
- self.Bind(e);
- }
- var send = self.Check(e, callback, config, blur);
- if (!send) {
- callback(config.error);
- return;
- }
- e.removeClass(self.disable);
- self.Bind(e);
- }
- ,Check : function(e, callback, config, blur)
- {
- var self = this;
- var send = {};
- if (typeof(config.input) != 'undefined') {
- if (typeof(config.input) == 'string' && config.input == 'parent') {
- if (e.attr('dever-send')) {
- send[e.attr('dever-send')] = e.attr('dever-' + e.attr('dever-send'));
- }
- send.id = e.attr('dever-id');
- send.value = e.parent().find('input').val();
- if (!send.id || !send.value) {
- callback(config.error);
- return false;
- }
- } else if (typeof(config.input) == 'string' && config.input == 'click') {
- if (e.attr('dever-send')) {
- var a = e.attr('dever-send').split('&');
- for (var i in a) {
- if (typeof(a[i]) == 'string') {
- var b = a[i].split('=');
- send[b[0]] = b[1];
- }
- }
- }
- } else if (typeof(config.input) == 'object') {
- if (blur) {
- for (var i in config.input) {
- var input = config.input[i];
- if (typeof(input) == 'string') {
- if (input.indexOf('|') != -1) {
- var t = input.split('|');
- input = t[0];
- }
- var el = $(input);
- el.on('input',function()
- {
- blur();
- });
- el.blur(function()
- {
- blur();
- });
- }
- }
- }
-
- for (var i in config.input) {
- var option = false;
- var input = config.input[i];
- if (typeof(input) == 'object' || typeof(input) == 'function') {
- send[i] = input();
- } else {
- var select = [];
- if (input.indexOf('|') != -1) {
- var t = input.split('|');
- input = t[0];
- option = true;
- if (t[1]) {
- select = t;
- }
- }
- var el = $(input);
- //e.get(0).tagName == 'TEXTAREA'
- send[i] = el.val();
- if (option == false) {
- state = self.Match(el, send[i], callback, config);
- if (!state) {
- return state;
- }
- } else if(select) {
- //只要有一个为真即可
- var result = false;
- if (select.length <= 0) {
- result = true;
- }
- for (var i in select) {
- if (typeof(select[i]) == 'string') {
- var el = $(select[i]);
- state = self.Match($(el), $(el).val(), false, config);
- if (state) {
- result = true;
- }
- }
- }
- if (result == false) {
- return result;
- }
- }
- }
- }
- }
- }
- return send;
- }
- ,Match : function(e, value, callback, config)
- {
- var m = true;
- if (value && e.attr('dever-match')) {
- var r = new RegExp(e.attr('dever-match'));
- m = r.test(value);
- }
- if (value && e.attr('dever-value')) {
- m = $(e.attr('dever-value')).val() == value;
- }
- if (!value || m != true) {
- if (callback) {
- var error = e.attr('dever-error') ? e.attr('dever-error') : config.error;
- callback(error);
- }
-
- return false;
- }
- return true;
- }
- //通用的保存数据功能
- ,Save : function(e, setting)
- {
- var self = this;
- var key = e.attr('data-i');
- var config = self.config[key];
- if (config.start) {
- config.start.call();
- }
-
- var callback = function(msg,state) {
- if (config.status && $(config.status).length) {
- //$(config.status).html(msg).css('visibility','initial');
- state ? $(config.status).html(msg).hide() : $(config.status).html(msg).show();
- } else if(!state) {
- Dever.Out(msg);
- }
- }
- var send = self.Check(e, callback, config);
- if (!send) {
- //callback(config.error);
- return;
- }
-
- if (!config.url) {
- config.url = e.attr('dever-send');
- }
- if (e.attr('dever-refresh')) {
- config.url = e.attr('dever-refresh');
- }
-
- callback(1,1);
-
- config.url = Dever.CallUrl(config.url);
- if (setting.proxy) {
- $.post(config.url, send, function(t) {
- t = eval('(' + t + ')');
- Dever.Msg(t, callback, (config.callback ? function(msg){config.callback(e,msg,t)} : false));
- });
- } else {
- $.getJSON(config.url, send, function(t) {
- Dever.Msg(t, callback, (config.callback ? function(msg){config.callback(e,msg,t)} : false));
- });
- }
- }
- };
- //编辑器相关
- var _Dever_Editor =
- {
- state : false,
- editors : [],
- Init : function()
- {
- if (typeof marked == 'function') {
- this.state = true;
- } else {
- this.state = false;
- }
- this.SetMark();
- var self = this;
-
- if ($('.dever-note').length) {
- $('.dever-note').each(function() {
- self.InitEditor($(this));
- })
- }
- }
- ,InitEditor : function(e)
- {
- var id = e.attr('id');
- var key = e.attr('key');
- if (config.uid && config.uid > 0) {
- var toolbar = [
- 'title'
- ,'bold'
- ,'italic'
- ,'underline'
- ,'strikethrough'
- ,'fontScale'
- ,'color'
- ,'ol'
- ,'ul'
- ,'blockquote'
- ,'code'
- ,'table'
- ,'link'
- ,'image'
- ,'hr'
- ,'indent'
- ,'outdent'
- ,'alignment'
- //,'emoji'
- ];
- } else {
- var toolbar = [
- 'title'
- ,'bold'
- ,'fontScale'
- ,'color'
- ,'ol'
- ,'ul'
- ,'blockquote'
- ,'indent'
- ,'outdent'
- ,'alignment'
- ];
- }
-
- if (typeof(this.editors[id]) == "undefined") {
- this.editors[id] = new Simditor({
- textarea: e,
- upload:
- {
- url: config.upload + '.simditor',
- params: {key:key},
- fileKey: 'file',
- connectionCount: 10,
- leaveConfirm: 'Uploading is in progress, are you sure to leave this page?'
- },
- toolbar : toolbar
- /*
- ,emoji:
- {
- imagePath: config.lib + 'simditor/plugins/emoji/images/emoji/'
- }
- */
- });
- }
- }
-
- ,SetMark : function()
- {
- return;
- }
-
- ,GetMark : function(content)
- {
- if (this.state == true) {
- return marked(content);
- }
-
- return content;
- }
- };
- //模板
- var _Dever_Template =
- {
- state : false,
- method : '',
- element : '',
- data : '',
- param : '',
- attr : '',
- Init : function()
- {
- if (this.Check()) {
- var url = config.proxyTemplateUrl;
- var send = {};
- var parsing = [];
- $("dever").each(function(){
- parsing.push($(this).html());
- $(this).remove();
- });
- send.url = location.href;
- send.parsing = parsing.join('');
- send.html = $("html").html();
- //$("html").html('dever template loading...');
- $.ajax({
- type: "POST",
- url: url,
- data: send,
- dataType: "json",
- success: function(result){
- $("html").html(result.data);
- }
- });
- }
- }
- ,Check : function()
- {
- if ($("dever").length) {
- this.state = true;
- } else {
- this.state = false;
- }
- return this.state;
- }
- }
- //上传 请载入layui
- var _Dever_Upload =
- {
- upload : [],
- upload_file : [],
- upload_pic : [],
- callback : {},
- Init : function()
- {
- var self = this;
- if ($(".dever-upload-drag").length) {
- $(".dever-upload-drag").each(function() {
- var key = $(this).attr('key');
-
- if (config.proxy) {
- var url = Dever.Proxy('upload/save.drag') + '&key=' + key;
- } else {
- var url = config.upload + '.drag?key='+ key;
- }
- $(this).inlineattachment({
- uploadUrl: url,
- progressText: '![文件上传中...]()',
- urlText: "\n",
- errorText: '上传失败'
- });
- });
- }
- if ($(".dever-upload").length) {
- $(".dever-upload").each(function(i) {
- self.Load(i,$(this));
- })
- }
- }
- ,Load : function(i,e)
- {
- var self = this;
- var id = e.attr('id');
- var value = e.attr('dever-upload-value');
- var key = e.attr('dever-upload-key');
- var pic = e.attr('dever-upload-pic');
- var type = e.attr('dever-upload-type');
- var callback = e.attr('dever-upload-callback');
- var input = $("#" + value);
- if (!input.length) {
- return;
- }
- var url = config.upload + '.start';
- if (typeof(this.upload[value]) != "undefined") {
- return;
- }
- if (!key) {
- key = 1;
- }
- self.upload[value] = true;
- self.upload_pic[value] = [];
- self.upload_file[value] = [];
- var input_value = input.val();
- if (input_value && type == 'mul') {
- var input_value_array = input_value.split(',');
- for (var i in input_value_array) {
- self.Set(type, e, input, value, callback, input_value_array[i]);
- }
- }
-
- layui.use(['upload','layer'], function() {
- var layer = layui.layer;
- var layuiUpload = layui.upload;
- var uploadInst = layuiUpload.render({
- elem: '#' + id
- ,data: {'key' : key}
- ,field: 'file'
- ,url: url
- ,multiple: true
- ,xhr:xhrOnProgress
- ,before: function(obj) {
- layer.load();
- }
- ,done: function(data) {
- layer.closeAll('loading');
- if (data.status == 1) {
- self.Set(type, e, input, value, callback, data.url, pic);
- } else {
- layer.msg(data.msg, {icon: 4});
- return false;
- }
- }
- ,error: function() {
- layer.closeAll('loading');
- layer.msg('上传失败', {icon: 4});
- return false;
- }
- });
- });
- }
- ,Set : function(type, e, input, value, callback, url, pic)
- {
- if (type == 'mul') {
- this.SetMul(e, input, value, callback, url);
- } else {
- this.SetOne(e, input, value, callback, url, pic);
- }
- }
- ,SetOne : function(e, input, value, callback, url, pic)
- {
- var html = '';
- if (this.callback && this.callback[callback]) {
- html = this.callback[callback].call(this, e, url, 'dever-upload-close');
- } else {
- pic = $("#" + pic);
- if (pic.length) {
- pic.attr('src', url).show();
- } else {
- html = '<img src="'+url+'" class="dever-upload-pic">';
- }
- }
- if (html) {
- e.html(html);
- }
- var input_value = input.val();
- if (input_value != url) {
- input.val(url);
- this.Blur(input);
- }
- }
- ,SetMul : function(e, input, value, callback, url)
- {
- var html = '';
- var input_value = input.val();
- if (this.upload_pic[value].length <= 0 && input_value) {
- this.upload_pic[value] = input_value.split(',');
- }
- if (this.callback && this.callback[callback]) {
- html += this.callback[callback].call(this, e, url, 'dever-upload-close');
- } else {
- html += '<li><img src="'+url+'" class="dever-upload-pics"><em class="dever-upload-close"></em></li>';
- }
- if (html) {
- e.before(html);
- }
- this.Close(value);
- if (input_value.indexOf(url) == -1) {
- this.upload_pic[value].push(url);
- input.val(this.upload_pic[value].join(','));
- this.Blur(input);
- }
- }
- ,Blur : function(input)
- {
- var events = $._data(input[0], 'events');
- if (events && events["blur"]) {
- input.blur();
- }
- }
- ,Close : function(v)
- {
- var self = this;
- $('.dever-upload-close').each(function()
- {
- $(this).unbind('click').bind('click', function() {
- var e = $(this).parent();
- var p = e.find('img').attr('src');
- e.remove();
- self.upload_pic[v].remove(p);
- $("#" + v).val(self.upload_pic[v].join(','));
- });
- });
- }
- }
- var _Dever_Emoji =
- {
- Init : function()
- {
- var self = this;
- if ($('.dever-emoji').length) {
- $('.dever-emoji').each(function()
- {
- self.Get($(this), $(this).html());
- })
- }
- }
- ,Get : function(e, content)
- {
- var handle = function()
- {
- e.html(twemoji.parse(content)).show(200);
- };
- if (typeof jEmoji != 'object') {
- $.getScript('//twemoji.maxcdn.com/2/twemoji.min.js?11.4', function() {
- /*
- $("head").append("<link>");
- var css = $("head").children(":last");
- css.attr({
- rel: "stylesheet",
- type: "text/css",
- href: config.script + '/lib/emoji/emoji.css'
- });
- */
- return handle();
- });
- } else {
- return handle();
- }
- }
- }
- //创建监听函数
- var xhrOnProgress=function(fun) {
- xhrOnProgress.onprogress = fun; //绑定监听
- //使用闭包实现监听绑
- return function() {
- //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
- var xhr = $.ajaxSettings.xhr();
- //判断监听函数是否为函数
- if (typeof xhrOnProgress.onprogress !== 'function') return xhr;
- //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
- if (xhrOnProgress.onprogress && xhr.upload) {
- xhr.upload.onprogress = xhrOnProgress.onprogress;
- }
- return xhr;
- }
- }
|