core.js 33 KB

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