core.js 32 KB

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