something with web

This commit is contained in:
YouROK
2020-11-12 15:48:32 +03:00
parent bba9d89346
commit 4645e7226e
7 changed files with 252 additions and 37 deletions

View File

@@ -19,6 +19,9 @@ const header = `
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js" integrity="sha384-LtrjvnR4Twt/qOuYxE721u19sVFLVSA4hf/rRt6PrZTmiPltdZcI7q7PXQBYTKyf" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js" integrity="sha384-LtrjvnR4Twt/qOuYxE721u19sVFLVSA4hf/rRt6PrZTmiPltdZcI7q7PXQBYTKyf" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.11.0/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.4.0/backbone-min.js"></script>
<title>TorrServer ` + version.Version + `</title> <title>TorrServer ` + version.Version + `</title>
</head> </head>
` `

View File

@@ -0,0 +1,83 @@
// Torrents
function addTorrent(link, title, poster, save, done, fail){
torrent("add",link,null,title,poster,save,done,fail);
}
function getTorrent(hash, done, fail){
torrent("get",null,hash,null,null,null,done,fail);
}
function remTorrent(hash, done, fail){
torrent("rem",null,hash,null,null,null,done,fail);
}
function listTorrent(done, fail){
torrent("list",null,null,null,null,null,done,fail);
}
function dropTorrent(hash, done, fail){
torrent("drop",null,hash,null,null,null,done,fail);
}
function torrent(action, link, hash, title, poster, save, done, fail){
var req = JSON.stringify({ action:action, link: link, title: title, poster: poster, save_to_db: save});
$.post('/torrents',req)
.done(function( data ) {
if (done)
done(data);
})
.fail(function( data ) {
if (fail)
fail(data);
});
}
//
// Settings
function getSettings(done, fail){
sendApi("get",null,"/settings",done,fail);
}
function setSettings(sets, done, fail){
sendApi("set",sets,"/settings",done,fail);
}
//
// Viewed
function listViewed(done, fail){
sendApi("list",null,"/viewed",done,fail);
}
function setViewed(hash, index, done, fail){
var obj = {"hash":hash, "file_index":index};
sendApi("set",obj,"/viewed",done,fail);
}
function remViewed(hash, index, done, fail){
var obj = {"hash":hash, "file_index":index};
sendApi("rem",obj,"/viewed",done,fail);
}
//
function sendApi(action, obj, path, done, fail){
if (obj==null)
obj={};
obj[action]=action;
var req = JSON.stringify(obj);
$.post(path,req)
.done(function( data ) {
if (done)
done(data);
})
.fail(function( data ) {
if (fail)
fail(data);
});
}
function humanizeSize(size) {
if (typeof size == 'undefined' || size == 0)
return "";
var i = Math.floor( Math.log(size) / Math.log(1024) );
return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
}

View File

@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="` + faviconB64 + `" rel="icon" type="image/x-icon">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.min.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.5.4/umd/popper.min.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js" integrity="sha384-LtrjvnR4Twt/qOuYxE721u19sVFLVSA4hf/rRt6PrZTmiPltdZcI7q7PXQBYTKyf" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.11.0/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
<title>TorrServer ` + version.Version + `</title>
</head>
<body ng-app="app">
<link rel="stylesheet" href="/main.css">
<script src="/api.js"></script>
<script src="/main.js"></script>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="navbar-nav">
<a href="#" class="nav-item nav-link torrents">Torrents</a>
<a href="#" class="nav-item nav-link settings">Settings</a>
<a href="#" class="nav-item nav-link cache">Cache</a>
<a href="#" class="nav-item nav-link about">About</a>
</div>
</nav>
<div id="torrents"></div>
</body>
</html>

View File

@@ -0,0 +1,47 @@
(function() {
const Torrent = Backbone.Model.extend({
defaults: function() {
return {
title: "",
torr: {},
url:"/torrents"
};
},
remove: function() {
this.destroy();
},
fetch: function (){
const collection = this;
getTorrent(this.torr.hash, function (torr){
console.log(torr);
collection.reset(torr);
})
}
});
var TorrentList = Backbone.Collection.extend({
model: Torrent,
update: function(){
listTorrent(function(torrs){
// torrs.forEach(tr=>
//
// )
Torrents.create({title:""});
console.log(Torrents);
},function (error) {
console.log(error);
});
}
});
var AppView = Backbone.View.extend({
el: $("#torrents"),
initialize: function() {
Torrents.update();
},
});
var Torrents = new TorrentList;
var App = new AppView;
})();

View File

@@ -10,12 +10,31 @@ func (t *Template) parseMainPage() {
const mainPage = ` const mainPage = `
<!DOCTYPE html> <!DOCTYPE html>
<html lang="ru"> <html lang="en">
` + header + ` ` + header + `
<body> <body ng-app="app">
<script src="/api.js"></script> <script src="/api.js"></script>
<script src="/main.js"></script> <script src="/main.js"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-light {{active}}" ng-click="$event.preventDefault()">
<div class="navbar-nav">
<a href="#" class="nav-item nav-link torrents" ng-click="active='torrents'">Torrents</a>
<a href="#" class="nav-item nav-link settings" ng-click="active='settings'">Settings</a>
<a href="#" class="nav-item nav-link cache" ng-click="active='cache'">Cache</a>
<a href="#" class="nav-item nav-link about" ng-click="active='about'">About</a>
</div>
</nav>
<p ng-hide="active">Please click a menu item</p>
<p ng-show="active">You chose <b>{{active}}</b></p>
</body>
</html>
`
const tmp = `
<style> <style>
.wrap { .wrap {
white-space: normal; white-space: normal;
@@ -64,5 +83,16 @@ const mainPage = `
</span> </span>
</footer> </footer>
</body> </body>
<script id="contactTemplate" type="text/template">
<img src="<%= photo %>" alt="<%= name %>" />
<h1><%= name %><span><%= type %></span></h1>
<div><%= address %></div>
<dl>
<dt>Tel:</dt><dd><%= tel %></dd>
<dt>Email:</dt><dd><a href="mailto:<%= email %>"><%= email %></a></dd>
</dl>
</script>
</html> </html>
` `

View File

@@ -1,40 +1,50 @@
package template package template
const mainJS = ` const mainJS = `
$(document).ready(function() { (function() {
watchInfo(); const Torrent = Backbone.Model.extend({
defaults: function() {
return {
title: "",
torr: {},
url:"/torrents"
};
},
remove: function() {
this.destroy();
},
fetch: function (){
const collection = this;
getTorrent(this.torr.hash, function (torr){
console.log(torr);
collection.reset(torr);
})
}
}); });
var lastTorrHtml = ''; var TorrentList = Backbone.Collection.extend({
model: Torrent,
function watchInfo(){ update: function(){
reloadTorrents(); listTorrent(function(torrs){
setInterval(function() { // torrs.forEach(tr=>
//
}, 1000); // )
Torrents.create({title:""});
console.log(Torrents);
},function (error) {
console.log(error);
});
} }
});
function reloadTorretns(){ var AppView = Backbone.View.extend({
var torrents = $("#torrents"); el: $("#torrents"),
torrents.empty(); initialize: function() {
} Torrents.update();
},
function loadTorrentInfoHtml(){ });
}
const torrElem =
` var Torrents = new TorrentList;
var App = new AppView;
//Backbone.js })();
var torrElem = `"""
<div class="btn-group d-flex" role="group">
<button type="button" class="btn btn-secondary wrap w-100" data-toggle="collapse" data-target="#info_'+tor.Hash+'"></button>';
<a role="button" class="btn btn-secondary" href="'+tor.Playlist+'"><i class="fas fa-th-list"></i> Плейлист</a>';
<button type="button" class="btn btn-secondary"><i class="fas fa-info"></i></a>';
<button type="button" class="btn btn-secondary"><i class="fas fa-trash-alt"></i> Удалить</button>';
</div>
"""
` `

View File

@@ -56,6 +56,7 @@ func (t *Template) parsePage(name, page string) error {
} else { } else {
tmpl = t.templates.New(name) tmpl = t.templates.New(name)
} }
tmpl.Delims("<<", ">>")
_, err := tmpl.Parse(s) _, err := tmpl.Parse(s)
if err != nil { if err != nil {
return err return err