core.js 33 KB

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