core.js 32 KB

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