addon.js 37 KB

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