Commit d4d11e93 authored by Leo Iannacone's avatar Leo Iannacone

Complete refactory on client side code

parent 2ee92017
...@@ -22,92 +22,44 @@ function get_path(path) { ...@@ -22,92 +22,44 @@ function get_path(path) {
} }
function select_navbar_voice(data) { function select_navbar_voice(data) {
$('#distributions li').removeClass('active')
$('#distribution-' + data.distribution.name).addClass('active')
}
function build_hash(data) {
hash = "#"
if (data.distribution && data.distribution.name) {
hash = hash + data.distribution.name
if (data.package && data.package.name && data.package.version) {
hash = hash + '/' + data.package.name + "/" + data.package.version
if (data.file && data.file.name)
hash = hash + '/' + data.file.name
}
}
return hash
} }
function update_breadcrumb(hash) { function update_breadcrumb(hash) {
new_html = ''
new_hash = '#'
info = hash.split('/')
for (var i = 0; i < info.length ; i++) {
new_hash += info[i]
if (i == (info.length - 1))
new_html += '<li class="active">' + info[i] + '</li>'
else
new_html += '<li><a href="' + new_hash + '">' + info[i] + '</a>'
new_hash += '/'
}
$('.breadcrumb').html(new_html)
} }
var socket = io.connect('//localhost:3000'); var socket = io.connect('//localhost:3000');
var old_data = Utils.from_hash_to_data()
socket.on('distributions', function(distributions) { socket.on('distributions', function(distributions) {
$('#distributions ul').html(''); Page.navbar.update(distributions)
distributions.forEach(function (name){
$('#distributions ul').append('<li id="distribution-' + name +'"><a href="#' + name + '">' + name + '</li>');
});
}); });
socket.on('distribution_packages', function(data){ socket.on('distribution_packages', function(data){
$('#packages ul').html('') Page.packages.set(data)
tmp = data
tmp.file = null
data.distribution.packages.forEach(function(p){
tmp.package = p
div = $('#packages ul').append('<li><a href="' + build_hash(tmp) + '">'+ p.name + ' <span>'+p.version+'</span></a></li>')
})
}) })
socket.on('package_file_list', function(data){ socket.on('package_file_list', function(data){
$('#files ul').html(''); Page.files.set(data)
$('#debs ul').html('');
$('#archives ul').html('')
tmp = data
data.package.files.forEach(function(f){
tmp.file = f
$('#files ul').append('<li><a name="'+ f.orig_name +'" href="'+ build_hash(tmp) + '">' + f.name + '</a></li>')
})
data.package.debs.forEach(function(f){
$('#debs ul').append('<li><a name="'+ f.orig_name +'" href="' + f.path + '">' + f.name +'</a> <span>.' + f.label + '</span></li>')
})
data.package.archives.forEach(function(f){
$('#archives ul').append('<li><a name="'+ f.orig_name +'" href="' + f.path + '">' + f.name +'</a></li>')
})
}) })
socket.on('file', function (data) { socket.on('file', function (data) {
$('#file .title').html(data.file.orig_name) Page.file.set(data)
$("#file pre").html(data.file.content)
}) })
socket.on('file_newcontent', function(data) { socket.on('file_newcontent', function(data) {
new_html = $("#file").html() + data.file.new_content Page.file.append(data)
$("#file").html(new_html)
}) })
socket.on('error', function() { console.error(arguments) }); socket.on('error', function() { console.error(arguments) });
$(window).on('hashchange', function() { $(window).on('hashchange', function() {
get_path(window.location.hash.replace('#','')); data = Utils.from_hash_to_data()
Page.update(data, old_data)
old_data = data
}); });
$(window).on('load', function (){ $(window).on('load', function (){
get_path(window.location.hash.replace('#','')); Page.update(old_data)
}); });
var Page = {
title: {
set: function(data) {
if (! data)
data = Utils.from_hash_to_data()
label = ''
if (Utils.check_data_file(data))
label = data.package.name + '_' + data.package.version + '.' + data.file.name
else if (Utils.check_data_package(data))
label = data.package.name + '_' + data.package.version
else if (Utils.check_data_distribution(data))
label = data.distribution.name
$('#title').html(label)
},
clean: function() {
$('#title').html('')
}
},
packages: {
set: function (data) {
$('#packages ul').html('')
tmp = data
tmp.file = null
data.distribution.packages.forEach(function(p){
tmp.package = p
div = $('#packages ul').append('<li><a href="' + Utils.from_data_to_hash(tmp) + '">'+ p.name + ' <span>'+p.version+'</span></a></li>')
})
},
clean: function () {
$('#packages ul').html('')
},
get: function (data) {
if (! data)
data = Utils.from_hash_to_data()
if (Utils.check_data_distribution(data))
socket.emit("get_distribution_packages", data)
}
},
files: {
set: function (data) {
Page.files.clean()
tmp = data
if (data.package.files) {
data.package.files.forEach(function(f){
tmp.file = f
$('#logs ul').append('<li><a name="'+ f.orig_name +'" href="'+ Utils.from_data_to_hash(tmp) + '">' + f.name + '</a></li>')
})
}
if (data.package.debs) {
data.package.debs.forEach(function(f){
$('#debs ul').append('<li><a name="'+ f.orig_name +'" href="' + f.path + '">' + f.name +'</a> <span>.' + f.label + '</span></li>')
})
}
if (data.package.archives) {
data.package.archives.forEach(function(f){
$('#archives ul').append('<li><a name="'+ f.orig_name +'" href="' + f.path + '">' + f.name +'</a></li>')
})
}
$('#files').show()
},
clean: function() {
$('#logs ul').html('');
$('#debs ul').html('');
$('#archives ul').html('')
$('#files').hide()
},
get: function (data) {
if (! data)
data = Utils.from_hash_to_data()
if (Utils.check_data_package(data))
socket.emit("get_package_file_list", data)
}
},
file: {
set: function(data) {
$("#file pre").html(data.file.content)
$("#file").show()
},
clean: function() {
$('#file pre').html('')
$('#file').hide()
},
append: function(data) {
new_html = $("#file pre").html() + data.file.new_content
$("#file pre").html(new_html)
},
get: function(data) {
if (! data)
data = Utils.from_hash_to_data()
if (Utils.check_data_file(data))
socket.emit("get_file", data)
}
},
breadcrumb: {
update: function(hash) {
if (! hash )
hash = window.location.hash
hash = hash.replace('#', '')
new_html = ''
new_hash = '#'
info = hash.split('/')
for (var i = 0; i < info.length ; i++) {
new_hash += info[i]
if (i == (info.length - 1))
new_html += '<li class="active">' + info[i] + '</li>'
else
new_html += '<li><a href="' + new_hash + '">' + info[i] + '</a>'
new_hash += '/'
}
$('.breadcrumb').html(new_html)
}
},
navbar: {
select: function (data) {
if (! data)
data = Utils.from_hash_to_data()
$('#distributions li').removeClass('active')
$('#distribution-' + data.distribution.name).addClass('active')
},
update: function (distributions) {
$('#distributions ul').html('');
distributions.forEach(function (name){
$('#distributions ul').append('<li id="distribution-' + name +'"><a href="#' + name + '">' + name + '</li>');
});
Page.navbar.select()
},
},
clean: function() {
Page.title.clean()
Page.packages.clean()
Page.files.clean()
Page.file.clean()
Page.navbar.select()
Page.breadcrumb.update()
},
update: function(data, old_data) {
if (! old_data ) {
if (! data )
Page.populate()
else
Page.populate(data)
return;
}
else {
if (! Utils.check_data_distribution(old_data) ||
! Utils.check_data_distribution(data) ||
data.distribution.name != old_data.distribution.name)
{
Page.clean()
Page.populate(data)
}
else if (
! Utils.check_data_package(old_data) ||
! Utils.check_data_package(data) ||
data.package.name != old_data.package.name ||
data.package.version != old_data.package.version )
{
Page.file.clean()
Page.files.get(data)
}
else if (
! Utils.check_data_file(old_data) ||
! Utils.check_data_file(data) ||
data.file.name != old_data.file.name
)
{
Page.file.get()
}
Page.title.set(data)
Page.breadcrumb.update()
}
},
populate: function (data) {
Page.clean()
if (! data )
data = Utils.from_hash_to_data()
Page.packages.get(data)
Page.files.get(data)
Page.file.get(data)
Page.navbar.select()
Page.breadcrumb.update()
Page.title.set(data)
}
}
var Utils = {
from_hash_to_data: function (hash) {
if (! hash )
hash = window.location.hash
hash = hash.replace('#', '')
info = hash.split('/');
data = {}
if (info.length >= 1) {
data.distribution = {}
data.distribution.name = info[0];
}
if (info.length >= 3){
data.package = {}
data.package.name = info[1];
data.package.version = info[2];
}
if (info.length >= 4) {
data.file = {}
data.file.name = info[3]
}
return data
},
from_data_to_hash: function (data) {
hash = "#"
if (Utils.check_data_distribution(data)) {
hash = hash + data.distribution.name
if (Utils.check_data_package(data)) {
hash = hash + '/' + data.package.name + "/" + data.package.version
if (Utils.check_data_file(data))
hash = hash + '/' + data.file.name
}
}
return hash
},
check_data_distribution: function(data) {
return data && data.distribution && data.distribution.name
},
check_data_package: function(data) {
return Utils.check_data_distribution(data) && data.package && data.package.name && data.package.version
},
check_data_file: function(data) {
return Utils.check_data_package(data) && data.file && data.file.name
},
}
...@@ -23,28 +23,30 @@ ...@@ -23,28 +23,30 @@
<ul></ul> <ul></ul>
</nav> </nav>
<section> <section id="files">
<nav id="files"> <h3 class="title">Files</h3>
<h3>Files</h2> <nav id="logs">
<ul class=""></ul> <ul class=""></ul>
</nav> </nav>
<nav id="archives"> <nav id="archives">
<h3>Archives</h2> <h4>Archives</h4>
<ul></ul> <ul></ul>
</nav> </nav>
<nav id="debs"> <nav id="debs">
<h3>Debs</h2> <h4>Debs</h4>
<ul></ul> <ul></ul>
</nav> </nav>
</section> </section>
</aside> </aside>
<section id="file" class="col-md-9"> <section class="col-md-9">
<header> <header>
<h1 class="title"></h1> <h1 id="title"></h1>
</header> </header>
<div id="file">
<pre></pre> <pre></pre>
</div>
</section> </section>
</article> </article>
<footer><%= footer %></footer> <footer><%= footer %></footer>
......
...@@ -9,7 +9,10 @@ ...@@ -9,7 +9,10 @@
<script src='//code.jquery.com/jquery-1.7.2.min.js'></script> <script src='//code.jquery.com/jquery-1.7.2.min.js'></script>
<script src='//localhost:3000/socket.io/socket.io.js'></script> <script src='//localhost:3000/socket.io/socket.io.js'></script>
<script src="/libs/bootstrap/3.1.1/js/bootstrap.min.js"></script> <script src="/libs/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src='/javascripts/debomatic-webui.js'></script>
<script src='/javascripts/utils.js'></script>
<script src='/javascripts/page.js'></script>
<script src='/javascripts/main.js'></script>
</head> </head>
<body> <body>
<%- body %> <%- body %>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment