index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // index block init.
  2. $(function() {
  3. $('#dashboard').dashboard( {
  4. height : 245,
  5. draggable : false,
  6. resizable : false,
  7. shadowType : false,
  8. afterOrdered : sortBlocks,
  9. sensitive : true,
  10. onResize : resizeBlock,
  11. panelRemovingTip : config.confirmRemoveBlock
  12. });
  13. /**
  14. * refresh index block
  15. * @access public
  16. * @return void
  17. */
  18. var refreshTimer = 1000 * 60 * 5;
  19. setInterval(function ()
  20. {
  21. $('.refresh-panel').trigger('click');
  22. }, refreshTimer);
  23. });
  24. var versionApiUrl = v.versionApiUrl;
  25. var xxcCurrentVersion = v.currentVersion;
  26. var upgradeNoticeInfo = $.zui.store.get('upgradeNoticeInfo');
  27. var upgradeNoticeStatus = upgradeNoticeInfo ? upgradeNoticeInfo.status : ''; // notice status `hide`|`show`
  28. var upgradeNoticeInfoInit = {
  29. date : new Date().getTime(),
  30. version : xxcCurrentVersion,
  31. status: 'show',
  32. }
  33. $(function ()
  34. {
  35. showNotice(xxcCurrentVersion, upgradeNoticeInfo);
  36. $('#noticeGoUpgrade .close').on('click', function ()
  37. {
  38. upgradeNoticeInfoInit.status = 'hide';
  39. $.zui.store.set('upgradeNoticeInfo', upgradeNoticeInfoInit);
  40. });
  41. });
  42. function showNotice(xxcCurrentVersion, upgradeNoticeInfo)
  43. {
  44. if(upgradeNoticeStatus === 'hide' && (new Date().getTime() - upgradeNoticeInfo.date) < 604800000) return;
  45. // upgradeNoticeInfo && store notice info version > xxc current version
  46. if(upgradeNoticeInfo && window.compareVersions(upgradeNoticeInfo.version, xxcCurrentVersion) === '1')
  47. {
  48. $('#noticeGoUpgrade .version').text(upgradeNoticeInfo.version);
  49. $('#noticeGoUpgrade').show();
  50. }
  51. else
  52. {
  53. $.get(versionApiUrl, function (data, status)
  54. {
  55. if(status === 'success')
  56. {
  57. var versionInfo = typeof data === 'string' ? JSON.parse(data) : data;
  58. var xxcVersion = versionInfo[0]['xxcVersion'];
  59. if(!xxcCurrentVersion || window.compareVersions(xxcVersion, xxcCurrentVersion) === '1')
  60. {
  61. $('#noticeGoUpgrade .version').text(xxcVersion);
  62. upgradeNoticeInfoInit.version = xxcVersion;
  63. $.zui.store.set('upgradeNoticeInfo', upgradeNoticeInfoInit);
  64. $('#noticeGoUpgrade').show();
  65. }
  66. }
  67. });
  68. }
  69. }
  70. /**
  71. * Sort blocks.
  72. *
  73. * @param object $orders format is {'block2' : 1, 'block1' : 2, oldOrder : newOrder}
  74. * @access public
  75. * @return void
  76. */
  77. function sortBlocks(orders)
  78. {
  79. var oldOrder = new Array();
  80. var newOrder = new Array();
  81. for(i in orders)
  82. {
  83. oldOrder.push(i.replace('block', ''));
  84. newOrder.push(orders[i]);
  85. }
  86. $.getJSON(createLink('block', 'sort', 'oldOrder=' + oldOrder.join(',') + '&newOrder=' + newOrder.join(',')), function(data)
  87. {
  88. if(data.result != 'success') return false;
  89. $('#home .panels-container .panel:not(.panel-dragging-holder)').each(function()
  90. {
  91. var $this = $(this);
  92. var index = $this.data('order');
  93. var url = createLink('entry', 'printBlock', 'index=' + index);
  94. /* Update new index for block id edit and delete. */
  95. $this.attr('id', 'block' + index).data('id', index).attr('data-url', url).data('url', url);
  96. $this.find('.panel-actions .edit-block').attr('href', createLink('block', 'admin', 'index=' + index));
  97. });
  98. });
  99. }
  100. /**
  101. * Resize block
  102. * @param object $event
  103. * @access public
  104. * @return void
  105. */
  106. function resizeBlock(event)
  107. {
  108. var blockID = event.element.find('.panel').data('blockid');
  109. var data = event.type == 'vertical' ? event.height : event.grid;
  110. $.getJSON(createLink('block', 'resize', 'id=' + blockID + '&type=' + event.type + '&data=' + data), function(data)
  111. {
  112. if(data.result !== 'success') event.revert();
  113. });
  114. }