core.js 32 KB

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