123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760 |
- /* ================================================================================
- * 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="../js/jquery.min.js"></script><script src="../js/dever.js"></script></filter>
- $(function()
- {
- Dever.Init();
- });
- //公共类库
- var Dever =
- {
- Init : function()
- {
- this.Import();
- this.Page().Init();
- this.User().Init();
- this.Editor().Init();
- this.Change();
- this.Upload();
- this.LoadPage();
- this.Extend();
- //this.Target();
- //this.Pjax();
- }
-
- ,Pjax : function()
- {
- $(document).pjax('a', '.container');
- //$.pjax.reload('.container');
- }
- ,Page : function()
- {
- return _Dever_Page;
- }
- ,Template : function()
- {
- return _Dever_Template;
- }
-
- ,Editor : function()
- {
- return _Dever_Editor;
- }
- ,User : function()
- {
- return _Dever_User;
- }
-
- ,Modal : function()
- {
- return _Dever_Modal;
- }
-
- ,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');
- }
- }
- })
- }
-
- ,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)
- {
- var result = '';
-
- uri = uri ? uri : '';
-
- result = config.host.replace('www', host);
- result = result.replace('main', host);
- result = result + config.type;
-
- result = result.replace('??', '?');
-
- if (config.proxy) {
- return this.Proxy(host + '/' + uri);
- }
- return result + uri;
- }
- // 输出数据,提示框
- ,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 ($(".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: "![文件描述]({filename})\n",
- errorText: '上传失败'
- });
- });
- }
- if ($(".dever-upload").length) {
- $(".dever-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);
- }
- }
-
- ,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 : '',
- 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('dever-list');
- $.get(url, function(t) {
- t = '<div>' + t + '</div>';
- var c = $(t).find(key).html();
- if (c) {
- $(key).append(c);
- var l = $(t).find(page).attr('link');
- $(page).attr('link', l);
- }
- });
- }
- }
- }
-
- ,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 =
- {
- 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)
- {
- if (this.state == false) {
- alert(content);
- 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);
- this.box.show();
- if (callback) {
- this.Bind(callback);
- }
- }
- }
-
- ,Confirm : function(content, callback)
- {
- if (this.state == false) {
- alert(content);
- return;
- } else {
- this.confirm.show();
- this.setting.hide();
-
- this.title.html(content);
- 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 _Dever_User =
- {
- config : [],
- uid : -1,
- name : '匿名用户',
- 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;
- }
- if ($(e).length) {
- $(e).attr('data-i', i);
- $(e).attr('data-c', c);
-
- $(e).unbind('click').bind('click', function() {
- if ($(this).attr('data-c') == 2) {
- var t1 = $(this);
- Dever.Confirm(function() {
- self.Save($(this).attr('data-i'), t1, config);
- });
- } else {
- self.Save($(this).attr('data-i'), $(this), config);
- }
- });
- }
- }
- }
- }
- //通用的保存数据功能
- ,Save : function(key, e, setting)
- {
- var self = this;
- var config = self.config[key];
- if (config.start) {
- config.start.call();
- }
- var send = {};
- 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);
- }
- }
- 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;
- }
- } 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') {
- 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);
- //e.get(0).tagName == 'TEXTAREA'
- send[i] = el.val();
- 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;
- }
-
- if (!config.url) {
- config.url = e.attr('dever-send');
- }
-
- 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;
- }
- }
|