core.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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. countdown : -1,
  29. modal : false,
  30. Init : function()
  31. {
  32. this.Import();
  33. this.Page().Init();
  34. this.User().Init();
  35. this.Editor().Init();
  36. this.Upload().Init();
  37. this.Change();
  38. this.LoadPage();
  39. this.Extend();
  40. //this.Target();
  41. //this.Pjax();
  42. this.AutoComplete();
  43. this.Emoji().Init();
  44. }
  45. ,AutoComplete : function()
  46. {
  47. if ($("input").length) {
  48. $("input").each(function()
  49. {
  50. $(this).attr('autocomplete', 'new-password');
  51. /*
  52. $(this).attr('readonly', true);
  53. $(this).focus(function()
  54. {
  55. $(this).attr('readonly', false);
  56. });
  57. */
  58. });
  59. }
  60. }
  61. ,Pjax : function()
  62. {
  63. $(document).pjax('a', '.container');
  64. //$.pjax.reload('.container');
  65. }
  66. ,Emoji : function()
  67. {
  68. return _Dever_Emoji;
  69. }
  70. ,Page : function()
  71. {
  72. return _Dever_Page;
  73. }
  74. ,Template : function()
  75. {
  76. return _Dever_Template;
  77. }
  78. ,Editor : function()
  79. {
  80. return _Dever_Editor;
  81. }
  82. ,User : function()
  83. {
  84. return _Dever_User;
  85. }
  86. ,Modal : function()
  87. {
  88. return new _Dever_Modal();
  89. }
  90. ,Upload : function()
  91. {
  92. return _Dever_Upload;
  93. }
  94. ,Proxy : function(uri)
  95. {
  96. return config.proxy + 'proxy_method=' + encodeURIComponent(uri);
  97. }
  98. ,Jump : function(url)
  99. {
  100. location.href = url;
  101. }
  102. ,BackRun : function(url)
  103. {
  104. $.post(url);
  105. }
  106. ,Reload : function(e)
  107. {
  108. e.attr('src', e.attr('src'));
  109. }
  110. ,Target : function()
  111. {
  112. var link = config.host;
  113. $("a").each(function() {
  114. if (!$(this).attr('onclick')) {
  115. var url = $(this).attr('href');
  116. if (url && url.indexOf(link) == -1) {
  117. //$(this).attr('target', '_blank');
  118. }
  119. }
  120. })
  121. }
  122. ,Time : function(e, time, text)
  123. {
  124. var self = this;
  125. if (this.countdown < 0) {
  126. this.countdown = time;
  127. }
  128. if (this.countdown == 0) {
  129. e.attr("disabled", false);
  130. e.html(text);
  131. self.Init();
  132. this.countdown = time;
  133. } else {
  134. e.attr("disabled", true);
  135. e.html(this.countdown + 's后' + text);
  136. this.countdown--;
  137. e.unbind('click');
  138. setTimeout(function() {
  139. self.Time(e, time, text)
  140. },1000)
  141. }
  142. }
  143. ,Import : function()
  144. {
  145. var self = this;
  146. var include = $("include");
  147. var count = include.length-1;
  148. if (include.length) {
  149. include.each(function(i){
  150. var dom = $(this);
  151. var system = dom.attr('system');
  152. var path = dom.attr('path');
  153. var name = dom.attr('file');
  154. var file = '';
  155. if(path) file = path;
  156. if(system) file = file + '/' + system;
  157. file += name + '.html';
  158. dom.load(file, function(){
  159. $("html").find("include").eq(i).get(0).outerHTML = dom.html();
  160. if (i == count) {
  161. self.Template().Init();
  162. }
  163. });
  164. })
  165. } else {
  166. self.Template().Init();
  167. }
  168. }
  169. ,LoadPage : function()
  170. {
  171. if ($('.dever-loadpage').length) {
  172. $('.dever-loadpage').each(function() {
  173. var url = $(this).attr('dever-url');
  174. var self = $(this);
  175. $.getJSON(Dever.CallUrl(url), function(t) {
  176. self.html(t.msg);
  177. _Dever_Page.Ajax(self);
  178. });
  179. });
  180. }
  181. }
  182. ,CallUrl : function(url)
  183. {
  184. if (url.indexOf('callback') == -1) {
  185. if (url.indexOf('=') != -1) {
  186. url += '&json=1&callback=?';
  187. } else {
  188. url += '?json=1&callback=?';
  189. }
  190. }
  191. return url;
  192. }
  193. ,Change : function()
  194. {
  195. if ($('.dever-change').length) {
  196. $('.dever-change').change(function() {
  197. Dever.Jump($(this).attr('dever-change')+$(this).val());
  198. });
  199. //$('.dever-change').val($('.dever-change').attr('dever-value'));
  200. }
  201. }
  202. ,Host : function(host, uri, state)
  203. {
  204. var result = '';
  205. uri = uri ? uri : '';
  206. if (state) {
  207. result = config.host.replace('www', host);
  208. } else {
  209. result = config.host;
  210. }
  211. result = result.replace('main', host);
  212. if (config.proxy) {
  213. return this.Proxy(host + '/' + uri);
  214. }
  215. return result + uri;
  216. }
  217. // 输出数据,提示框
  218. ,Out : function(content, callback, title)
  219. {
  220. if (content && content.indexOf('登录') != -1 && $("#login_url").length) {
  221. var href = $("#login_url").val();
  222. location.href = href;
  223. return;
  224. } else {
  225. if (callback) {
  226. callback = {'yes':callback, 'no':callback};
  227. }
  228. if (this.modal) {
  229. this.modal.Alert(content, callback, title);
  230. } else {
  231. this.Modal().Alert(content, callback, title);
  232. }
  233. }
  234. }
  235. // 将数据解析,并进行下一步操作
  236. ,Msg : function(result, error_callback, success_callback)
  237. {
  238. if (result.status == 2) {
  239. if (result.data) {
  240. if (result.data.element) {
  241. if (result.data.attr) {
  242. $(result.data.element).attr(result.data.attr, result.data.value);
  243. } else {
  244. $(result.data.element).val(result.data.value);
  245. }
  246. } else if (result.data.url) {
  247. location.href = result.data.url;
  248. }
  249. }
  250. if (error_callback) {
  251. return error_callback(result.msg);
  252. }
  253. this.Out(result.msg);
  254. } else {
  255. if (typeof(result.data) != 'object') {
  256. result.msg = result.data;
  257. }
  258. if (success_callback) {
  259. return success_callback(result.msg);
  260. }
  261. if (result.msg.indexOf('http://') != -1) {
  262. var self = this;
  263. if (result.msg.indexOf('||') != -1) {
  264. var temp = result.msg.split('||');
  265. result.msg = temp[0];
  266. result.data = temp[1];
  267. this.Out(result.data, function() {
  268. self.Jump(result.msg);
  269. });
  270. } else {
  271. self.Jump(result.msg);
  272. }
  273. } else if (parseInt(result.msg) > 0) {
  274. this.Out('操作成功', function() {
  275. location.reload();
  276. });
  277. } else {
  278. this.Out(result.msg);
  279. }
  280. }
  281. }
  282. ,Confirm : function(content, callback, title)
  283. {
  284. if (this.modal) {
  285. this.modal.Confirm(content, {'yes':callback}, title);
  286. } else {
  287. this.Modal().Confirm(content, {'yes':callback}, title);
  288. }
  289. }
  290. ,Extend : function()
  291. {
  292. Array.prototype.indexIn = function(val) {
  293. for (var i = 0; i < this.length; i++) {
  294. if (this[i] == val) return i;
  295. }
  296. return -1;
  297. };
  298. Array.prototype.remove = function(val) {
  299. var index = this.indexIn(val);
  300. if (index > -1) {
  301. this.splice(index, 1);
  302. }
  303. };
  304. $("select option").each(function() {
  305. if ($(this).attr('select') && $(this).attr('select') == 'true') {
  306. $(this).attr('selected', 'true');
  307. } else {
  308. $(this).removeAttr('selected');
  309. }
  310. });
  311. }
  312. ,Ucfirst : function(str)
  313. {
  314. var str = str.toLowerCase();
  315. var strarr = str.split(' ');
  316. var result = '';
  317. for (var i in strarr){
  318. result += strarr[i].substring(0,1).toUpperCase()+strarr[i].substring(1)+' ';
  319. }
  320. return result;
  321. }
  322. };
  323. //瀑布流分页
  324. var _Dever_Page =
  325. {
  326. name : '',
  327. loading : '',
  328. state :false,
  329. Init : function()
  330. {
  331. if ($(this.name).length) {
  332. var self = this;
  333. $(window).scroll(function() {
  334. if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
  335. self.Start(self.name, self.loading);
  336. }
  337. });
  338. }
  339. }
  340. ,Start : function(page, loading)
  341. {
  342. page = page ? page : this.name;
  343. loading = loading ? loading : this.loading;
  344. if ($(page).length) {
  345. $(page).hide();
  346. var url = $(page).attr('dever-page-link');
  347. if (url) {
  348. var key = $(page).attr('dever-page-list');
  349. if ($(loading).length) {
  350. $(loading).show();
  351. }
  352. var self = this;
  353. if (self.state) {
  354. return;
  355. }
  356. self.state = true;
  357. $.get(url, function(t) {
  358. self.state = false;
  359. t = '<div>' + t + '</div>';
  360. var c = $(t).find(key).html();
  361. if ($(loading).length) {
  362. $(loading).hide();
  363. }
  364. if (c) {
  365. $(key).append(c);
  366. var l = $(t).find(page).attr('dever-page-link');
  367. $(page).attr('dever-page-link', l);
  368. //Dever.Init();
  369. var callback = $(page).attr('dever-page-call');
  370. if (callback) {
  371. eval(callback);
  372. }
  373. }
  374. });
  375. }
  376. }
  377. }
  378. ,Ajax : function(e)
  379. {
  380. var self = this;
  381. e.find('a').each(function() {
  382. var url = $(this).attr('href');
  383. $(this).attr('href', 'javascript:;');
  384. $(this).unbind('click').bind('click', function() {
  385. $.get(url.replace('callback', ''), function(t) {
  386. e.html(t);
  387. self.Ajax(e);
  388. });
  389. });
  390. });
  391. }
  392. }
  393. //模态框 非静态类
  394. var _Dever_Modal = function()
  395. {
  396. var self = this;
  397. self.state = false;
  398. self.html = '';
  399. self.box = '';
  400. self.title = '';
  401. self.content = '';
  402. self.yes = '';
  403. self.no = '';
  404. self.setting = '';
  405. self.confirm = '';
  406. self.open = false;
  407. self.Init = function()
  408. {
  409. this.state = false;
  410. if (this.html) {
  411. $('body').append(this.html);
  412. }
  413. if (this.box && this.content) {
  414. this.box = $(this.box);
  415. if (this.box.length) {
  416. this.box.hide();
  417. this.content = $(this.content);
  418. if(this.title) this.title = $(this.title);
  419. if(this.yes) this.yes = $(this.yes);
  420. if(this.no) this.no = $(this.no);
  421. if(this.setting) this.setting = $(this.setting);
  422. if(this.confirm) this.confirm = $(this.confirm);
  423. if(!this.timeout) this.timeout = 3000;
  424. this.state = true;
  425. }
  426. }
  427. };
  428. self.Alert = function(content, callback, title)
  429. {
  430. if (self.open == true) {
  431. return;
  432. }
  433. self.open = true;
  434. if (this.state == false) {
  435. alert(content);
  436. if (callback) {
  437. callback.yes();
  438. }
  439. } else {
  440. if (this.setting.length && this.setting.html()) {
  441. if (this.confirm) this.confirm.hide();
  442. this.setting.show();
  443. } else {
  444. if (this.setting) this.setting.hide();
  445. if (this.confirm) this.confirm.show();
  446. }
  447. this.content.html(content);
  448. if (title) {
  449. this.title.html(title);
  450. }
  451. this.box.show();
  452. if (this.yes && callback.yes) {
  453. this.Bind(callback);
  454. } else {
  455. this.TimeOut(callback);
  456. }
  457. }
  458. };
  459. self.Confirm = function(content, callback, title)
  460. {
  461. if (self.open == true) {
  462. return;
  463. }
  464. self.open = true;
  465. if (this.state == false) {
  466. if (confirm(content)) {
  467. callback.yes();
  468. }
  469. } else {
  470. if (this.confirm) this.confirm.show();
  471. if (this.setting) this.setting.hide();
  472. this.content.html(content);
  473. if (title) {
  474. this.title.html(title);
  475. }
  476. this.box.show();
  477. if (callback.yes) {
  478. this.Bind(callback);
  479. }
  480. }
  481. }
  482. self.Bind = function(callback)
  483. {
  484. if (this.yes.length && callback.yes) {
  485. this.yes.unbind('click').bind('click', callback.yes);
  486. }
  487. if (this.no.length) {
  488. if (callback.no) {
  489. this.no.unbind('click').bind('click', callback.no);
  490. } else {
  491. var self = this;
  492. this.no.unbind('click').bind('click', function()
  493. {
  494. self.Close();
  495. });
  496. }
  497. }
  498. };
  499. self.TimeOut = function(callback)
  500. {
  501. var self = this;
  502. setTimeout(function()
  503. {
  504. if (callback && callback.yes) {
  505. callback.yes();
  506. } else {
  507. self.Close();
  508. }
  509. }, self.timeout);
  510. }
  511. self.Close = function()
  512. {
  513. this.open = false;
  514. this.box.hide();
  515. }
  516. return self;
  517. };
  518. //用户相关 静态类
  519. var _Dever_User =
  520. {
  521. config : [],
  522. uid : -1,
  523. name : '匿名用户',
  524. //按钮不可点击的样式
  525. disable : '',
  526. Init : function()
  527. {
  528. var self = this;
  529. if (this.config.click) {
  530. for (var i in this.config.click) {
  531. var e = self.config.click[i];
  532. var c = 1;
  533. if (e.indexOf('|') != -1) {
  534. var t = e.split('|');
  535. e = t[0];
  536. c = 2;
  537. }
  538. var e = $(e);
  539. if (e.length) {
  540. e.attr('data-i', i);
  541. e.attr('data-c', c);
  542. if (self.disable) {
  543. e.addClass(self.disable);
  544. self.DisableCheck(e);
  545. } else {
  546. self.Bind(e);
  547. }
  548. }
  549. }
  550. }
  551. }
  552. ,Bind : function(e)
  553. {
  554. var self = this;
  555. e.unbind('click').bind('click', function() {
  556. if ($(this).attr('data-c') == 2) {
  557. Dever.Confirm(function() {
  558. self.Save($(this), config);
  559. });
  560. } else {
  561. self.Save($(this), config);
  562. }
  563. });
  564. }
  565. ,DisableCheck : function(e)
  566. {
  567. var self = this;
  568. var key = e.attr('data-i');
  569. var config = self.config[key];
  570. var callback = function(msg,state) {
  571. e.addClass(self.disable);
  572. }
  573. var blur = function() {
  574. var send = self.Check(e, callback, config);
  575. if (!send) {
  576. e.addClass(self.disable);
  577. return;
  578. }
  579. e.removeClass(self.disable);
  580. self.Bind(e);
  581. }
  582. var send = self.Check(e, callback, config, blur);
  583. if (!send) {
  584. callback(config.error);
  585. return;
  586. }
  587. e.removeClass(self.disable);
  588. self.Bind(e);
  589. }
  590. ,Check : function(e, callback, config, blur)
  591. {
  592. var self = this;
  593. var send = {};
  594. if (typeof(config.input) != 'undefined') {
  595. if (typeof(config.input) == 'string' && config.input == 'parent') {
  596. if (e.attr('dever-send')) {
  597. send[e.attr('dever-send')] = e.attr('dever-' + e.attr('dever-send'));
  598. }
  599. send.id = e.attr('dever-id');
  600. send.value = e.parent().find('input').val();
  601. if (!send.id || !send.value) {
  602. callback(config.error);
  603. return false;
  604. }
  605. } else if (typeof(config.input) == 'string' && config.input == 'click') {
  606. if (e.attr('dever-send')) {
  607. var a = e.attr('dever-send').split('&');
  608. for (var i in a) {
  609. if (typeof(a[i]) == 'string') {
  610. var b = a[i].split('=');
  611. send[b[0]] = b[1];
  612. }
  613. }
  614. }
  615. } else if (typeof(config.input) == 'object') {
  616. if (blur) {
  617. for (var i in config.input) {
  618. var input = config.input[i];
  619. if (typeof(input) == 'string') {
  620. if (input.indexOf('|') != -1) {
  621. var t = input.split('|');
  622. input = t[0];
  623. }
  624. var el = $(input);
  625. el.on('input',function()
  626. {
  627. blur();
  628. });
  629. el.blur(function()
  630. {
  631. blur();
  632. });
  633. }
  634. }
  635. }
  636. for (var i in config.input) {
  637. var option = false;
  638. var input = config.input[i];
  639. if (typeof(input) == 'object' || typeof(input) == 'function') {
  640. send[i] = input();
  641. } else {
  642. var select = [];
  643. if (input.indexOf('|') != -1) {
  644. var t = input.split('|');
  645. input = t[0];
  646. option = true;
  647. if (t[1]) {
  648. select = t;
  649. }
  650. }
  651. var el = $(input);
  652. //e.get(0).tagName == 'TEXTAREA'
  653. send[i] = el.val();
  654. if (option == false) {
  655. state = self.Match(el, send[i], callback, config);
  656. if (!state) {
  657. return state;
  658. }
  659. } else if(select) {
  660. //只要有一个为真即可
  661. var result = false;
  662. if (select.length <= 0) {
  663. result = true;
  664. }
  665. for (var i in select) {
  666. if (typeof(select[i]) == 'string') {
  667. var el = $(select[i]);
  668. state = self.Match($(el), $(el).val(), false, config);
  669. if (state) {
  670. result = true;
  671. }
  672. }
  673. }
  674. if (result == false) {
  675. return result;
  676. }
  677. }
  678. }
  679. }
  680. }
  681. }
  682. return send;
  683. }
  684. ,Match : function(e, value, callback, config)
  685. {
  686. var m = true;
  687. if (value && e.attr('dever-match')) {
  688. var r = new RegExp(e.attr('dever-match'));
  689. m = r.test(value);
  690. }
  691. if (value && e.attr('dever-value')) {
  692. m = $(e.attr('dever-value')).val() == value;
  693. }
  694. if (!value || m != true) {
  695. if (callback) {
  696. var error = e.attr('dever-error') ? e.attr('dever-error') : config.error;
  697. callback(error);
  698. }
  699. return false;
  700. }
  701. return true;
  702. }
  703. //通用的保存数据功能
  704. ,Save : function(e, setting)
  705. {
  706. var self = this;
  707. var key = e.attr('data-i');
  708. var config = self.config[key];
  709. if (config.start) {
  710. config.start.call();
  711. }
  712. var callback = function(msg,state) {
  713. if (config.status && $(config.status).length) {
  714. //$(config.status).html(msg).css('visibility','initial');
  715. state ? $(config.status).html(msg).hide() : $(config.status).html(msg).show();
  716. } else if(!state) {
  717. Dever.Out(msg);
  718. }
  719. }
  720. var send = self.Check(e, callback, config);
  721. if (!send) {
  722. //callback(config.error);
  723. return;
  724. }
  725. if (!config.url) {
  726. config.url = e.attr('dever-send');
  727. }
  728. if (e.attr('dever-refresh')) {
  729. config.url = e.attr('dever-refresh');
  730. }
  731. callback(1,1);
  732. config.url = Dever.CallUrl(config.url);
  733. if (setting.proxy) {
  734. $.post(config.url, send, function(t) {
  735. t = eval('(' + t + ')');
  736. Dever.Msg(t, callback, (config.callback ? function(msg){config.callback(e,msg,t)} : false));
  737. });
  738. } else {
  739. $.getJSON(config.url, send, function(t) {
  740. Dever.Msg(t, callback, (config.callback ? function(msg){config.callback(e,msg,t)} : false));
  741. });
  742. }
  743. }
  744. };
  745. //编辑器相关
  746. var _Dever_Editor =
  747. {
  748. state : false,
  749. editors : [],
  750. Init : function()
  751. {
  752. if (typeof marked == 'function') {
  753. this.state = true;
  754. } else {
  755. this.state = false;
  756. }
  757. this.SetMark();
  758. var self = this;
  759. if ($('.dever-note').length) {
  760. $('.dever-note').each(function() {
  761. self.InitEditor($(this));
  762. })
  763. }
  764. }
  765. ,InitEditor : function(e)
  766. {
  767. var id = e.attr('id');
  768. var key = e.attr('key');
  769. if (config.uid && config.uid > 0) {
  770. var toolbar = [
  771. 'title'
  772. ,'bold'
  773. ,'italic'
  774. ,'underline'
  775. ,'strikethrough'
  776. ,'fontScale'
  777. ,'color'
  778. ,'ol'
  779. ,'ul'
  780. ,'blockquote'
  781. ,'code'
  782. ,'table'
  783. ,'link'
  784. ,'image'
  785. ,'hr'
  786. ,'indent'
  787. ,'outdent'
  788. ,'alignment'
  789. //,'emoji'
  790. ];
  791. } else {
  792. var toolbar = [
  793. 'title'
  794. ,'bold'
  795. ,'fontScale'
  796. ,'color'
  797. ,'ol'
  798. ,'ul'
  799. ,'blockquote'
  800. ,'indent'
  801. ,'outdent'
  802. ,'alignment'
  803. ];
  804. }
  805. if (typeof(this.editors[id]) == "undefined") {
  806. this.editors[id] = new Simditor({
  807. textarea: e,
  808. upload:
  809. {
  810. url: config.upload + '.simditor',
  811. params: {key:key},
  812. fileKey: 'file',
  813. connectionCount: 10,
  814. leaveConfirm: 'Uploading is in progress, are you sure to leave this page?'
  815. },
  816. toolbar : toolbar
  817. /*
  818. ,emoji:
  819. {
  820. imagePath: config.lib + 'simditor/plugins/emoji/images/emoji/'
  821. }
  822. */
  823. });
  824. }
  825. }
  826. ,SetMark : function()
  827. {
  828. return;
  829. }
  830. ,GetMark : function(content)
  831. {
  832. if (this.state == true) {
  833. return marked(content);
  834. }
  835. return content;
  836. }
  837. };
  838. //模板
  839. var _Dever_Template =
  840. {
  841. state : false,
  842. method : '',
  843. element : '',
  844. data : '',
  845. param : '',
  846. attr : '',
  847. Init : function()
  848. {
  849. if (this.Check()) {
  850. var url = config.proxyTemplateUrl;
  851. var send = {};
  852. var parsing = [];
  853. $("dever").each(function(){
  854. parsing.push($(this).html());
  855. $(this).remove();
  856. });
  857. send.url = location.href;
  858. send.parsing = parsing.join('');
  859. send.html = $("html").html();
  860. //$("html").html('dever template loading...');
  861. $.ajax({
  862. type: "POST",
  863. url: url,
  864. data: send,
  865. dataType: "json",
  866. success: function(result){
  867. $("html").html(result.data);
  868. }
  869. });
  870. }
  871. }
  872. ,Check : function()
  873. {
  874. if ($("dever").length) {
  875. this.state = true;
  876. } else {
  877. this.state = false;
  878. }
  879. return this.state;
  880. }
  881. }
  882. //上传 请载入layui
  883. var _Dever_Upload =
  884. {
  885. upload : [],
  886. upload_file : [],
  887. upload_pic : [],
  888. callback : {},
  889. Init : function()
  890. {
  891. var self = this;
  892. if ($(".dever-upload-drag").length) {
  893. $(".dever-upload-drag").each(function() {
  894. var key = $(this).attr('key');
  895. if (config.proxy) {
  896. var url = Dever.Proxy('upload/save.drag') + '&key=' + key;
  897. } else {
  898. var url = config.upload + '.drag?key='+ key;
  899. }
  900. $(this).inlineattachment({
  901. uploadUrl: url,
  902. progressText: '![文件上传中...]()',
  903. urlText: "![文件描述]({filename})\n",
  904. errorText: '上传失败'
  905. });
  906. });
  907. }
  908. if ($(".dever-upload").length) {
  909. $(".dever-upload").each(function(i) {
  910. self.Load(i,$(this));
  911. })
  912. }
  913. }
  914. ,Load : function(i,e)
  915. {
  916. var self = this;
  917. var id = e.attr('id');
  918. var value = e.attr('dever-upload-value');
  919. var key = e.attr('dever-upload-key');
  920. var pic = e.attr('dever-upload-pic');
  921. var type = e.attr('dever-upload-type');
  922. var callback = e.attr('dever-upload-callback');
  923. var input = $("#" + value);
  924. if (!input.length) {
  925. return;
  926. }
  927. var url = config.upload + '.start';
  928. if (typeof(this.upload[value]) != "undefined") {
  929. return;
  930. }
  931. if (!key) {
  932. key = 1;
  933. }
  934. self.upload[value] = true;
  935. self.upload_pic[value] = [];
  936. self.upload_file[value] = [];
  937. var input_value = input.val();
  938. if (input_value && type == 'mul') {
  939. var input_value_array = input_value.split(',');
  940. for (var i in input_value_array) {
  941. self.Set(type, e, input, value, callback, input_value_array[i]);
  942. }
  943. }
  944. layui.use(['upload','layer'], function() {
  945. var layer = layui.layer;
  946. var layuiUpload = layui.upload;
  947. var uploadInst = layuiUpload.render({
  948. elem: '#' + id
  949. ,data: {'key' : key}
  950. ,field: 'file'
  951. ,url: url
  952. ,multiple: true
  953. ,xhr:xhrOnProgress
  954. ,before: function(obj) {
  955. layer.load();
  956. }
  957. ,done: function(data) {
  958. layer.closeAll('loading');
  959. if (data.status == 1) {
  960. self.Set(type, e, input, value, callback, data.url, pic);
  961. } else {
  962. layer.msg(data.msg, {icon: 4});
  963. return false;
  964. }
  965. }
  966. ,error: function() {
  967. layer.closeAll('loading');
  968. layer.msg('上传失败', {icon: 4});
  969. return false;
  970. }
  971. });
  972. });
  973. }
  974. ,Set : function(type, e, input, value, callback, url, pic)
  975. {
  976. if (type == 'mul') {
  977. this.SetMul(e, input, value, callback, url);
  978. } else {
  979. this.SetOne(e, input, value, callback, url, pic);
  980. }
  981. }
  982. ,SetOne : function(e, input, value, callback, url, pic)
  983. {
  984. var html = '';
  985. if (this.callback && this.callback[callback]) {
  986. html = this.callback[callback].call(this, e, url, 'dever-upload-close');
  987. } else {
  988. pic = $("#" + pic);
  989. if (pic.length) {
  990. pic.attr('src', url).show();
  991. } else {
  992. html = '<img src="'+url+'" class="dever-upload-pic">';
  993. }
  994. }
  995. if (html) {
  996. e.html(html);
  997. }
  998. var input_value = input.val();
  999. if (input_value != url) {
  1000. input.val(url);
  1001. this.Blur(input);
  1002. }
  1003. }
  1004. ,SetMul : function(e, input, value, callback, url)
  1005. {
  1006. var html = '';
  1007. var input_value = input.val();
  1008. if (this.upload_pic[value].length <= 0 && input_value) {
  1009. this.upload_pic[value] = input_value.split(',');
  1010. }
  1011. if (this.callback && this.callback[callback]) {
  1012. html += this.callback[callback].call(this, e, url, 'dever-upload-close');
  1013. } else {
  1014. html += '<li><img src="'+url+'" class="dever-upload-pics"><em class="dever-upload-close"></em></li>';
  1015. }
  1016. if (html) {
  1017. e.before(html);
  1018. }
  1019. this.Close(value);
  1020. if (input_value.indexOf(url) == -1) {
  1021. this.upload_pic[value].push(url);
  1022. input.val(this.upload_pic[value].join(','));
  1023. this.Blur(input);
  1024. }
  1025. }
  1026. ,Blur : function(input)
  1027. {
  1028. var events = $._data(input[0], 'events');
  1029. if (events && events["blur"]) {
  1030. input.blur();
  1031. }
  1032. }
  1033. ,Close : function(v)
  1034. {
  1035. var self = this;
  1036. $('.dever-upload-close').each(function()
  1037. {
  1038. $(this).unbind('click').bind('click', function() {
  1039. var e = $(this).parent();
  1040. var p = e.find('img').attr('src');
  1041. e.remove();
  1042. self.upload_pic[v].remove(p);
  1043. $("#" + v).val(self.upload_pic[v].join(','));
  1044. });
  1045. });
  1046. }
  1047. }
  1048. var _Dever_Emoji =
  1049. {
  1050. Init : function()
  1051. {
  1052. var self = this;
  1053. if ($('.dever-emoji').length) {
  1054. $('.dever-emoji').each(function()
  1055. {
  1056. self.Get($(this), $(this).html());
  1057. })
  1058. }
  1059. }
  1060. ,Get : function(e, content)
  1061. {
  1062. var handle = function()
  1063. {
  1064. e.html(twemoji.parse(content)).show(200);
  1065. };
  1066. if (typeof jEmoji != 'object') {
  1067. $.getScript('//twemoji.maxcdn.com/2/twemoji.min.js?11.4', function() {
  1068. /*
  1069. $("head").append("<link>");
  1070. var css = $("head").children(":last");
  1071. css.attr({
  1072. rel: "stylesheet",
  1073. type: "text/css",
  1074. href: config.script + '/lib/emoji/emoji.css'
  1075. });
  1076. */
  1077. return handle();
  1078. });
  1079. } else {
  1080. return handle();
  1081. }
  1082. }
  1083. }
  1084. //创建监听函数
  1085. var xhrOnProgress=function(fun) {
  1086. xhrOnProgress.onprogress = fun; //绑定监听
  1087. //使用闭包实现监听绑
  1088. return function() {
  1089. //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
  1090. var xhr = $.ajaxSettings.xhr();
  1091. //判断监听函数是否为函数
  1092. if (typeof xhrOnProgress.onprogress !== 'function') return xhr;
  1093. //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
  1094. if (xhrOnProgress.onprogress && xhr.upload) {
  1095. xhr.upload.onprogress = xhrOnProgress.onprogress;
  1096. }
  1097. return xhr;
  1098. }
  1099. }