core.js 32 KB

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