Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feat(Frontend): Add localforage As Cache Provider
Browse files Browse the repository at this point in the history
Signed-off-by: Rhilip <rhilipruan@gmail.com>
  • Loading branch information
Rhilip committed Jun 7, 2019
1 parent bec2fc0 commit f830f22
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 59 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
- **README:** Fix Mailer Env typo

### Feat
- **Auth:** Add UserRecover Form
- **Auth:** Add full Advanced Options for Login
- **Auth:** Add UserRecover Form
- **Cleanup:** Add disable cleanup job by set priority to 0
- **Cleanup:** Cleanup dead peers
- **Debug:** Add extend debug output
- **Debug:** Remove DebugMiddleware
- **Debug:** Add extend debug output
- **Email:** Add email template
- **Form Validator:** Add library bootstrap-validator library
- **Invite:** Add base invite table
Expand All @@ -26,6 +26,7 @@
- **Task:** Add task process support
- **Timer:** Add Timer Example
- **Torrent:** Add folder size sum in torrent file modal
- **Typo:** Fix Typo of `secret`

### Fix
- **Admin Panel:** Fix index num in Redis Keys Page
Expand Down
25 changes: 17 additions & 8 deletions apps/components/User/UserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ trait UserTrait
private $invites;
private $temp_invites_sum;
private $temp_invites_details;
private $pending_invites;
private $invitees;

protected $peer_status;
protected $infoCacheKey;
Expand Down Expand Up @@ -351,15 +353,22 @@ public function getTempInviteDetails() {
return $this->temp_invites_details;
}

public function getPendingInvites() { // FIXME
return app()->pdo->createCommand('SELECT * FROM `invite` WHERE inviter_id = :uid AND expire_at > NOW() AND used = 0')->bindParams([
'uid'=> $this->id
])->queryAll();
public function getPendingInvites() {
if (is_null($this->pending_invites)) {
$this->pending_invites = app()->pdo->createCommand('SELECT * FROM `invite` WHERE inviter_id = :uid AND expire_at > NOW() AND used = 0')->bindParams([
'uid'=> $this->id
])->queryAll();
}
return $this->pending_invites;
}

public function getInvitees() { // FIXME
return app()->pdo->createCommand('SELECT id,username,email,status,class,uploaded,downloaded FROM `users` WHERE `invite_by` = :uid')->bindParams([
'uid' => $this->id
])->queryAll();
public function getInvitees() {
if (is_null($this->invitees)) {
$this->invitees = app()->pdo->createCommand('SELECT id,username,email,status,class,uploaded,downloaded FROM `users` WHERE `invite_by` = :uid')->bindParams([
'uid' => $this->id
])->queryAll();
}

return $this->invitees;
}
}
112 changes: 64 additions & 48 deletions apps/public/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function location_search_replace(new_params) {
return '?' + search.toString();
}

jQuery(document).ready(function() {
jQuery(document).ready(function () {
// Drop all support of IE 6-11
if ($.zui.browser.ie) {
$.zui.browser.tip();
Expand All @@ -50,7 +50,7 @@ jQuery(document).ready(function() {
page: _location_search.get('page') || 0,
maxNavCount: 8,
elements: ['first_icon', 'prev_icon', 'pages', 'next_icon', 'last_icon'],
linkCreator: function(page, pager) {
linkCreator: function (page, pager) {
return location_search_replace({
'page': page,
'limit': pager.recPerPage
Expand All @@ -60,23 +60,23 @@ jQuery(document).ready(function() {

// Captcha Img Re-flush
let captcha_img_another = $('.captcha_img');
captcha_img_another.on('click',function () {
$(this).attr('src','/captcha?t=' + Date.now()) // Change src to get another captcha image
captcha_img_another.on('click', function () {
$(this).attr('src', '/captcha?t=' + Date.now()) // Change src to get another captcha image
.parent('.captcha_img_load').addClass('load-indicator loading'); // Add loading indicator in parent of img tag
});
captcha_img_another.on('load',function () {
captcha_img_another.on('load', function () {
$(this).parent('.captcha_img_load').removeClass('load-indicator loading');
});

// TODO Add Scroll to TOP fixbar

// Common Function
function create_error_notice(text,option) {
function create_error_notice(text, option) {
option = $.extend({
icon: 'exclamation-sign',
type: 'danger',
placement: 'top-right'
},option);
}, option);
return new $.zui.Messager(text, option).show();
}

Expand All @@ -93,7 +93,9 @@ jQuery(document).ready(function() {
password_strength.show();
strength_text.html(paswordStrengthText[result.score]);
let feedback = [];
if (result.feedback.warning !== "") {feedback.push(result.feedback.warning);}
if (result.feedback.warning !== "") {
feedback.push(result.feedback.warning);
}
feedback = feedback.concat(result.feedback.suggestions);
if (feedback.length > 0) {
strength_suggest.html('<ul><li>' + feedback.join('</li><li>') + '</li></ul>');
Expand All @@ -118,7 +120,7 @@ jQuery(document).ready(function() {
help_info.removeClass('fa-eye-slash').addClass('fa-eye');
}
});

// Torrent favour Add/Remove action
$('.torrent-favour').click(function () {
let that = $(this);
Expand All @@ -143,11 +145,12 @@ jQuery(document).ready(function() {
// View Torrent File list
let ext2Icon = $.zui.store.get('rid_ext2Icon');
if (!ext2Icon) {
$.getJSON('/static/json/ext2Icon.json',function (data) {
$.getJSON('/static/json/ext2Icon.json', function (data) {
ext2Icon = data;
$.zui.store.set('rid_ext2Icon', ext2Icon);
})
}

function get_ext_icon(ext) {
for (let type in ext2Icon) {
if (ext2Icon[type].indexOf(ext.toLowerCase()) >= 0) {
Expand All @@ -158,46 +161,58 @@ jQuery(document).ready(function() {
}

$('.torrent-files').click(function () {
const torrent_files_localforage = localforage.createInstance({name: 'torrent_files'});

let that = $(this);
let tid = that.attr('data-tid');

function list_worker(tree, par = '') {
let ret = '';
let size = 0;
for (let k in tree) {
let v = tree[k];
if (typeof v == 'object') {
let [in_ret, in_size] = list_worker(v, par + "/" + k);
ret += `<li${par === '' ? ' class="open"' : ''}><a href="#"><b>${k}</b> (<span class="file-size" data-size="${v}">${humanFileSize(in_size)}</span>)</a><ul>${in_ret}</ul></li>`;
size += in_size;
} else {
let ext = k.substr(k.lastIndexOf('.') + 1).toLowerCase();
torrent_files_localforage.getItem(tid, function (err, value) {
function list_worker(tree, par = '') {
let ret = '';
let size = 0;
for (let k in tree) {
let v = tree[k];
if (typeof v == 'object') {
let [in_ret, in_size] = list_worker(v, par + "/" + k);
ret += `<li${par === '' ? ' class="open"' : ''}><a href="#"><b>${k}</b> (<span class="file-size" data-size="${v}">${humanFileSize(in_size)}</span>)</a><ul>${in_ret}</ul></li>`;
size += in_size;
} else {
let ext = k.substr(k.lastIndexOf('.') + 1).toLowerCase();

ret += `<li><i class="fa ${get_ext_icon(ext)} fa-fw"></i><b>${k}</b> (<span class="file-size" data-size="${v}">${humanFileSize(v)}</span>)</li>`;
size += v;
ret += `<li><i class="fa ${get_ext_icon(ext)} fa-fw"></i><b>${k}</b> (<span class="file-size" data-size="${v}">${humanFileSize(v)}</span>)</li>`;
size += v;
}
}
return [ret, size];
}
return [ret, size];
}

// TODO Add Client Cache ( innodb ) since the file list will not change in specific torrent
$.get(api_point + '/torrent/filelist', {'tid': tid}, function (res) {
if (res.success) {
let file_list = res.result;

(new $.zui.ModalTrigger({
name: 'torrent_filelist_model',
showHeader: false,
size: 'lg',
moveable: true,
custom: "<ul class='tree tree-lines tree-folders' data-ride='tree' id='torrent_filelist'>" + list_worker(file_list)[0] + "</ul>"
})).show({
shown:function () {
$('#torrent_filelist').tree();
}
});
function build_file_tree(res) {
if (res.success) {
let file_list = res.result;

(new $.zui.ModalTrigger({
name: 'torrent_filelist_model',
showHeader: false,
size: 'lg',
moveable: true,
custom: "<ul class='tree tree-lines tree-folders' data-ride='tree' id='torrent_filelist'>" + list_worker(file_list)[0] + "</ul>"
})).show({
shown: function () {
$('#torrent_filelist').tree();
}
});
} else {
create_error_notice(res.errors.join(', '));
}
}

if (value !== null) {
build_file_tree(value);
} else {
create_error_notice(res.errors.join(', '));
$.getJSON(api_point + '/torrent/filelist', {'tid': tid}, function (res) {
torrent_files_localforage.setItem(tid, res);
build_file_tree(res);
});
}
});
});
Expand All @@ -208,7 +223,7 @@ jQuery(document).ready(function() {
$(this).next('ul').toggle();
});
}

// User Invite
$('.invite-btn').click(function () {
$('.invite-btn').removeAttr('disabled');
Expand All @@ -218,7 +233,7 @@ jQuery(document).ready(function() {
let invite_type = that.data('type');
let invite_panel_notice = $('#invite_type');

that.attr('disabled','disabled');
that.attr('disabled', 'disabled');
$('#invite_create_form input[name=invite_type]').val(invite_type);
if (invite_type === 'temporarily') {
$('#invite_create_form input[name=temp_id]').val(that.data('temp-invite-id'));
Expand All @@ -227,7 +242,7 @@ jQuery(document).ready(function() {
invite_panel_notice.text('(Using Permanent Invite)');
}
});

// Show Extend debug info of Database sql execute and Redis key hit
if (typeof _extend_debug_info !== 'undefined' && _extend_debug_info) {
$('#extend_debug_info').modalTrigger({
Expand All @@ -237,16 +252,17 @@ jQuery(document).ready(function() {
let parsed_sql_data = JSON.parse(_sql_data || '[]');
let parsed_redis_data = JSON.parse(_redis_data || '{}');
ret += '<b>SQL query list:</b><ul>';
$.each(parsed_sql_data,function (i,v) {
$.each(parsed_sql_data, function (i, v) {
ret += `<li><code>${v}</code></li>`;
});
ret += '</ul>';
ret += '<b>Redis keys hit: (Some keys hit may not appear here)</b><ul>';
$.each(parsed_redis_data,function (k, v) {
$.each(parsed_redis_data, function (k, v) {
ret += '<li><code>' + k + "</code> : " + v + '</li>';
});
ret += '</ul>';
return ret;
}});
}
});
}
});
1 change: 1 addition & 0 deletions apps/views/layout/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
<!-- Javascript of Library -->
<script src="/lib/jquery/dist/jquery.min.js"></script>
<script src="/lib/zui/dist/js/zui.min.js"></script>
<script src="/lib/localforage/dist/localforage.min.js"></script>

<!-- Custom Javascript of this template -->
<script src="/static/js/main.js?<?= $css_tag ?>"></script>
Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"respond": "^1.4.2",
"flag-css": "^0.0.12",
"zxcvbn": "^4.4.2",
"bootstrap-validator": "^0.11.9"
"bootstrap-validator": "^0.11.9",
"localforage": "^1.7.3"
}
}

0 comments on commit f830f22

Please sign in to comment.