dever.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /* ================================================================================
  2. * dever.js v1.0
  3. * http://git.shemic.com/dever/script
  4. * ================================================================================
  5. * Copyright 2017-2018 Dever(dever.cc)
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ================================================================================
  19. */
  20. //<filter><script src="../js/jquery.min.js"></script><script src="../js/dever.js"></script></filter>
  21. $(function()
  22. {
  23. Dever.Init();
  24. });
  25. //公共类库
  26. var Dever =
  27. {
  28. Init : function()
  29. {
  30. this.Import();
  31. this.Page().Init();
  32. this.User().Init();
  33. this.Editor().Init();
  34. this.Change();
  35. this.Upload();
  36. this.LoadPage();
  37. this.Extend();
  38. //this.Target();
  39. //this.Pjax();
  40. }
  41. ,Pjax : function()
  42. {
  43. $(document).pjax('a', '.container');
  44. //$.pjax.reload('.container');
  45. }
  46. ,Page : function()
  47. {
  48. return _Dever_Page;
  49. }
  50. ,Template : function()
  51. {
  52. return _Dever_Template;
  53. }
  54. ,Editor : function()
  55. {
  56. return _Dever_Editor;
  57. }
  58. ,User : function()
  59. {
  60. return _Dever_User;
  61. }
  62. ,Modal : function()
  63. {
  64. return _Dever_Modal;
  65. }
  66. ,Proxy : function(uri)
  67. {
  68. return config.proxy + 'proxy_method=' + encodeURIComponent(uri);
  69. }
  70. ,Jump : function(url)
  71. {
  72. location.href = url;
  73. }
  74. ,BackRun : function(url)
  75. {
  76. $.post(url);
  77. }
  78. ,Reload : function(e)
  79. {
  80. e.attr('src', e.attr('src'));
  81. }
  82. ,Target : function()
  83. {
  84. var link = config.host;
  85. $("a").each(function() {
  86. if (!$(this).attr('onclick')) {
  87. var url = $(this).attr('href');
  88. if (url && url.indexOf(link) == -1) {
  89. //$(this).attr('target', '_blank');
  90. }
  91. }
  92. })
  93. }
  94. ,Import : function()
  95. {
  96. var self = this;
  97. var include = $("include");
  98. var count = include.length-1;
  99. if (include.length) {
  100. include.each(function(i){
  101. var dom = $(this);
  102. var system = dom.attr('system');
  103. var path = dom.attr('path');
  104. var name = dom.attr('file');
  105. var file = '';
  106. if(path) file = path;
  107. if(system) file = file + '/' + system;
  108. file += name + '.html';
  109. dom.load(file, function(){
  110. dom.get(0).outerHTML = dom.html();
  111. if (i == count) {
  112. self.Template().Init();
  113. }
  114. });
  115. })
  116. } else {
  117. self.Template().Init();
  118. }
  119. }
  120. ,LoadPage : function()
  121. {
  122. if ($('.dever-loadpage').length) {
  123. $('.dever-loadpage').each(function() {
  124. var url = $(this).attr('dever-url');
  125. var self = $(this);
  126. $.getJSON(Dever.CallUrl(url), function(t) {
  127. self.html(t.msg);
  128. _Dever_Page.Ajax(self);
  129. });
  130. });
  131. }
  132. }
  133. ,CallUrl : function(url)
  134. {
  135. if (url.indexOf('callback') == -1) {
  136. if (url.indexOf('=') != -1) {
  137. url += '&json=1&callback=?';
  138. } else {
  139. url += '?json=1&callback=?';
  140. }
  141. }
  142. return url;
  143. }
  144. ,Change : function()
  145. {
  146. if ($('.dever-change').length) {
  147. $('.dever-change').change(function() {
  148. Dever.Jump($(this).attr('dever-change')+$(this).val());
  149. });
  150. //$('.dever-change').val($('.dever-change').attr('dever-value'));
  151. }
  152. }
  153. ,Host : function(host, uri)
  154. {
  155. var result = '';
  156. uri = uri ? uri : '';
  157. if (config.type == '?') {
  158. result = config.host.replace('?', host + '?');
  159. } else {
  160. result = config.host.replace('www', host);
  161. }
  162. result = result.replace('main', host);
  163. result = result + config.type;
  164. result = result.replace('??', '?');
  165. if (config.proxy) {
  166. return this.Proxy(host + '/' + uri);
  167. }
  168. return result + uri;
  169. }
  170. // 输出数据,提示框
  171. ,Out : function(content, callback, ele, title)
  172. {
  173. if (content && content.indexOf('登录') != -1 && $("#login_url").length) {
  174. var href = $("#login_url").val();
  175. location.href = href;
  176. return;
  177. } else {
  178. alert(content);
  179. if (ele && callback) {
  180. ele.unbind('click').bind('click', callback);
  181. } else if (callback) {
  182. callback();
  183. }
  184. }
  185. }
  186. // 将数据解析,并进行下一步操作
  187. ,Msg : function(result, error_callback, success_callback)
  188. {
  189. if (result.status == 2) {
  190. if (error_callback) {
  191. return error_callback(result.msg);
  192. }
  193. this.Out(result.msg);
  194. } else {
  195. if (result.data) {
  196. result.msg = result.data;
  197. }
  198. if (success_callback) {
  199. return success_callback(result.msg);
  200. }
  201. if (result.msg.indexOf('http://') != -1) {
  202. this.Jump(result.msg);
  203. } else if (parseInt(result.msg) > 0) {
  204. this.Out('操作成功', function() {
  205. location.reload();
  206. });
  207. } else {
  208. this.Out(result.msg);
  209. }
  210. }
  211. }
  212. ,Confirm : function(callback)
  213. {
  214. if (confirm('确定进行此项操作吗?')) {
  215. callback();
  216. }
  217. }
  218. //上传组件加载
  219. ,Upload : function()
  220. {
  221. if ($(".dever-upload-drag").length) {
  222. $(".dever-upload-drag").each(function() {
  223. var key = $(this).attr('key');
  224. if (config.proxy) {
  225. var url = Dever.Proxy('upload/save.drag') + '&key=' + key;
  226. } else {
  227. var url = config.upload + '.drag?key='+ key;
  228. }
  229. $(this).inlineattachment({
  230. uploadUrl: url,
  231. progressText: '![文件上传中...]()',
  232. urlText: "![文件描述]({filename})\n",
  233. errorText: '上传失败'
  234. });
  235. });
  236. }
  237. if ($(".dever-upload").length) {
  238. $(".dever-upload").each(function(i) {
  239. loadUpload(i,$(this),$(this).attr('key'),config.upload + '.start', config.assets + 'image/');//三个参数说明1:第几个上传框2:文件对象3:图片的基本配置标题
  240. })
  241. }
  242. }
  243. ,Load : function(filename,filetype)
  244. {
  245. if (filetype == "js") {
  246. var fileref = document.createElement('script');
  247. fileref.setAttribute("type","text/javascript");
  248. fileref.setAttribute("src",filename + '.' + filetype);
  249. } else if (filetype == "css") {
  250. var fileref = document.createElement('link');
  251. fileref.setAttribute("rel","stylesheet");
  252. fileref.setAttribute("type","text/css");
  253. fileref.setAttribute("href",filename + '.' + filetype);
  254. }
  255. if (typeof fileref != "undefined") {
  256. document.getElementsByTagName("head")[0].appendChild(fileref);
  257. }
  258. }
  259. ,Extend : function()
  260. {
  261. Array.prototype.indexIn = function(val) {
  262. for (var i = 0; i < this.length; i++) {
  263. if (this[i] == val) return i;
  264. }
  265. return -1;
  266. };
  267. Array.prototype.remove = function(val) {
  268. var index = this.indexIn(val);
  269. if (index > -1) {
  270. this.splice(index, 1);
  271. }
  272. };
  273. $("select option").each(function() {
  274. if ($(this).attr('select') && $(this).attr('select') == 'true') {
  275. $(this).attr('selected', 'true');
  276. } else {
  277. $(this).removeAttr('selected');
  278. }
  279. });
  280. }
  281. ,Ucfirst : function(str)
  282. {
  283. var str = str.toLowerCase();
  284. var strarr = str.split(' ');
  285. var result = '';
  286. for (var i in strarr){
  287. result += strarr[i].substring(0,1).toUpperCase()+strarr[i].substring(1)+' ';
  288. }
  289. return result;
  290. }
  291. };
  292. //瀑布流分页
  293. var _Dever_Page =
  294. {
  295. name : '',
  296. Init : function()
  297. {
  298. if ($(this.name).length) {
  299. var self = this;
  300. $(window).scroll(function() {
  301. if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
  302. self.Start(self.name);
  303. }
  304. });
  305. }
  306. }
  307. ,Start : function(page)
  308. {
  309. page = page ? page : this.name;
  310. if ($(page).length) {
  311. $(page).hide();
  312. var url = $(page).attr('link');
  313. if (url) {
  314. var key = $(page).attr('dever-list');
  315. $.get(url, function(t) {
  316. t = '<div>' + t + '</div>';
  317. var c = $(t).find(key).html();
  318. if (c) {
  319. $(key).append(c);
  320. var l = $(t).find(page).attr('link');
  321. $(page).attr('link', l);
  322. }
  323. });
  324. }
  325. }
  326. }
  327. ,Ajax : function(e)
  328. {
  329. var self = this;
  330. e.find('a').each(function() {
  331. var url = $(this).attr('href');
  332. $(this).attr('href', 'javascript:;');
  333. $(this).unbind('click').bind('click', function() {
  334. $.get(url.replace('callback', ''), function(t) {
  335. e.html(t);
  336. self.Ajax(e);
  337. });
  338. });
  339. });
  340. }
  341. }
  342. //模态框
  343. var _Dever_Modal =
  344. {
  345. state : false,
  346. html : '',
  347. box : '',
  348. title : '',
  349. content : '',
  350. yes : '',
  351. no : '',
  352. setting : '',
  353. confirm : '',
  354. Init : function()
  355. {
  356. this.state = false;
  357. if (this.html) {
  358. $('body').append(this.html);
  359. if(this.box) this.box = $(this.box);
  360. if(this.title) this.title = $(this.title);
  361. if(this.content) this.content = $(this.content);
  362. if(this.yes) this.yes = $(this.yes);
  363. if(this.no) this.no = $(this.no);
  364. if(this.setting) this.setting = $(this.setting);
  365. if(this.confirm) this.confirm = $(this.confirm);
  366. this.state = true;
  367. }
  368. }
  369. ,Alert : function(content, callback)
  370. {
  371. if (this.state == false) {
  372. alert(content);
  373. return;
  374. } else {
  375. if (this.setting.length && this.setting.html()) {
  376. this.confirm.hide();
  377. this.setting.show();
  378. } else {
  379. this.setting.hide();
  380. this.confirm.show();
  381. }
  382. this.title.html(content);
  383. this.box.show();
  384. if (callback) {
  385. this.Bind(callback);
  386. }
  387. }
  388. }
  389. ,Confirm : function(content, callback)
  390. {
  391. if (this.state == false) {
  392. alert(content);
  393. return;
  394. } else {
  395. this.confirm.show();
  396. this.setting.hide();
  397. this.title.html(content);
  398. this.box.show();
  399. if(callback)
  400. {
  401. this.Bind(callback);
  402. }
  403. }
  404. }
  405. ,Bind : function(callback)
  406. {
  407. if (this.yes.length && callback.yes) {
  408. this.yes.unbind('click').bind('click', callback.yes);
  409. }
  410. if (this.no.length) {
  411. if (callback.no) {
  412. this.no.unbind('click').bind('click', callback.no);
  413. } else {
  414. var self = this;
  415. this.no.unbind('click').bind('click', function()
  416. {
  417. self.Close();
  418. });
  419. }
  420. }
  421. }
  422. ,Close : function()
  423. {
  424. this.box.hide();
  425. }
  426. }
  427. //用户相关
  428. var _Dever_User =
  429. {
  430. config : [],
  431. uid : -1,
  432. name : '匿名用户',
  433. Init : function()
  434. {
  435. var self = this;
  436. if (this.config.click) {
  437. for (var i in this.config.click) {
  438. var e = self.config.click[i];
  439. var c = 1;
  440. if (e.indexOf('|') != -1) {
  441. var t = e.split('|');
  442. e = t[0];
  443. c = 2;
  444. }
  445. if ($(e).length) {
  446. $(e).attr('data-i', i);
  447. $(e).attr('data-c', c);
  448. $(e).unbind('click').bind('click', function() {
  449. if ($(this).attr('data-c') == 2) {
  450. var t1 = $(this);
  451. Dever.Confirm(function() {
  452. self.Save($(this).attr('data-i'), t1, config);
  453. });
  454. } else {
  455. self.Save($(this).attr('data-i'), $(this), config);
  456. }
  457. });
  458. }
  459. }
  460. }
  461. }
  462. //通用的保存数据功能
  463. ,Save : function(key, e, setting)
  464. {
  465. var self = this;
  466. var config = self.config[key];
  467. if (config.start) {
  468. config.start.call();
  469. }
  470. var send = {};
  471. var callback = function(msg,state) {
  472. if (config.status && $(config.status).length) {
  473. //$(config.status).html(msg).css('visibility','initial');
  474. state ? $(config.status).html(msg).hide() : $(config.status).html(msg).show();
  475. } else if(!state) {
  476. Dever.Out(msg);
  477. }
  478. }
  479. if (typeof(config.input) != 'undefined') {
  480. if (typeof(config.input) == 'string' && config.input == 'parent') {
  481. if (e.attr('dever-send')) {
  482. send[e.attr('dever-send')] = e.attr('dever-' + e.attr('dever-send'));
  483. }
  484. send.id = e.attr('dever-id');
  485. send.value = e.parent().find('input').val();
  486. if (!send.id || !send.value) {
  487. callback(config.error);
  488. return;
  489. }
  490. } else if (typeof(config.input) == 'string' && config.input == 'click') {
  491. if (e.attr('dever-send')) {
  492. var a = e.attr('dever-send').split('&');
  493. for (var i in a) {
  494. if (typeof(a[i]) == 'string') {
  495. var b = a[i].split('=');
  496. send[b[0]] = b[1];
  497. }
  498. }
  499. }
  500. } else if (typeof(config.input) == 'object') {
  501. for (var i in config.input) {
  502. var option = false;
  503. var input = config.input[i];
  504. if (input.indexOf('|') != -1) {
  505. var t = input.split('|');
  506. input = t[0];
  507. option = true;
  508. }
  509. var m = true;
  510. var el = $(input);
  511. //e.get(0).tagName == 'TEXTAREA'
  512. send[i] = el.val();
  513. if (option == false) {
  514. if (send[i] && el.attr('dever-match')) {
  515. var r = new RegExp(el.attr('dever-match'));
  516. m = r.test(send[i]);
  517. }
  518. if (send[i] && el.attr('dever-value')) {
  519. m = $(el.attr('dever-value')).val() == send[i];
  520. }
  521. if (!send[i] || m != true) {
  522. var error = el.attr('dever-error') ? el.attr('dever-error') : config.error;
  523. callback(error);
  524. return;
  525. }
  526. }
  527. }
  528. }
  529. }
  530. if (!send) {
  531. callback(config.error);
  532. return;
  533. }
  534. if (!config.url) {
  535. config.url = e.attr('dever-send');
  536. }
  537. callback(1,1);
  538. config.url = Dever.CallUrl(config.url);
  539. if (setting.proxy) {
  540. $.post(config.url, send, function(t) {
  541. t = eval('(' + t + ')');
  542. Dever.Msg(t, callback, (config.callback ? function(msg){config.callback(e,msg,t)} : false));
  543. });
  544. } else {
  545. $.getJSON(config.url, send, function(t) {
  546. Dever.Msg(t, callback, (config.callback ? function(msg){config.callback(e,msg,t)} : false));
  547. });
  548. }
  549. }
  550. };
  551. //编辑器相关
  552. var _Dever_Editor =
  553. {
  554. state : false,
  555. editors : [],
  556. Init : function()
  557. {
  558. if (typeof marked == 'function') {
  559. this.state = true;
  560. } else {
  561. this.state = false;
  562. }
  563. this.SetMark();
  564. var self = this;
  565. if ($('.dever-note').length) {
  566. $('.dever-note').each(function() {
  567. self.InitEditor($(this));
  568. })
  569. }
  570. }
  571. ,InitEditor : function(e)
  572. {
  573. var id = e.attr('id');
  574. var key = e.attr('key');
  575. if (config.uid && config.uid > 0) {
  576. var toolbar = [
  577. 'title'
  578. ,'bold'
  579. ,'italic'
  580. ,'underline'
  581. ,'strikethrough'
  582. ,'fontScale'
  583. ,'color'
  584. ,'ol'
  585. ,'ul'
  586. ,'blockquote'
  587. ,'code'
  588. ,'table'
  589. ,'link'
  590. ,'image'
  591. ,'hr'
  592. ,'indent'
  593. ,'outdent'
  594. ,'alignment'
  595. //,'emoji'
  596. ];
  597. } else {
  598. var toolbar = [
  599. 'title'
  600. ,'bold'
  601. ,'fontScale'
  602. ,'color'
  603. ,'ol'
  604. ,'ul'
  605. ,'blockquote'
  606. ,'indent'
  607. ,'outdent'
  608. ,'alignment'
  609. ];
  610. }
  611. if (typeof(this.editors[id]) == "undefined") {
  612. this.editors[id] = new Simditor({
  613. textarea: e,
  614. upload:
  615. {
  616. url: config.upload + '.simditor',
  617. params: {key:key},
  618. fileKey: 'file',
  619. connectionCount: 10,
  620. leaveConfirm: 'Uploading is in progress, are you sure to leave this page?'
  621. },
  622. toolbar : toolbar
  623. /*
  624. ,emoji:
  625. {
  626. imagePath: config.lib + 'simditor/plugins/emoji/images/emoji/'
  627. }
  628. */
  629. });
  630. }
  631. }
  632. ,SetMark : function()
  633. {
  634. return;
  635. }
  636. ,GetMark : function(content)
  637. {
  638. if (this.state == true) {
  639. return marked(content);
  640. }
  641. return content;
  642. }
  643. };
  644. //模板
  645. var _Dever_Template =
  646. {
  647. state : false,
  648. method : '',
  649. element : '',
  650. data : '',
  651. param : '',
  652. attr : '',
  653. Init : function()
  654. {
  655. if (this.Check()) {
  656. var url = config.proxyTemplateUrl;
  657. var send = {};
  658. var parsing = [];
  659. $("dever").each(function(){
  660. parsing.push($(this).html());
  661. $(this).remove();
  662. });
  663. send.url = location.href;
  664. send.parsing = parsing.join('');
  665. send.html = $("html").html();
  666. //$("html").html('dever template loading...');
  667. $.ajax({
  668. type: "POST",
  669. url: url,
  670. data: send,
  671. dataType: "json",
  672. success: function(result){
  673. $("html").html(result.data);
  674. }
  675. });
  676. }
  677. }
  678. ,Check : function()
  679. {
  680. if ($("dever").length) {
  681. this.state = true;
  682. } else {
  683. this.state = false;
  684. }
  685. return this.state;
  686. }
  687. }