core.js 33 KB

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