dever.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. $("html").find("include").eq(i).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. result = config.host.replace('www', host);
  158. result = result.replace('main', host);
  159. result = result + config.type;
  160. result = result.replace('??', '?');
  161. if (config.proxy) {
  162. return this.Proxy(host + '/' + uri);
  163. }
  164. return result + uri;
  165. }
  166. // 输出数据,提示框
  167. ,Out : function(content, callback, ele, title)
  168. {
  169. if (content && content.indexOf('登录') != -1 && $("#login_url").length) {
  170. var href = $("#login_url").val();
  171. location.href = href;
  172. return;
  173. } else {
  174. alert(content);
  175. if (ele && callback) {
  176. ele.unbind('click').bind('click', callback);
  177. } else if (callback) {
  178. callback();
  179. }
  180. }
  181. }
  182. // 将数据解析,并进行下一步操作
  183. ,Msg : function(result, error_callback, success_callback)
  184. {
  185. if (result.status == 2) {
  186. if (error_callback) {
  187. return error_callback(result.msg);
  188. }
  189. this.Out(result.msg);
  190. } else {
  191. if (success_callback) {
  192. return success_callback(result.msg);
  193. }
  194. if (result.msg.indexOf('http://') != -1) {
  195. this.Jump(result.msg);
  196. } else if (parseInt(result.msg) > 0) {
  197. this.Out('操作成功', function() {
  198. location.reload();
  199. });
  200. } else {
  201. this.Out(result.msg);
  202. }
  203. }
  204. }
  205. ,Confirm : function(callback)
  206. {
  207. if (confirm('确定进行此项操作吗?')) {
  208. callback();
  209. }
  210. }
  211. //上传组件加载
  212. ,Upload : function()
  213. {
  214. if ($(".dever-upload-drag").length) {
  215. $(".dever-upload-drag").each(function() {
  216. var key = $(this).attr('key');
  217. if (config.proxy) {
  218. var url = Dever.Proxy('upload/save.drag') + '&key=' + key;
  219. } else {
  220. var url = config.upload + '.drag?key='+ key;
  221. }
  222. $(this).inlineattachment({
  223. uploadUrl: url,
  224. progressText: '![文件上传中...]()',
  225. urlText: "![文件描述]({filename})\n",
  226. errorText: '上传失败'
  227. });
  228. });
  229. }
  230. if ($(".dever-upload").length) {
  231. $(".dever-upload").each(function(i) {
  232. loadUpload(i,$(this),$(this).attr('key'),config.upload + '.start', config.assets + 'image/');//三个参数说明1:第几个上传框2:文件对象3:图片的基本配置标题
  233. })
  234. }
  235. }
  236. ,Load : function(filename,filetype)
  237. {
  238. if (filetype == "js") {
  239. var fileref = document.createElement('script');
  240. fileref.setAttribute("type","text/javascript");
  241. fileref.setAttribute("src",filename + '.' + filetype);
  242. } else if (filetype == "css") {
  243. var fileref = document.createElement('link');
  244. fileref.setAttribute("rel","stylesheet");
  245. fileref.setAttribute("type","text/css");
  246. fileref.setAttribute("href",filename + '.' + filetype);
  247. }
  248. if (typeof fileref != "undefined") {
  249. document.getElementsByTagName("head")[0].appendChild(fileref);
  250. }
  251. }
  252. ,Extend : function()
  253. {
  254. Array.prototype.indexIn = function(val) {
  255. for (var i = 0; i < this.length; i++) {
  256. if (this[i] == val) return i;
  257. }
  258. return -1;
  259. };
  260. Array.prototype.remove = function(val) {
  261. var index = this.indexIn(val);
  262. if (index > -1) {
  263. this.splice(index, 1);
  264. }
  265. };
  266. $("select option").each(function() {
  267. if ($(this).attr('select') && $(this).attr('select') == 'true') {
  268. $(this).attr('selected', 'true');
  269. } else {
  270. $(this).removeAttr('selected');
  271. }
  272. });
  273. }
  274. ,Ucfirst : function(str)
  275. {
  276. var str = str.toLowerCase();
  277. var strarr = str.split(' ');
  278. var result = '';
  279. for (var i in strarr){
  280. result += strarr[i].substring(0,1).toUpperCase()+strarr[i].substring(1)+' ';
  281. }
  282. return result;
  283. }
  284. };
  285. //瀑布流分页
  286. var _Dever_Page =
  287. {
  288. name : '',
  289. Init : function()
  290. {
  291. if ($(this.name).length) {
  292. var self = this;
  293. $(window).scroll(function() {
  294. if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
  295. self.Start(self.name);
  296. }
  297. });
  298. }
  299. }
  300. ,Start : function(page)
  301. {
  302. page = page ? page : this.name;
  303. if ($(page).length) {
  304. $(page).hide();
  305. var url = $(page).attr('link');
  306. if (url) {
  307. var key = $(page).attr('dever-list');
  308. $.get(url, function(t) {
  309. t = '<div>' + t + '</div>';
  310. var c = $(t).find(key).html();
  311. if (c) {
  312. $(key).append(c);
  313. var l = $(t).find(page).attr('link');
  314. $(page).attr('link', l);
  315. }
  316. });
  317. }
  318. }
  319. }
  320. ,Ajax : function(e)
  321. {
  322. var self = this;
  323. e.find('a').each(function() {
  324. var url = $(this).attr('href');
  325. $(this).attr('href', 'javascript:;');
  326. $(this).unbind('click').bind('click', function() {
  327. $.get(url.replace('callback', ''), function(t) {
  328. e.html(t);
  329. self.Ajax(e);
  330. });
  331. });
  332. });
  333. }
  334. }
  335. //模态框
  336. var _Dever_Modal =
  337. {
  338. state : false,
  339. html : '',
  340. box : '',
  341. title : '',
  342. content : '',
  343. yes : '',
  344. no : '',
  345. setting : '',
  346. confirm : '',
  347. Init : function()
  348. {
  349. this.state = false;
  350. if (this.html) {
  351. $('body').append(this.html);
  352. if(this.box) this.box = $(this.box);
  353. if(this.title) this.title = $(this.title);
  354. if(this.content) this.content = $(this.content);
  355. if(this.yes) this.yes = $(this.yes);
  356. if(this.no) this.no = $(this.no);
  357. if(this.setting) this.setting = $(this.setting);
  358. if(this.confirm) this.confirm = $(this.confirm);
  359. this.state = true;
  360. }
  361. }
  362. ,Alert : function(content, callback)
  363. {
  364. if (this.state == false) {
  365. alert(content);
  366. return;
  367. } else {
  368. if (this.setting.length && this.setting.html()) {
  369. this.confirm.hide();
  370. this.setting.show();
  371. } else {
  372. this.setting.hide();
  373. this.confirm.show();
  374. }
  375. this.title.html(content);
  376. this.box.show();
  377. if (callback) {
  378. this.Bind(callback);
  379. }
  380. }
  381. }
  382. ,Confirm : function(content, callback)
  383. {
  384. if (this.state == false) {
  385. alert(content);
  386. return;
  387. } else {
  388. this.confirm.show();
  389. this.setting.hide();
  390. this.title.html(content);
  391. this.box.show();
  392. if(callback)
  393. {
  394. this.Bind(callback);
  395. }
  396. }
  397. }
  398. ,Bind : function(callback)
  399. {
  400. if (this.yes.length && callback.yes) {
  401. this.yes.unbind('click').bind('click', callback.yes);
  402. }
  403. if (this.no.length) {
  404. if (callback.no) {
  405. this.no.unbind('click').bind('click', callback.no);
  406. } else {
  407. var self = this;
  408. this.no.unbind('click').bind('click', function()
  409. {
  410. self.Close();
  411. });
  412. }
  413. }
  414. }
  415. ,Close : function()
  416. {
  417. this.box.hide();
  418. }
  419. }
  420. //用户相关
  421. var _Dever_User =
  422. {
  423. config : [],
  424. uid : -1,
  425. name : '匿名用户',
  426. Init : function()
  427. {
  428. var self = this;
  429. if (this.config.click) {
  430. for (var i in this.config.click) {
  431. var e = self.config.click[i];
  432. var c = 1;
  433. if (e.indexOf('|') != -1) {
  434. var t = e.split('|');
  435. e = t[0];
  436. c = 2;
  437. }
  438. if ($(e).length) {
  439. $(e).attr('data-i', i);
  440. $(e).attr('data-c', c);
  441. $(e).unbind('click').bind('click', function() {
  442. if ($(this).attr('data-c') == 2) {
  443. var t1 = $(this);
  444. Dever.Confirm(function() {
  445. self.Save($(this).attr('data-i'), t1, config);
  446. });
  447. } else {
  448. self.Save($(this).attr('data-i'), $(this), config);
  449. }
  450. });
  451. }
  452. }
  453. }
  454. }
  455. //通用的保存数据功能
  456. ,Save : function(key, e, setting)
  457. {
  458. var self = this;
  459. var config = self.config[key];
  460. if (config.start) {
  461. config.start.call();
  462. }
  463. var send = {};
  464. var callback = function(msg,state) {
  465. if (config.status && $(config.status).length) {
  466. //$(config.status).html(msg).css('visibility','initial');
  467. state ? $(config.status).html(msg).hide() : $(config.status).html(msg).show();
  468. } else if(!state) {
  469. Dever.Out(msg);
  470. }
  471. }
  472. if (typeof(config.input) != 'undefined') {
  473. if (typeof(config.input) == 'string' && config.input == 'parent') {
  474. if (e.attr('dever-send')) {
  475. send[e.attr('dever-send')] = e.attr('dever-' + e.attr('dever-send'));
  476. }
  477. send.id = e.attr('dever-id');
  478. send.value = e.parent().find('input').val();
  479. if (!send.id || !send.value) {
  480. callback(config.error);
  481. return;
  482. }
  483. } else if (typeof(config.input) == 'string' && config.input == 'click') {
  484. if (e.attr('dever-send')) {
  485. var a = e.attr('dever-send').split('&');
  486. for (var i in a) {
  487. if (typeof(a[i]) == 'string') {
  488. var b = a[i].split('=');
  489. send[b[0]] = b[1];
  490. }
  491. }
  492. }
  493. } else if (typeof(config.input) == 'object') {
  494. for (var i in config.input) {
  495. var option = false;
  496. var input = config.input[i];
  497. if (input.indexOf('|') != -1) {
  498. var t = input.split('|');
  499. input = t[0];
  500. option = true;
  501. }
  502. var m = true;
  503. var el = $(input);
  504. //e.get(0).tagName == 'TEXTAREA'
  505. send[i] = el.val();
  506. if (option == false) {
  507. if (send[i] && el.attr('dever-match')) {
  508. var r = new RegExp(el.attr('dever-match'));
  509. m = r.test(send[i]);
  510. }
  511. if (send[i] && el.attr('dever-value')) {
  512. m = $(el.attr('dever-value')).val() == send[i];
  513. }
  514. if (!send[i] || m != true) {
  515. var error = el.attr('dever-error') ? el.attr('dever-error') : config.error;
  516. callback(error);
  517. return;
  518. }
  519. }
  520. }
  521. }
  522. }
  523. if (!send) {
  524. callback(config.error);
  525. return;
  526. }
  527. if (!config.url) {
  528. config.url = e.attr('dever-send');
  529. }
  530. callback(1,1);
  531. config.url = Dever.CallUrl(config.url);
  532. if (setting.proxy) {
  533. $.post(config.url, send, function(t) {
  534. t = eval('(' + t + ')');
  535. Dever.Msg(t, callback, (config.callback ? function(msg){config.callback(e,msg,t)} : false));
  536. });
  537. } else {
  538. $.getJSON(config.url, send, function(t) {
  539. Dever.Msg(t, callback, (config.callback ? function(msg){config.callback(e,msg,t)} : false));
  540. });
  541. }
  542. }
  543. };
  544. //编辑器相关
  545. var _Dever_Editor =
  546. {
  547. state : false,
  548. editors : [],
  549. Init : function()
  550. {
  551. if (typeof marked == 'function') {
  552. this.state = true;
  553. } else {
  554. this.state = false;
  555. }
  556. this.SetMark();
  557. var self = this;
  558. if ($('.dever-note').length) {
  559. $('.dever-note').each(function() {
  560. self.InitEditor($(this));
  561. })
  562. }
  563. }
  564. ,InitEditor : function(e)
  565. {
  566. var id = e.attr('id');
  567. var key = e.attr('key');
  568. if (config.uid && config.uid > 0) {
  569. var toolbar = [
  570. 'title'
  571. ,'bold'
  572. ,'italic'
  573. ,'underline'
  574. ,'strikethrough'
  575. ,'fontScale'
  576. ,'color'
  577. ,'ol'
  578. ,'ul'
  579. ,'blockquote'
  580. ,'code'
  581. ,'table'
  582. ,'link'
  583. ,'image'
  584. ,'hr'
  585. ,'indent'
  586. ,'outdent'
  587. ,'alignment'
  588. //,'emoji'
  589. ];
  590. } else {
  591. var toolbar = [
  592. 'title'
  593. ,'bold'
  594. ,'fontScale'
  595. ,'color'
  596. ,'ol'
  597. ,'ul'
  598. ,'blockquote'
  599. ,'indent'
  600. ,'outdent'
  601. ,'alignment'
  602. ];
  603. }
  604. if (typeof(this.editors[id]) == "undefined") {
  605. this.editors[id] = new Simditor({
  606. textarea: e,
  607. upload:
  608. {
  609. url: config.upload + '.simditor',
  610. params: {key:key},
  611. fileKey: 'file',
  612. connectionCount: 10,
  613. leaveConfirm: 'Uploading is in progress, are you sure to leave this page?'
  614. },
  615. toolbar : toolbar
  616. /*
  617. ,emoji:
  618. {
  619. imagePath: config.lib + 'simditor/plugins/emoji/images/emoji/'
  620. }
  621. */
  622. });
  623. }
  624. }
  625. ,SetMark : function()
  626. {
  627. return;
  628. }
  629. ,GetMark : function(content)
  630. {
  631. if (this.state == true) {
  632. return marked(content);
  633. }
  634. return content;
  635. }
  636. };
  637. //模板
  638. var _Dever_Template =
  639. {
  640. state : false,
  641. method : '',
  642. element : '',
  643. data : '',
  644. param : '',
  645. attr : '',
  646. Init : function()
  647. {
  648. if (this.Check()) {
  649. var url = config.proxyTemplateUrl;
  650. var send = {};
  651. var parsing = [];
  652. $("dever").each(function(){
  653. parsing.push($(this).html());
  654. $(this).remove();
  655. });
  656. send.url = location.href;
  657. send.parsing = parsing.join('');
  658. send.html = $("html").html();
  659. //$("html").html('dever template loading...');
  660. $.ajax({
  661. type: "POST",
  662. url: url,
  663. data: send,
  664. dataType: "json",
  665. success: function(result){
  666. $("html").html(result.data);
  667. }
  668. });
  669. }
  670. }
  671. ,Check : function()
  672. {
  673. if ($("dever").length) {
  674. this.state = true;
  675. } else {
  676. this.state = false;
  677. }
  678. return this.state;
  679. }
  680. }