core.js 32 KB

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