addon.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'cookie'], function ($, undefined, Backend, Table, Form, Template, undefined) {
  2. $.cookie.prototype.defaults = {path: Config.moduleurl};
  3. var Controller = {
  4. index: function () {
  5. // 初始化表格参数配置
  6. Table.api.init({
  7. extend: {
  8. index_url: Config.api_url ? Config.api_url + '/addon/index' : "addon/downloaded",
  9. add_url: '',
  10. edit_url: '',
  11. del_url: '',
  12. multi_url: ''
  13. }
  14. });
  15. var table = $("#table");
  16. // 弹窗自适应宽高
  17. var area = Fast.config.openArea != undefined ? Fast.config.openArea : [$(window).width() > 800 ? '800px' : '95%', $(window).height() > 600 ? '600px' : '95%'];
  18. var switch_local = function () {
  19. if ($(".btn-switch.active").data("type") != "local") {
  20. Layer.confirm(__('Store not available tips'), {
  21. title: __('Warmtips'),
  22. btn: [__('Switch to the local'), __('Try to reload')]
  23. }, function (index) {
  24. layer.close(index);
  25. $(".panel .nav-tabs").hide();
  26. $(".toolbar > *:not(:first)").hide();
  27. $(".btn-switch[data-type='local']").trigger("click");
  28. }, function (index) {
  29. layer.close(index);
  30. table.bootstrapTable('refresh');
  31. });
  32. return false;
  33. }
  34. };
  35. table.on('load-success.bs.table', function (e, json) {
  36. if (json && typeof json.category != 'undefined' && $(".nav-category li").length == 2) {
  37. $.each(json.category, function (i, j) {
  38. $("<li><a href='javascript:;' data-id='" + j.id + "'>" + j.name + "</a></li>").insertBefore($(".nav-category li:last"));
  39. });
  40. }
  41. if (typeof json.rows === 'undefined' && typeof json.code != 'undefined') {
  42. switch_local();
  43. }
  44. });
  45. table.on('load-error.bs.table', function (e, status, res) {
  46. console.log(e, status, res);
  47. switch_local();
  48. });
  49. table.on('post-body.bs.table', function (e, settings, json, xhr) {
  50. var parenttable = table.closest('.bootstrap-table');
  51. var d = $(".fixed-table-toolbar", parenttable).find(".search input");
  52. d.off("keyup drop blur");
  53. d.on("keyup", function (e) {
  54. if (e.keyCode == 13) {
  55. var that = this;
  56. var options = table.bootstrapTable('getOptions');
  57. var queryParams = options.queryParams;
  58. options.pageNumber = 1;
  59. options.queryParams = function (params) {
  60. var params = queryParams(params);
  61. params.search = $(that).val();
  62. return params;
  63. };
  64. table.bootstrapTable('refresh', {});
  65. }
  66. });
  67. });
  68. Template.helper("Moment", Moment);
  69. Template.helper("addons", Config['addons']);
  70. $("#faupload-addon").data("params", function (files, xhr) {
  71. var userinfo = Controller.api.userinfo.get();
  72. return {
  73. uid: userinfo ? userinfo.id : '',
  74. token: userinfo ? userinfo.token : '',
  75. version: Config.faversion,
  76. force: (files[0].force || false) ? 1 : 0
  77. };
  78. });
  79. // 初始化表格
  80. table.bootstrapTable({
  81. url: $.fn.bootstrapTable.defaults.extend.index_url,
  82. pageSize: 50,
  83. queryParams: function (params) {
  84. var userinfo = Controller.api.userinfo.get();
  85. $.extend(params, {
  86. uid: userinfo ? userinfo.id : '',
  87. token: userinfo ? userinfo.token : '',
  88. domain: Config.domain,
  89. version: Config.faversion,
  90. sid: Controller.api.sid()
  91. });
  92. return params;
  93. },
  94. columns: [
  95. [
  96. {field: 'id', title: 'ID', operate: false, visible: false},
  97. {
  98. field: 'home',
  99. title: __('Index'),
  100. width: '50px',
  101. formatter: Controller.api.formatter.home
  102. },
  103. {field: 'name', title: __('Name'), operate: false, visible: false, width: '120px'},
  104. {
  105. field: 'title',
  106. title: __('Title'),
  107. operate: 'LIKE',
  108. align: 'left',
  109. formatter: Controller.api.formatter.title
  110. },
  111. {
  112. field: 'intro',
  113. title: __('Intro'),
  114. operate: 'LIKE',
  115. align: 'left',
  116. class: 'visible-lg',
  117. formatter: Controller.api.formatter.intro
  118. },
  119. {
  120. field: 'author',
  121. title: __('Author'),
  122. operate: 'LIKE',
  123. width: '100px',
  124. formatter: Controller.api.formatter.author
  125. },
  126. {
  127. field: 'price',
  128. title: __('Price'),
  129. operate: 'LIKE',
  130. width: '100px',
  131. align: 'center',
  132. formatter: Controller.api.formatter.price
  133. },
  134. {
  135. field: 'downloads',
  136. title: __('Downloads'),
  137. operate: 'LIKE',
  138. width: '80px',
  139. align: 'center',
  140. formatter: Controller.api.formatter.downloads
  141. },
  142. {
  143. field: 'version',
  144. title: __('Version'),
  145. operate: 'LIKE',
  146. width: '80px',
  147. align: 'center',
  148. formatter: Controller.api.formatter.version
  149. },
  150. {
  151. field: 'toggle',
  152. title: __('Status'),
  153. width: '80px',
  154. formatter: Controller.api.formatter.toggle
  155. },
  156. {
  157. field: 'id',
  158. title: __('Operate'),
  159. table: table,
  160. formatter: Controller.api.formatter.operate,
  161. align: 'right'
  162. },
  163. ]
  164. ],
  165. responseHandler: function (res) {
  166. $.each(res.rows, function (i, j) {
  167. j.addon = typeof Config.addons[j.name] != 'undefined' ? Config.addons[j.name] : null;
  168. });
  169. return res;
  170. },
  171. dataType: 'jsonp',
  172. templateView: false,
  173. clickToSelect: false,
  174. search: true,
  175. showColumns: false,
  176. showToggle: false,
  177. showExport: false,
  178. showSearch: false,
  179. commonSearch: true,
  180. searchFormVisible: true,
  181. searchFormTemplate: 'searchformtpl',
  182. });
  183. // 为表格绑定事件
  184. Table.api.bindevent(table);
  185. // 离线安装
  186. require(['upload'], function (Upload) {
  187. Upload.api.upload("#faupload-addon", function (data, ret, up, file) {
  188. Config['addons'][data.addon.name] = data.addon;
  189. var addon = data.addon;
  190. var testdata = data.addon.testdata;
  191. operate(data.addon.name, 'enable', false, function (data, ret) {
  192. Layer.alert(__('Offline installed tips') + (testdata ? __('Testdata tips') : ""), {
  193. btn: testdata ? [__('Import testdata'), __('Skip testdata')] : [__('OK')],
  194. title: __('Warning'),
  195. yes: function (index) {
  196. if (testdata) {
  197. Fast.api.ajax({
  198. url: 'addon/testdata',
  199. data: {
  200. name: addon.name,
  201. version: addon.version,
  202. faversion: Config.faversion
  203. }
  204. }, function (data, ret) {
  205. Layer.close(index);
  206. });
  207. } else {
  208. Layer.close(index);
  209. }
  210. },
  211. icon: 1
  212. });
  213. });
  214. return false;
  215. }, function (data, ret, up, file) {
  216. if (ret.msg && ret.msg.match(/(login|登录)/g)) {
  217. return Layer.alert(ret.msg, {
  218. title: __('Warning'),
  219. btn: [__('Login now')],
  220. yes: function (index, layero) {
  221. $(".btn-userinfo").trigger("click");
  222. }
  223. });
  224. } else if (ret.code === -1) {
  225. Layer.confirm(__('Upgrade tips', data.title), {title: __('Warmtips')}, function (index, layero) {
  226. up.removeFile(file);
  227. file.force = true;
  228. up.uploadFile(file);
  229. Layer.close(index);
  230. });
  231. return false;
  232. }
  233. });
  234. // 检测是否登录
  235. $(document).on("mousedown", "#faupload-addon", function (e) {
  236. var userinfo = Controller.api.userinfo.get();
  237. var uid = userinfo ? userinfo.id : 0;
  238. var uploadBtn = Upload.list['faupload-addon'];
  239. if (parseInt(uid) === 0) {
  240. uploadBtn.disable();
  241. $(".btn-userinfo").trigger("click");
  242. return false;
  243. } else {
  244. if (uploadBtn.disabled) {
  245. uploadBtn.enable();
  246. }
  247. }
  248. });
  249. });
  250. // 查看插件首页
  251. $(document).on("click", ".btn-addonindex", function () {
  252. if ($(this).attr("href") == 'javascript:;') {
  253. Layer.msg(__('Not installed tips'), {icon: 7});
  254. } else if ($(this).closest(".operate").find("a.btn-enable").length > 0) {
  255. Layer.msg(__('Not enabled tips'), {icon: 7});
  256. return false;
  257. }
  258. });
  259. // 切换
  260. $(document).on("click", ".btn-switch", function () {
  261. $(".btn-switch").removeClass("active");
  262. $(this).addClass("active");
  263. $("form.form-commonsearch input[name='type']").val($(this).data("type"));
  264. var method = $(this).data("type") == 'local' ? 'hideColumn' : 'showColumn';
  265. table.bootstrapTable(method, 'price');
  266. table.bootstrapTable(method, 'downloads');
  267. table.bootstrapTable('refresh', {url: ($(this).data("url") ? $(this).data("url") : $.fn.bootstrapTable.defaults.extend.index_url), pageNumber: 1});
  268. return false;
  269. });
  270. // 切换分类
  271. $(document).on("click", ".nav-category li a", function () {
  272. $(".nav-category li").removeClass("active");
  273. $(this).parent().addClass("active");
  274. $("form.form-commonsearch input[name='category_id']").val($(this).data("id"));
  275. table.bootstrapTable('refresh', {url: $(this).data("url"), pageNumber: 1});
  276. return false;
  277. });
  278. var tables = [];
  279. $(document).on("click", "#droptables", function () {
  280. if ($(this).prop("checked")) {
  281. Fast.api.ajax({
  282. url: "addon/get_table_list",
  283. async: false,
  284. data: {name: $(this).data("name")}
  285. }, function (data) {
  286. tables = data.tables;
  287. return false;
  288. });
  289. var html;
  290. html = tables.length > 0 ? '<div class="alert alert-warning-light droptablestips" style="max-width:480px;max-height:300px;overflow-y: auto;">' + __('The following data tables will be deleted') + ':<br>' + tables.join("<br>") + '</div>'
  291. : '<div class="alert alert-warning-light droptablestips">' + __('The Addon did not create a data table') + '</div>';
  292. $(html).insertAfter($(this).closest("p"));
  293. } else {
  294. $(".droptablestips").remove();
  295. }
  296. $(window).resize();
  297. });
  298. // 会员信息
  299. $(document).on("click", ".btn-userinfo", function (e, name, version) {
  300. var that = this;
  301. var area = [$(window).width() > 800 ? '500px' : '95%', $(window).height() > 600 ? '400px' : '95%'];
  302. var userinfo = Controller.api.userinfo.get();
  303. if (!userinfo) {
  304. Fast.api.ajax({
  305. url: Config.api_url + '/user/logintpl',
  306. type: 'post',
  307. loading: false,
  308. data: {
  309. version: Config.faversion,
  310. sid: Controller.api.sid()
  311. }
  312. }, function (tpldata, ret) {
  313. Layer.open({
  314. content: Template.render(tpldata, {}),
  315. zIndex: 99,
  316. area: area,
  317. title: __('Login'),
  318. resize: false,
  319. btn: [__('Login')],
  320. yes: function (index, layero) {
  321. var data = $("form", layero).serializeArray();
  322. data.push({name: "faversion", value: Config.faversion});
  323. data.push({name: "sid", value: Controller.api.sid()});
  324. Fast.api.ajax({
  325. url: Config.api_url + '/user/login',
  326. type: 'post',
  327. data: data
  328. }, function (data, ret) {
  329. Controller.api.userinfo.set(data);
  330. Layer.closeAll();
  331. Layer.alert(ret.msg, {title: __('Warning'), icon: 1});
  332. return false;
  333. }, function (data, ret) {
  334. });
  335. },
  336. success: function (layero, index) {
  337. this.checkEnterKey = function (event) {
  338. if (event.keyCode === 13) {
  339. $(".layui-layer-btn0").trigger("click");
  340. return false;
  341. }
  342. };
  343. $(document).on('keydown', this.checkEnterKey);
  344. },
  345. end: function () {
  346. $(document).off('keydown', this.checkEnterKey);
  347. }
  348. });
  349. return false;
  350. });
  351. } else {
  352. Fast.api.ajax({
  353. url: Config.api_url + '/user/userinfotpl',
  354. type: 'post',
  355. data: {
  356. uid: userinfo.id,
  357. token: userinfo.token,
  358. version: Config.faversion,
  359. sid: Controller.api.sid()
  360. }
  361. }, function (tpldata, ret) {
  362. Layer.open({
  363. content: Template.render(tpldata, userinfo),
  364. area: area,
  365. title: __('Userinfo'),
  366. resize: false,
  367. btn: [__('Logout'), __('Close')],
  368. yes: function () {
  369. Fast.api.ajax({
  370. url: Config.api_url + '/user/logout',
  371. data: {
  372. uid: userinfo.id,
  373. token: userinfo.token,
  374. version: Config.faversion,
  375. sid: Controller.api.sid()
  376. }
  377. }, function (data, ret) {
  378. Controller.api.userinfo.set(null);
  379. Layer.closeAll();
  380. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  381. }, function (data, ret) {
  382. Controller.api.userinfo.set(null);
  383. Layer.closeAll();
  384. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  385. });
  386. }
  387. });
  388. return false;
  389. }, function (data) {
  390. Controller.api.userinfo.set(null);
  391. $(that).trigger('click');
  392. return false;
  393. });
  394. }
  395. });
  396. //刷新授权
  397. $(document).on("click", ".btn-authorization", function () {
  398. var userinfo = Controller.api.userinfo.get();
  399. if (!userinfo) {
  400. $(".btn-userinfo").trigger("click");
  401. return false;
  402. }
  403. Layer.confirm(__('Are you sure you want to refresh authorization?'), {icon: 3, title: __('Warmtips')}, function () {
  404. Fast.api.ajax({
  405. url: 'addon/authorization',
  406. data: {
  407. uid: userinfo.id,
  408. token: userinfo.token
  409. }
  410. }, function (data, ret) {
  411. $(".btn-refresh").trigger("click");
  412. Layer.closeAll();
  413. });
  414. });
  415. return false;
  416. });
  417. var install = function (name, version, force) {
  418. var userinfo = Controller.api.userinfo.get();
  419. var uid = userinfo ? userinfo.id : 0;
  420. var token = userinfo ? userinfo.token : '';
  421. Fast.api.ajax({
  422. url: 'addon/install',
  423. data: {
  424. name: name,
  425. force: force ? 1 : 0,
  426. uid: uid,
  427. token: token,
  428. version: version,
  429. faversion: Config.faversion
  430. }
  431. }, function (data, ret) {
  432. Layer.closeAll();
  433. Config['addons'][data.addon.name] = ret.data.addon;
  434. operate(data.addon.name, 'enable', false, function () {
  435. Layer.alert(__('Online installed tips') + (data.addon.testdata ? __('Testdata tips') : ""), {
  436. btn: data.addon.testdata ? [__('Import testdata'), __('Skip testdata')] : [__('OK')],
  437. title: __('Warning'),
  438. yes: function (index) {
  439. if (data.addon.testdata) {
  440. Fast.api.ajax({
  441. url: 'addon/testdata',
  442. data: {
  443. name: name,
  444. uid: uid,
  445. token: token,
  446. version: version,
  447. faversion: Config.faversion
  448. }
  449. }, function (data, ret) {
  450. Layer.close(index);
  451. });
  452. } else {
  453. Layer.close(index);
  454. }
  455. },
  456. icon: 1
  457. });
  458. Controller.api.refresh(table, name);
  459. });
  460. }, function (data, ret) {
  461. var area = Fast.config.openArea != undefined ? Fast.config.openArea : [$(window).width() > 650 ? '650px' : '95%', $(window).height() > 710 ? '710px' : '95%'];
  462. if (ret && ret.code === -2) {
  463. //如果登录已经超时,重新提醒登录
  464. if (uid && uid != ret.data.uid) {
  465. Controller.api.userinfo.set(null);
  466. $(".operate[data-name='" + name + "'] .btn-install").trigger("click");
  467. return;
  468. }
  469. top.Fast.api.open(ret.data.payurl, __('Pay now'), {
  470. area: area,
  471. end: function () {
  472. Fast.api.ajax({
  473. url: 'addon/isbuy',
  474. data: {
  475. name: name,
  476. force: force ? 1 : 0,
  477. uid: uid,
  478. token: token,
  479. version: version,
  480. faversion: Config.faversion
  481. }
  482. }, function () {
  483. top.Layer.alert(__('Pay successful tips'), {
  484. btn: [__('Continue installation')],
  485. title: __('Warning'),
  486. icon: 1,
  487. yes: function (index) {
  488. top.Layer.close(index);
  489. install(name, version);
  490. }
  491. });
  492. return false;
  493. }, function () {
  494. console.log(__('Canceled'));
  495. return false;
  496. });
  497. }
  498. });
  499. } else if (ret && ret.code === -3) {
  500. //插件目录发现影响全局的文件
  501. Layer.open({
  502. content: Template("conflicttpl", ret.data),
  503. shade: 0.8,
  504. area: area,
  505. title: __('Warning'),
  506. btn: [__('Continue install'), __('Cancel')],
  507. end: function () {
  508. },
  509. yes: function () {
  510. install(name, version, true);
  511. }
  512. });
  513. } else {
  514. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  515. }
  516. return false;
  517. });
  518. };
  519. var uninstall = function (name, force, droptables) {
  520. Fast.api.ajax({
  521. url: 'addon/uninstall',
  522. data: {name: name, force: force ? 1 : 0, droptables: droptables ? 1 : 0}
  523. }, function (data, ret) {
  524. delete Config['addons'][name];
  525. Layer.closeAll();
  526. Controller.api.refresh(table, name);
  527. }, function (data, ret) {
  528. if (ret && ret.code === -3) {
  529. //插件目录发现影响全局的文件
  530. Layer.open({
  531. content: Template("conflicttpl", ret.data),
  532. shade: 0.8,
  533. area: area,
  534. title: __('Warning'),
  535. btn: [__('Continue uninstall'), __('Cancel')],
  536. end: function () {
  537. },
  538. yes: function () {
  539. uninstall(name, true, droptables);
  540. }
  541. });
  542. } else {
  543. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  544. }
  545. return false;
  546. });
  547. };
  548. var operate = function (name, action, force, success) {
  549. Fast.api.ajax({
  550. url: 'addon/state',
  551. data: {name: name, action: action, force: force ? 1 : 0}
  552. }, function (data, ret) {
  553. var addon = Config['addons'][name];
  554. addon.state = action === 'enable' ? 1 : 0;
  555. Layer.closeAll();
  556. if (typeof success === 'function') {
  557. success(data, ret);
  558. }
  559. Controller.api.refresh(table, name);
  560. }, function (data, ret) {
  561. if (ret && ret.code === -3) {
  562. //插件目录发现影响全局的文件
  563. Layer.open({
  564. content: Template("conflicttpl", ret.data),
  565. shade: 0.8,
  566. area: area,
  567. title: __('Warning'),
  568. btn: [__('Continue operate'), __('Cancel')],
  569. end: function () {
  570. },
  571. yes: function () {
  572. operate(name, action, true, success);
  573. }
  574. });
  575. } else {
  576. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  577. }
  578. return false;
  579. });
  580. };
  581. var upgrade = function (name, version) {
  582. var userinfo = Controller.api.userinfo.get();
  583. var uid = userinfo ? userinfo.id : 0;
  584. var token = userinfo ? userinfo.token : '';
  585. Fast.api.ajax({
  586. url: 'addon/upgrade',
  587. data: {name: name, uid: uid, token: token, version: version, faversion: Config.faversion}
  588. }, function (data, ret) {
  589. Config['addons'][name] = data.addon;
  590. Layer.closeAll();
  591. Controller.api.refresh(table, name);
  592. }, function (data, ret) {
  593. Layer.alert(ret.msg, {title: __('Warning')});
  594. return false;
  595. });
  596. };
  597. // 点击安装
  598. $(document).on("click", ".btn-install", function () {
  599. var that = this;
  600. var name = $(this).closest(".operate").data("name");
  601. var version = $(this).data("version");
  602. var userinfo = Controller.api.userinfo.get();
  603. var uid = userinfo ? userinfo.id : 0;
  604. if (parseInt(uid) === 0) {
  605. $(".btn-userinfo").trigger("click", name, version);
  606. return false;
  607. }
  608. install(name, version, false);
  609. });
  610. // 点击卸载
  611. $(document).on("click", ".btn-uninstall", function () {
  612. var name = $(this).closest(".operate").data('name');
  613. if (Config['addons'][name].state == 1) {
  614. Layer.alert(__('Please disable the add before trying to uninstall'), {icon: 7});
  615. return false;
  616. }
  617. Template.helper("__", __);
  618. tables = [];
  619. Layer.confirm(Template("uninstalltpl", {addon: Config['addons'][name]}), {focusBtn: false, title: __("Warning")}, function (index, layero) {
  620. uninstall(name, false, $("input[name='droptables']", layero).prop("checked"));
  621. });
  622. });
  623. // 点击配置
  624. $(document).on("click", ".btn-config", function () {
  625. var name = $(this).closest(".operate").data("name");
  626. Fast.api.open("addon/config?name=" + name, __('Setting'));
  627. });
  628. // 点击启用/禁用
  629. $(document).on("click", ".btn-enable,.btn-disable", function () {
  630. var name = $(this).data("name");
  631. var action = $(this).data("action");
  632. operate(name, action, false);
  633. });
  634. // 点击升级
  635. $(document).on("click", ".btn-upgrade", function () {
  636. var name = $(this).closest(".operate").data('name');
  637. if (Config['addons'][name].state == 1) {
  638. Layer.alert(__('Please disable the add before trying to upgrade'), {icon: 7});
  639. return false;
  640. }
  641. var version = $(this).data("version");
  642. Layer.confirm(__('Upgrade tips', Config['addons'][name].title), {title: __('Warmtips')}, function (index, layero) {
  643. upgrade(name, version);
  644. });
  645. });
  646. $(document).on("click", ".operate .btn-group .dropdown-toggle", function () {
  647. $(this).closest(".btn-group").toggleClass("dropup", $(document).height() - $(this).offset().top <= 200);
  648. });
  649. $(document).on("click", ".view-screenshots", function () {
  650. var row = Table.api.getrowbyindex(table, parseInt($(this).data("index")));
  651. var data = [];
  652. $.each(row.screenshots, function (i, j) {
  653. data.push({
  654. "src": j
  655. });
  656. });
  657. var json = {
  658. "title": row.title,
  659. "data": data
  660. };
  661. top.Layer.photos(top.JSON.parse(JSON.stringify({photos: json})));
  662. });
  663. },
  664. add: function () {
  665. Controller.api.bindevent();
  666. },
  667. config: function () {
  668. $(document).on("click", ".nav-group li a[data-toggle='tab']", function () {
  669. if ($(this).attr("href") == "#all") {
  670. $(".tab-pane").addClass("active in");
  671. }
  672. return;
  673. var type = $(this).attr("href").substring(1);
  674. if (type == 'all') {
  675. $(".table-config tr").show();
  676. } else {
  677. $(".table-config tr").hide();
  678. $(".table-config tr[data-group='" + type + "']").show();
  679. }
  680. });
  681. Controller.api.bindevent();
  682. },
  683. api: {
  684. formatter: {
  685. title: function (value, row, index) {
  686. if ($(".btn-switch.active").data("type") == "local") {
  687. // return value;
  688. }
  689. var title = '<a class="title" href="' + row.url + '" data-toggle="tooltip" title="' + __('View addon home page') + '" target="_blank"><span class="' + Fast.api.escape(row.color) + '">' + value + '</span></a>';
  690. if (row.screenshots && row.screenshots.length > 0) {
  691. title += ' <a href="javascript:;" data-index="' + index + '" class="view-screenshots text-success" title="' + __('View addon screenshots') + '" data-toggle="tooltip"><i class="fa fa-image"></i></a>';
  692. }
  693. return title;
  694. },
  695. intro: function (value, row, index) {
  696. return row.intro + (row.extend ? "<a href='" + Fast.api.escape(row.extend[1]) + "' class='" + Fast.api.escape(row.extend[2]) + "'>" + Fast.api.escape(row.extend[0]) + "</a>" : "");
  697. },
  698. operate: function (value, row, index) {
  699. return Template("operatetpl", {item: row, index: index});
  700. },
  701. toggle: function (value, row, index) {
  702. if (!row.addon) {
  703. return '';
  704. }
  705. return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Click to toggle status') + '" class="btn btn-toggle btn-' + (row.addon.state == 1 ? "disable" : "enable") + '" data-action="' + (row.addon.state == 1 ? "disable" : "enable") + '" data-name="' + row.name + '"><i class="fa ' + (row.addon.state == 0 ? 'fa-toggle-on fa-rotate-180 text-gray' : 'fa-toggle-on text-success') + ' fa-2x"></i></a>';
  706. },
  707. author: function (value, row, index) {
  708. var url = 'javascript:';
  709. if (typeof row.homepage !== 'undefined') {
  710. url = row.homepage;
  711. } else if (typeof row.qq !== 'undefined' && row.qq) {
  712. url = 'https://wpa.qq.com/msgrd?v=3&uin=' + row.qq + '&site=&menu=yes';
  713. }
  714. return '<a href="' + url + '" target="_blank" data-toggle="tooltip" class="text-primary">' + value + '</a>';
  715. },
  716. price: function (value, row, index) {
  717. if (isNaN(value)) {
  718. return value;
  719. }
  720. return parseFloat(value) == 0 ? '<span class="text-success">' + __('Free') + '</span>' : '<span class="text-danger">¥' + value + '</span>';
  721. },
  722. downloads: function (value, row, index) {
  723. return value;
  724. },
  725. version: function (value, row, index) {
  726. return row.addon && row.addon.version != row.version ? '<a href="' + row.url + '?version=' + row.version + '" target="_blank"><span class="releasetips text-primary" data-toggle="tooltip" title="' + __('New version tips', row.version) + '">' + row.addon.version + '<i></i></span></a>' : row.version;
  727. },
  728. home: function (value, row, index) {
  729. return row.addon && parseInt(row.addon.state) > 0 ? '<a href="' + row.addon.url + '" data-toggle="tooltip" title="' + __('View addon index page') + '" target="_blank"><i class="fa fa-home text-primary"></i></a>' : '<a href="javascript:;"><i class="fa fa-home text-gray"></i></a>';
  730. },
  731. },
  732. bindevent: function () {
  733. Form.api.bindevent($("form[role=form]"));
  734. },
  735. userinfo: {
  736. get: function () {
  737. if (typeof $.cookie !== 'undefined') {
  738. var userinfo = $.cookie('fastadmin_userinfo');
  739. } else {
  740. var userinfo = sessionStorage.getItem("fastadmin_userinfo");
  741. }
  742. return userinfo ? JSON.parse(userinfo) : null;
  743. },
  744. set: function (data) {
  745. if (typeof $.cookie !== 'undefined') {
  746. if (data) {
  747. $.cookie("fastadmin_userinfo", JSON.stringify(data));
  748. } else {
  749. $.removeCookie("fastadmin_userinfo");
  750. }
  751. } else {
  752. if (data) {
  753. sessionStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
  754. } else {
  755. sessionStorage.removeItem("fastadmin_userinfo");
  756. }
  757. }
  758. }
  759. },
  760. sid: function () {
  761. var sid = $.cookie('fastadmin_sid');
  762. if (!sid) {
  763. sid = Math.random().toString(20).substr(2, 12);
  764. $.cookie('fastadmin_sid', sid);
  765. }
  766. return sid;
  767. },
  768. refresh: function (table, name) {
  769. //刷新左侧边栏
  770. Fast.api.refreshmenu();
  771. //刷新行数据
  772. if ($(".operate[data-name='" + name + "']").length > 0) {
  773. var tr = $(".operate[data-name='" + name + "']").closest("tr[data-index]");
  774. var index = tr.data("index");
  775. var row = Table.api.getrowbyindex(table, index);
  776. row.addon = typeof Config['addons'][name] !== 'undefined' ? Config['addons'][name] : undefined;
  777. table.bootstrapTable("updateRow", {index: index, row: row});
  778. } else if ($(".btn-switch.active").data("type") == "local") {
  779. $(".btn-refresh").trigger("click");
  780. }
  781. }
  782. }
  783. };
  784. return Controller;
  785. });