core.js 32 KB

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