core.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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="../script/lib/jquery/jquery.min.js"></script><script src="../script/dever/core.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.Upload().Init();
  35. this.Change();
  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. ,Upload : function()
  67. {
  68. return _Dever_Upload;
  69. }
  70. ,Proxy : function(uri)
  71. {
  72. return config.proxy + 'proxy_method=' + encodeURIComponent(uri);
  73. }
  74. ,Jump : function(url)
  75. {
  76. location.href = url;
  77. }
  78. ,BackRun : function(url)
  79. {
  80. $.post(url);
  81. }
  82. ,Reload : function(e)
  83. {
  84. e.attr('src', e.attr('src'));
  85. }
  86. ,Target : function()
  87. {
  88. var link = config.host;
  89. $("a").each(function() {
  90. if (!$(this).attr('onclick')) {
  91. var url = $(this).attr('href');
  92. if (url && url.indexOf(link) == -1) {
  93. //$(this).attr('target', '_blank');
  94. }
  95. }
  96. })
  97. }
  98. ,Import : function()
  99. {
  100. var self = this;
  101. var include = $("include");
  102. var count = include.length-1;
  103. if (include.length) {
  104. include.each(function(i){
  105. var dom = $(this);
  106. var system = dom.attr('system');
  107. var path = dom.attr('path');
  108. var name = dom.attr('file');
  109. var file = '';
  110. if(path) file = path;
  111. if(system) file = file + '/' + system;
  112. file += name + '.html';
  113. dom.load(file, function(){
  114. $("html").find("include").eq(i).get(0).outerHTML = dom.html();
  115. if (i == count) {
  116. self.Template().Init();
  117. }
  118. });
  119. })
  120. } else {
  121. self.Template().Init();
  122. }
  123. }
  124. ,LoadPage : function()
  125. {
  126. if ($('.dever-loadpage').length) {
  127. $('.dever-loadpage').each(function() {
  128. var url = $(this).attr('dever-url');
  129. var self = $(this);
  130. $.getJSON(Dever.CallUrl(url), function(t) {
  131. self.html(t.msg);
  132. _Dever_Page.Ajax(self);
  133. });
  134. });
  135. }
  136. }
  137. ,CallUrl : function(url)
  138. {
  139. if (url.indexOf('callback') == -1) {
  140. if (url.indexOf('=') != -1) {
  141. url += '&json=1&callback=?';
  142. } else {
  143. url += '?json=1&callback=?';
  144. }
  145. }
  146. return url;
  147. }
  148. ,Change : function()
  149. {
  150. if ($('.dever-change').length) {
  151. $('.dever-change').change(function() {
  152. Dever.Jump($(this).attr('dever-change')+$(this).val());
  153. });
  154. //$('.dever-change').val($('.dever-change').attr('dever-value'));
  155. }
  156. }
  157. ,Host : function(host, uri)
  158. {
  159. var result = '';
  160. uri = uri ? uri : '';
  161. result = config.host.replace('www', host);
  162. result = result.replace('main', host);
  163. if (config.proxy) {
  164. return this.Proxy(host + '/' + uri);
  165. }
  166. return result + uri;
  167. }
  168. // 输出数据,提示框
  169. ,Out : function(content, callback, ele, title)
  170. {
  171. if (content && content.indexOf('登录') != -1 && $("#login_url").length) {
  172. var href = $("#login_url").val();
  173. location.href = href;
  174. return;
  175. } else {
  176. alert(content);
  177. if (ele && callback) {
  178. ele.unbind('click').bind('click', callback);
  179. } else if (callback) {
  180. callback();
  181. }
  182. }
  183. }
  184. // 将数据解析,并进行下一步操作
  185. ,Msg : function(result, error_callback, success_callback)
  186. {
  187. if (result.status == 2) {
  188. if (result.data) {
  189. if (result.data.element) {
  190. if (result.data.attr) {
  191. $(result.data.element).attr(result.data.attr, result.data.value);
  192. } else {
  193. $(result.data.element).val(result.data.value);
  194. }
  195. } else if (result.data.url) {
  196. location.href = result.data.url;
  197. }
  198. }
  199. if (error_callback) {
  200. return error_callback(result.msg);
  201. }
  202. this.Out(result.msg);
  203. } else {
  204. if (typeof(result.data) == 'string') {
  205. result.msg = result.data;
  206. }
  207. if (success_callback) {
  208. return success_callback(result.msg);
  209. }
  210. if (result.msg.indexOf('http://') != -1) {
  211. this.Jump(result.msg);
  212. } else if (parseInt(result.msg) > 0) {
  213. this.Out('操作成功', function() {
  214. location.reload();
  215. });
  216. } else {
  217. this.Out(result.msg);
  218. }
  219. }
  220. }
  221. ,Confirm : function(callback)
  222. {
  223. if (confirm('确定进行此项操作吗?')) {
  224. callback();
  225. }
  226. }
  227. ,Extend : function()
  228. {
  229. Array.prototype.indexIn = function(val) {
  230. for (var i = 0; i < this.length; i++) {
  231. if (this[i] == val) return i;
  232. }
  233. return -1;
  234. };
  235. Array.prototype.remove = function(val) {
  236. var index = this.indexIn(val);
  237. if (index > -1) {
  238. this.splice(index, 1);
  239. }
  240. };
  241. $("select option").each(function() {
  242. if ($(this).attr('select') && $(this).attr('select') == 'true') {
  243. $(this).attr('selected', 'true');
  244. } else {
  245. $(this).removeAttr('selected');
  246. }
  247. });
  248. }
  249. ,Ucfirst : function(str)
  250. {
  251. var str = str.toLowerCase();
  252. var strarr = str.split(' ');
  253. var result = '';
  254. for (var i in strarr){
  255. result += strarr[i].substring(0,1).toUpperCase()+strarr[i].substring(1)+' ';
  256. }
  257. return result;
  258. }
  259. };
  260. //瀑布流分页
  261. var _Dever_Page =
  262. {
  263. name : '',
  264. Init : function()
  265. {
  266. if ($(this.name).length) {
  267. var self = this;
  268. $(window).scroll(function() {
  269. if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
  270. self.Start(self.name);
  271. }
  272. });
  273. }
  274. }
  275. ,Start : function(page)
  276. {
  277. page = page ? page : this.name;
  278. if ($(page).length) {
  279. $(page).hide();
  280. var url = $(page).attr('link');
  281. if (url) {
  282. var key = $(page).attr('dever-list');
  283. $.get(url, function(t) {
  284. t = '<div>' + t + '</div>';
  285. var c = $(t).find(key).html();
  286. if (c) {
  287. $(key).append(c);
  288. var l = $(t).find(page).attr('link');
  289. $(page).attr('link', l);
  290. }
  291. });
  292. }
  293. }
  294. }
  295. ,Ajax : function(e)
  296. {
  297. var self = this;
  298. e.find('a').each(function() {
  299. var url = $(this).attr('href');
  300. $(this).attr('href', 'javascript:;');
  301. $(this).unbind('click').bind('click', function() {
  302. $.get(url.replace('callback', ''), function(t) {
  303. e.html(t);
  304. self.Ajax(e);
  305. });
  306. });
  307. });
  308. }
  309. }
  310. //模态框
  311. var _Dever_Modal =
  312. {
  313. state : false,
  314. html : '',
  315. box : '',
  316. title : '',
  317. content : '',
  318. yes : '',
  319. no : '',
  320. setting : '',
  321. confirm : '',
  322. Init : function()
  323. {
  324. this.state = false;
  325. if (this.html) {
  326. $('body').append(this.html);
  327. if(this.box) this.box = $(this.box);
  328. if(this.title) this.title = $(this.title);
  329. if(this.content) this.content = $(this.content);
  330. if(this.yes) this.yes = $(this.yes);
  331. if(this.no) this.no = $(this.no);
  332. if(this.setting) this.setting = $(this.setting);
  333. if(this.confirm) this.confirm = $(this.confirm);
  334. this.state = true;
  335. }
  336. }
  337. ,Alert : function(content, callback)
  338. {
  339. if (this.state == false) {
  340. alert(content);
  341. return;
  342. } else {
  343. if (this.setting.length && this.setting.html()) {
  344. this.confirm.hide();
  345. this.setting.show();
  346. } else {
  347. this.setting.hide();
  348. this.confirm.show();
  349. }
  350. this.title.html(content);
  351. this.box.show();
  352. if (callback) {
  353. this.Bind(callback);
  354. }
  355. }
  356. }
  357. ,Confirm : function(content, callback)
  358. {
  359. if (this.state == false) {
  360. alert(content);
  361. return;
  362. } else {
  363. this.confirm.show();
  364. this.setting.hide();
  365. this.title.html(content);
  366. this.box.show();
  367. if(callback)
  368. {
  369. this.Bind(callback);
  370. }
  371. }
  372. }
  373. ,Bind : function(callback)
  374. {
  375. if (this.yes.length && callback.yes) {
  376. this.yes.unbind('click').bind('click', callback.yes);
  377. }
  378. if (this.no.length) {
  379. if (callback.no) {
  380. this.no.unbind('click').bind('click', callback.no);
  381. } else {
  382. var self = this;
  383. this.no.unbind('click').bind('click', function()
  384. {
  385. self.Close();
  386. });
  387. }
  388. }
  389. }
  390. ,Close : function()
  391. {
  392. this.box.hide();
  393. }
  394. }
  395. //用户相关
  396. var _Dever_User =
  397. {
  398. config : [],
  399. uid : -1,
  400. name : '匿名用户',
  401. Init : function()
  402. {
  403. var self = this;
  404. if (this.config.click) {
  405. for (var i in this.config.click) {
  406. var e = self.config.click[i];
  407. var c = 1;
  408. if (e.indexOf('|') != -1) {
  409. var t = e.split('|');
  410. e = t[0];
  411. c = 2;
  412. }
  413. if ($(e).length) {
  414. $(e).attr('data-i', i);
  415. $(e).attr('data-c', c);
  416. $(e).unbind('click').bind('click', function() {
  417. if ($(this).attr('data-c') == 2) {
  418. var t1 = $(this);
  419. Dever.Confirm(function() {
  420. self.Save($(this).attr('data-i'), t1, config);
  421. });
  422. } else {
  423. self.Save($(this).attr('data-i'), $(this), config);
  424. }
  425. });
  426. }
  427. }
  428. }
  429. }
  430. //通用的保存数据功能
  431. ,Save : function(key, e, setting)
  432. {
  433. var self = this;
  434. var config = self.config[key];
  435. if (config.start) {
  436. config.start.call();
  437. }
  438. var send = {};
  439. var callback = function(msg,state) {
  440. if (config.status && $(config.status).length) {
  441. //$(config.status).html(msg).css('visibility','initial');
  442. state ? $(config.status).html(msg).hide() : $(config.status).html(msg).show();
  443. } else if(!state) {
  444. Dever.Out(msg);
  445. }
  446. }
  447. if (typeof(config.input) != 'undefined') {
  448. if (typeof(config.input) == 'string' && config.input == 'parent') {
  449. if (e.attr('dever-send')) {
  450. send[e.attr('dever-send')] = e.attr('dever-' + e.attr('dever-send'));
  451. }
  452. send.id = e.attr('dever-id');
  453. send.value = e.parent().find('input').val();
  454. if (!send.id || !send.value) {
  455. callback(config.error);
  456. return;
  457. }
  458. } else if (typeof(config.input) == 'string' && config.input == 'click') {
  459. if (e.attr('dever-send')) {
  460. var a = e.attr('dever-send').split('&');
  461. for (var i in a) {
  462. if (typeof(a[i]) == 'string') {
  463. var b = a[i].split('=');
  464. send[b[0]] = b[1];
  465. }
  466. }
  467. }
  468. } else if (typeof(config.input) == 'object') {
  469. for (var i in config.input) {
  470. var option = false;
  471. var input = config.input[i];
  472. if (input.indexOf('|') != -1) {
  473. var t = input.split('|');
  474. input = t[0];
  475. option = true;
  476. }
  477. var m = true;
  478. var el = $(input);
  479. //e.get(0).tagName == 'TEXTAREA'
  480. send[i] = el.val();
  481. if (option == false) {
  482. if (send[i] && el.attr('dever-match')) {
  483. var r = new RegExp(el.attr('dever-match'));
  484. m = r.test(send[i]);
  485. }
  486. if (send[i] && el.attr('dever-value')) {
  487. m = $(el.attr('dever-value')).val() == send[i];
  488. }
  489. if (!send[i] || m != true) {
  490. var error = el.attr('dever-error') ? el.attr('dever-error') : config.error;
  491. callback(error);
  492. return;
  493. }
  494. }
  495. }
  496. }
  497. }
  498. if (!send) {
  499. callback(config.error);
  500. return;
  501. }
  502. if (!config.url) {
  503. config.url = e.attr('dever-send');
  504. }
  505. if (e.attr('dever-refresh')) {
  506. config.url = e.attr('dever-refresh');
  507. }
  508. callback(1,1);
  509. config.url = Dever.CallUrl(config.url);
  510. if (setting.proxy) {
  511. $.post(config.url, send, function(t) {
  512. t = eval('(' + t + ')');
  513. Dever.Msg(t, callback, (config.callback ? function(msg){config.callback(e,msg,t)} : false));
  514. });
  515. } else {
  516. $.getJSON(config.url, send, function(t) {
  517. Dever.Msg(t, callback, (config.callback ? function(msg){config.callback(e,msg,t)} : false));
  518. });
  519. }
  520. }
  521. };
  522. //编辑器相关
  523. var _Dever_Editor =
  524. {
  525. state : false,
  526. editors : [],
  527. Init : function()
  528. {
  529. if (typeof marked == 'function') {
  530. this.state = true;
  531. } else {
  532. this.state = false;
  533. }
  534. this.SetMark();
  535. var self = this;
  536. if ($('.dever-note').length) {
  537. $('.dever-note').each(function() {
  538. self.InitEditor($(this));
  539. })
  540. }
  541. }
  542. ,InitEditor : function(e)
  543. {
  544. var id = e.attr('id');
  545. var key = e.attr('key');
  546. if (config.uid && config.uid > 0) {
  547. var toolbar = [
  548. 'title'
  549. ,'bold'
  550. ,'italic'
  551. ,'underline'
  552. ,'strikethrough'
  553. ,'fontScale'
  554. ,'color'
  555. ,'ol'
  556. ,'ul'
  557. ,'blockquote'
  558. ,'code'
  559. ,'table'
  560. ,'link'
  561. ,'image'
  562. ,'hr'
  563. ,'indent'
  564. ,'outdent'
  565. ,'alignment'
  566. //,'emoji'
  567. ];
  568. } else {
  569. var toolbar = [
  570. 'title'
  571. ,'bold'
  572. ,'fontScale'
  573. ,'color'
  574. ,'ol'
  575. ,'ul'
  576. ,'blockquote'
  577. ,'indent'
  578. ,'outdent'
  579. ,'alignment'
  580. ];
  581. }
  582. if (typeof(this.editors[id]) == "undefined") {
  583. this.editors[id] = new Simditor({
  584. textarea: e,
  585. upload:
  586. {
  587. url: config.upload + '.simditor',
  588. params: {key:key},
  589. fileKey: 'file',
  590. connectionCount: 10,
  591. leaveConfirm: 'Uploading is in progress, are you sure to leave this page?'
  592. },
  593. toolbar : toolbar
  594. /*
  595. ,emoji:
  596. {
  597. imagePath: config.lib + 'simditor/plugins/emoji/images/emoji/'
  598. }
  599. */
  600. });
  601. }
  602. }
  603. ,SetMark : function()
  604. {
  605. return;
  606. }
  607. ,GetMark : function(content)
  608. {
  609. if (this.state == true) {
  610. return marked(content);
  611. }
  612. return content;
  613. }
  614. };
  615. //模板
  616. var _Dever_Template =
  617. {
  618. state : false,
  619. method : '',
  620. element : '',
  621. data : '',
  622. param : '',
  623. attr : '',
  624. Init : function()
  625. {
  626. if (this.Check()) {
  627. var url = config.proxyTemplateUrl;
  628. var send = {};
  629. var parsing = [];
  630. $("dever").each(function(){
  631. parsing.push($(this).html());
  632. $(this).remove();
  633. });
  634. send.url = location.href;
  635. send.parsing = parsing.join('');
  636. send.html = $("html").html();
  637. //$("html").html('dever template loading...');
  638. $.ajax({
  639. type: "POST",
  640. url: url,
  641. data: send,
  642. dataType: "json",
  643. success: function(result){
  644. $("html").html(result.data);
  645. }
  646. });
  647. }
  648. }
  649. ,Check : function()
  650. {
  651. if ($("dever").length) {
  652. this.state = true;
  653. } else {
  654. this.state = false;
  655. }
  656. return this.state;
  657. }
  658. }
  659. //上传 请载入layui
  660. var _Dever_Upload =
  661. {
  662. upload : [],
  663. upload_file : [],
  664. upload_pic : [],
  665. callback : {},
  666. Init : function()
  667. {
  668. var self = this;
  669. if ($(".dever-upload-drag").length) {
  670. $(".dever-upload-drag").each(function() {
  671. var key = $(this).attr('key');
  672. if (config.proxy) {
  673. var url = Dever.Proxy('upload/save.drag') + '&key=' + key;
  674. } else {
  675. var url = config.upload + '.drag?key='+ key;
  676. }
  677. $(this).inlineattachment({
  678. uploadUrl: url,
  679. progressText: '![文件上传中...]()',
  680. urlText: "![文件描述]({filename})\n",
  681. errorText: '上传失败'
  682. });
  683. });
  684. }
  685. if ($(".dever-upload").length) {
  686. $(".dever-upload").each(function(i) {
  687. self.Load(i,$(this));
  688. })
  689. }
  690. }
  691. ,Load : function(i,e)
  692. {
  693. var self = this;
  694. var id = e.attr('id');
  695. var value = e.attr('dever-upload-value');
  696. var key = e.attr('dever-upload-key');
  697. var url = config.upload + '.start';
  698. if (typeof(this.upload[value]) != "undefined") {
  699. return;
  700. }
  701. if (!key) {
  702. key = 1;
  703. }
  704. self.upload[value] = true;
  705. self.upload_pic[value] = [];
  706. self.upload_file[value] = [];
  707. layui.use(['upload','layer'], function() {
  708. var layer = layui.layer;
  709. var layuiUpload = layui.upload;
  710. var uploadInst = layuiUpload.render({
  711. elem: '#' + id
  712. ,data: {'key' : key}
  713. ,field: 'file'
  714. ,url: url
  715. ,multiple: true
  716. ,before: function(obj) {
  717. layer.load();
  718. }
  719. ,done: function(data) {
  720. layer.closeAll('loading');
  721. if (data.status == 1) {
  722. var type = e.attr('dever-upload-type');
  723. var html = '';
  724. var callback = e.attr('dever-upload-callback');
  725. if (type == 'mul') {
  726. if (self.upload_pic[value].length <= 0 && $("#" + value).val()) {
  727. self.upload_pic[value] = $("#" + value).val().split(',');
  728. }
  729. if (self.callback && self.callback[callback]) {
  730. html += self.callback[callback].call(self, e, data.url, 'dever-upload-close');
  731. } else {
  732. html += '<li><img src="'+data.url+'" class="pics"><em class="dever-upload-close"></em></li>';
  733. }
  734. e.before(html);
  735. self.Close(value);
  736. self.upload_pic[value].push(data.url);
  737. $("#" + value).val(self.upload_pic[value].join(','));
  738. } else {
  739. if (self.callback && self.callback[callback]) {
  740. html = self.callback[callback].call(self, e, data.url, 'dever-upload-close');
  741. } else {
  742. html = '<img src="'+data.url+'" class="pics">';
  743. }
  744. e.html(html);
  745. }
  746. } else {
  747. layer.msg(data.msg, {icon: 4});
  748. return false;
  749. }
  750. }
  751. ,error: function() {
  752. layer.msg('上传失败', {icon: 4});
  753. return false;
  754. }
  755. });
  756. });
  757. }
  758. ,Close : function(v)
  759. {
  760. var self = this;
  761. $('.dever-upload-close').each(function()
  762. {
  763. $(this).unbind('click').bind('click', function() {
  764. var e = $(this).parent();
  765. var p = e.find('img').attr('src');
  766. e.remove();
  767. self.upload_pic[v].remove(p);
  768. $("#" + v).val(self.upload_pic[v].join(','));
  769. });
  770. });
  771. }
  772. }