core.js 32 KB

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