core.js 32 KB

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