qiang-gao-cn / exercise

my repository for exercise
0 stars 0 forks source link

UI #1

Open qiang-gao-cn opened 5 years ago

qiang-gao-cn commented 5 years ago

all ui code

qiang-gao-cn commented 5 years ago

HTML:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="index.css">
    <script type="text/javascript" src="index.js"></script>
  </head>
  <body>
    <button class="div1" id="div1" onclick="div1()">load</button>
    <button class="divb" id="divb" onclick="save_json()">save</button>
    <div class="divval" id="divval">
      <table id="tabs">

      </table>
    </div>

  </body>
</html>

SCRIPT:

function load_json(url_of_json,div_to_be_display){
  var url = url_of_json;
  var xmlhttp;
  if(window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
  }else {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState==4&&xmlhttp.status==200){
      var data=xmlhttp.responseText;
      dataObj=JSON.parse(data);
      for (var objs in dataObj) {
        console.log(objs);
        console.log(dataObj[objs]);
        var trs = document.createElement("tr");
        document.getElementById("tabs").appendChild(trs);
        trs.innerHTML = "<tr><th>"+ objs +"</th>"+"<td>"+ dataObj[objs] +"</td></tr>";
      }
    }
  }
  xmlhttp.open("GET",url,true);
  xmlhttp.send();
}
function put_json(url_of_json, jobNew){
  var xmlhttp = new XMLHttpRequest();
  var obj = {};
  for(var key in jobNew) obj[key] = jobNew[key];
  xmlhttp.open("PUT",url_of_json, true);
  xmlhttp.setRequestHeader("Content-Type","text/plain;charset=UTF-8");
  xmlhttp.send(JSON.stringify(obj));
  xmlhttp.onreadystatechange = function(){
      if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
          console.log(xmlhttp.responseText);
      }
  }
}
function save_json(url_of_json, obj_to_save) {
  var url="b.JSON";
  var jobNew = {};
  var ths = document.getElementsByTagName("th");
  var tds = document.getElementsByTagName("td");
  var len = document.getElementsByTagName("th").length;
  for(var i=0;i<len;i++){
    jobNew[ths[i].innerText] = tds[i].innerText;
  }
  put_json(url, jobNew);
}
function div1(){
  var url_of_json = "a.json";
  load_json(url_of_json);
}
qiang-gao-cn commented 5 years ago
/*
* volo.js: Simple javascript to demonstrate volo api.
* Author: harry.ma@dao-lab.com
*/

if (!(document.body || document.getElementsByTagName("body")[0])) {
    document.body = document.createElement("body");
}

if (!(document.head || document.getElementsByTagName("head")[0])) {
    document.head = document.createElement("head");
}

if (!(document.title || document.getElementsByTagName("title")[0])) {
    document.title = "Volo Video Optimizer";
}

var css  = document.createElement('link');
css.rel  = "stylesheet";
css.type = "text/css";
css.href = "index.css";
css.media = 'all';
document.head.appendChild(css);

var volo = document.createElement("div");
volo.id = "Volo";
volo.className = "container-fluid";
document.body.appendChild(volo);

var heads = document.createElement("div");
heads.id = "heads";
volo.appendChild(heads);
var h3s = document.createElement("h3");
heads.appendChild(h3s);
h3s.innerHTML = "Volo Cube Job Summary";

for(var i=0;i<3;i++){
    var bodys = document.createElement("div");
    bodys.id = "bodys";
    bodys.className = "col-lg-4 col-md-12 col-sm-12 col-xs-12";
    volo.appendChild(bodys);

    var JobsList = document.createElement("table");
    JobsList.id = "JobsList"+i;
    JobsList.innerHTML = "<tr><th>Start/Stop</th><td><button id=\"start"+i+"\" name=\"start\" onclick=\"startchangenable(this)\">start</button><button  style=\"display:none;\" id=\"stop"+i+"\" name=\"stop\"  onclick=\"stopchangenable(this)\">stop</button></td></tr>"
        + "<tr><th>Job Name</th><td></td></tr>"
        + "<tr><th>Enable</th><td id=\"enable"+i+"\" onchange=\"isstart("+i+")\"></td></tr>"
        + "<tr><th>Last Update</th><td></td></tr>"
        + "<tr><th>Status</th><td></td></tr>"
        + "<tr><th>Speed</th><td></td></tr>"
        + "<tr><th>Bitrate</th><td></td></tr>"
        + "<tr><th>Run Time</th><td></td></tr>"
        + "<tr><th>VideoSize</th><td></td></tr>"
    + "<tr><th>Server URL</th><td><input type=\"text\" id=\"edit"+i+"td1\" placeholder=\"SERVER URL\" value=\"\" disabled /></td></tr>"
        + "<tr><th>Server Key</th><td><input type=\"text\" id=\"edit"+i+"td3\" placeholder=\"SERVER KEY\" value=\"\" disabled /></td></tr>"
        + "<tr><th>Edit/Save</th><td><button id=\"edit"+i+"\" onclick=\"edit(this)\">edit</button><button style=\"display:none;\" name=\"save\" id=\"save"+i+"\" onclick=\"save(this)\">save</button></td></tr>"
    //+ "<tr><th>Video</th><td><iframe src=\"https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2Fam730hk%2Fvideos%2F773464702989294%2F&width=426&show_text=false&height=240&appId\" width=\"426\" height=\"240\" style=\"border:none;overflow:hidden\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" allow=\"encrypted-media\" allowFullScreen=\"true\"></iframe></td></tr>"
    + "<tr><th>Load</th><td><input type=\"text\" value=\"\" placeholder=\"Iframe URL\" ><button id=\"load"+i+"\" onclick=\"refrushs(this)\">load</button></td></tr>"
    bodys.appendChild(JobsList); 
  }

var len = document.getElementsByTagName("table").length;
for(var i =0;i< len;i++){
    loadJson("newenc"+i+".json", update,i);
  var timer=setInterval(function(){ loadJson("newenc"+i+".json", update,i); }, 3000);   
}

function refrushs(self){
  var num = self.id.split("load");
  isstart(num[1]);
  loadJson("newenc"+num[1]+".json", update,num[1]);

}

var configElem = document.createElement("div");
configElem.setAttribute("id", "config");
//configElem.style.visibility = "hidden";
volo.appendChild(configElem);
function loadJson(filename, callback=null,count=null) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            try {
                var obj = JSON.parse(this.responseText);
                if (obj && callback) {
                    callback(obj,count,filename);
                }
            } catch(e) {}
        }
    };
    xhttp.open("GET",filename, true);
    xhttp.send();
}
function showJobStatus(obj,i) {
    document.getElementById("JobsList"+i).rows[3].cells[1].textContent = obj.report.out_time;
    document.getElementById("JobsList"+i).rows[4].cells[1].textContent = obj.state;
    document.getElementById("JobsList"+i).rows[5].cells[1].textContent = obj.report.speed;
    document.getElementById("JobsList"+i).rows[6].cells[1].textContent = obj.report.bitrate;
    document.getElementById("JobsList"+i).rows[7].cells[1].textContent = obj.report.out_time;
    document.getElementById("JobsList"+i).rows[8].cells[1].textContent = obj.info.input.stream[0].width + "*" + obj.info.input.stream[0].height;
}

function update(obj,i) {
    document.getElementById("JobsList"+i).rows[1].cells[1].textContent = obj.service;
    document.getElementById("JobsList"+i).rows[2].cells[1].textContent = obj.enable;
    var splitval = (obj.output).split("/",4);
    var tr1val = splitval[0]+"/"+splitval[1]+"/"+splitval[2]+"/"+splitval[3];
    var tr3val = (obj.output).split(tr1val+"/")[1];
    document.getElementById("edit"+i+"td1").value=tr1val;
    document.getElementById("edit"+i+"td3").value=tr3val;
    loadJson("newstate"+i+".json",showJobStatus,i);
}

function saveJob(jobOld, jobNew, filename) {
    var result = {};
    for(var key in jobOld) result[key] = jobOld[key];
    for(var key in jobNew) result[key] = jobNew[key];
    var xhttp = new XMLHttpRequest();
    xhttp.open("PUT", filename, true);
    xhttp.send(JSON.stringify(result));
}

function startchangenable(self){
  self.style.display="none";
  var num = self.id.split("start");
  document.getElementById("stop"+num[1]).style.display="block";
      var timestamp=new Date().getTime();
  var jobNew = {"enable": "1","version":timestamp};
  loadJson("newenc"+num[1]+".json", saveJob, jobNew);
}
function stopchangenable(self){
  self.style.display="none";
  var num = self.id.split("stop");
  document.getElementById("start"+num[1]).style.display="block";
      var timestamp=new Date().getTime();
  var jobNew = {"enable": "0","version":timestamp};
  loadJson("newenc"+num[1]+".json", saveJob, jobNew);
}

function edit(self){
    self.style.display = "none";
    var num = self.id.split("edit");
    document.getElementById("save"+num[1]).style.display = "block";
    document.getElementById("edit"+num[1]+"td1").disabled = false;
    document.getElementById("edit"+num[1]+"td3").disabled = false;
    clearInterval(timer);

}

function save(self){
  self.style.display = "none";
  var num = self.id.split("save");
  document.getElementById("edit"+num[1]).style.display = "block";
  document.getElementById("edit"+num[1]+"td1").disabled = true;
  document.getElementById("edit"+num[1]+"td3").disabled = true;
  var tr1val = document.getElementById("edit"+num[1]+"td1").value;
  var tr3val = document.getElementById("edit"+num[1]+"td3").value;
  var jobNew = {"output": tr1val+"/"+tr3val};
  loadJson("newenc"+num[1]+".json", saveJob, jobNew);
}

function isstart(i){
    var enval = document.getElementById("enable"+i).innerHTML;
    if(enval == "1"){
        document.getElementById("start"+i).style.display = "none";
    document.getElementById("stop"+i).style.display = "block";
    }
    if(enval == "0"){
      document.getElementById("stop"+i).style.display = "none";
      document.getElementById("start"+i).style.display = "block";
   }
}