var numAll = ''
var numPhoto = ''
var token = ''
var userData
var dataInstrument = []
var start = 0
var nextUrl = ''
htmlText = ''
var dataUri = [];
var processingNext = false;
var clickOnHideButton = false;
var xhr = null;
var tagsTmpl = _.template($("script#tag-list").html());
var hideItem = function(itemId, divBox) {
var html = divBox.html();
divBox.html('')
data = {'hide': true};
token = sessionStorage.getItem('token')
$.ajax({
type: "PATCH",
url: 'http://www.serv.stdb.most.go.th/api/v2/instrument/'+ itemId +'/',
data: JSON.stringify(data),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
},
success: function(respone) {
divBox.html('ลบเรียบร้อยแล้ว ...').delay(2000).fadeOut(500);
},
fail: function() {
divBox.html('มีปัญหาขัดข้อง ...').delay(1000).html(html);
},
dataType: 'json'
});
}
var reloadCardListener = function() {
$('.hideBtn').click(function(evt) {
clickOnHideButton = true;
setTimeout(function() {
clickOnHideButton = false;
}, 1000);
evt.stopPropagation();
var that = $(this);
var itemId = that.data('id');
var $parent = that.closest('.cardStyle');
swal(
{
title: "ลบ?",
text: "ไม่ใช่ครุภัณฑ์วิทยาศาสตร์",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "ลบเลย!"
}, function(yes) {
if (yes)
hideItem(itemId, $parent)
}
);
});
}
var reloadItem = function() {
if (xhr)
xhr.abort();
// reset stuff
start = 0
$('#modalAll').html('');
$('#card').html('');
htmlText = '';
modalText = '';
dataInstrument = [];
/// start doing new onw
var orgId = sessionStorage.getItem('orgId');
if (!orgId) {
alert('something is wrong, try reloading');
}
var token = sessionStorage.getItem('token');
var tagFilter = '';
if (localStorage.getItem('selFilters')) {
tagFilter = localStorage.getItem('selFilters').split(',');
tagFilter = '&tags=' + tagFilter;
}
var isFilter = '';
if (localStorage.getItem('selIsFilters')) {
isFilter = localStorage.getItem('selIsFilters').split(',');
isFilter = '&is=' + isFilter;
}
var url = 'http://www.serv.stdb.most.go.th/api/v2/instrument/?organization='+orgId+tagFilter+isFilter;
xhr = $.ajax({
type: "GET",
url: url,
headers :{
Authorization: 'Bearer '+ token,
Accept: 'application/json, text/javascript',
'Accept-Language' : 'en-US,en;q=0.8,th;q=0.6',
},
success: getData,
dataType: 'json',
});
}
var reloadFilterListener = function() {
var selectedFilter = [];
if (localStorage.getItem('selFilters')) {
selectedFilter = localStorage.getItem('selFilters').split(',');
}
$('input[name="tags"]').change(
function() {
var n = $(this).val();
if ($(this).is(':checked')) {
selectedFilter = [n];
localStorage.setItem('selFilters', selectedFilter.join(','));
$('input[name="tags"]').each(function() {
if ($(this).val() != n)
$(this).prop('checked', false);
});
} else {
selectedFilter = [];
localStorage.removeItem('selFilters');
$('input[name="tags"]').prop('checked', false);
}
reloadItem();
});
$('input[name="is"]').change(
function() {
var n = $(this).val();
if ($(this).is(':checked')) {
selectedIsFilter = [n];
localStorage.setItem('selIsFilters', selectedIsFilter.join(','));
$('input[name="is"]').each(function() {
if ($(this).val() != n)
$(this).prop('checked', false);
});
} else {
selectedIsFilter = [];
localStorage.removeItem('selIsFilters');
$('input[name="is"]').prop('checked', false);
}
reloadItem();
});
}
var getOrgTags = function(orgId) {
var url = `//www.serv.stdb.most.go.th/api/v2/organization/${orgId}/tags/`;
var promise = $.get(url);
var $box = $('#any-filter');
promise.fail(function() {
$box.html('');
});
promise.done(function(resp) {
var selectedFilter = [];
if (localStorage.getItem('selFilters')) {
selectedFilter = localStorage.getItem('selFilters').split(',');
}
$box.html(tagsTmpl({
items: resp,
selected: selectedFilter
}));
reloadFilterListener();
});
}
function getStartPage() {
sesToken = sessionStorage.getItem('token');
if( sesToken === null) {
token = localStorage.getItem("token")
if( token != null ) {
$.ajax({
type: "POST",
url: 'http://www.serv.stdb.most.go.th/api-token-verify/',
data: { "token" : token },
header: {
Authorization: 'Bearer '+ token,
},
success: function(respone) {
sessionStorage.setItem("token",respone.token)
getStat();
},
dataType: 'json'
});
}
else{
window.location.href = 'login.html'
}
}
else{
getStat();
}
}
function LogoutPage() {
localStorage.removeItem('token');
sessionStorage.removeItem('token');
window.location.href = 'index.html'
}
function getStat() {
token = sessionStorage.getItem("token")
// console.log(token)
// console.log('======== 2 ======')
dataUser = jwt_decode(token)
userId = dataUser.user_id
if( token != null ) {
$.ajax({
type: "GET",
url: 'http://www.serv.stdb.most.go.th/api/v2/instrument/stats/',
headers :{
Authorization: 'Bearer '+ token,
},
success: getDataStat,
dataType: 'json',
});
}
}
function getUser(status) {
token = sessionStorage.getItem("token");
dataUser = jwt_decode(token);
userId = dataUser.user_id.toString();
$.ajax({
type: "GET",
url: 'http://www.serv.stdb.most.go.th/api/v2/user/'+userId+'/',
headers :{
Authorization: 'Bearer '+ token,
},
success: function(respone) {
userData = respone
organ = '
'+respone.organizations[0].name+'
'
document.getElementById("nameOrgan").innerHTML = organ
token = sessionStorage.getItem("token")
var searchText = ''
if (status === 'search') {
// console.log("search")
dataInstrument = []
searchText = '&search='+document.getElementById("searchId").value
start = 0
$('#modalAll').html('');
$('#card').html('');
htmlText = '';
modalText = '';
dataInstrument = [];
}
var tagFilter = '';
if (localStorage.getItem('selFilters')) {
tagFilter = localStorage.getItem('selFilters').split(',');
tagFilter = '&tags=' + tagFilter;
}
var isFilter = '';
if (localStorage.getItem('selIsFilters')) {
isFilter = localStorage.getItem('selIsFilters').split(',');
isFilter = '&is=' + isFilter;
}
sessionStorage.setItem('orgId', respone.organizations[0].id);
var url = 'http://www.serv.stdb.most.go.th/api/v2/instrument/?organization='+respone.organizations[0].id+searchText+tagFilter+isFilter;
getOrgTags(respone.organizations[0].id);
xhr = $.ajax({
type: "GET",
url: url,
headers :{
Authorization: 'Bearer '+ token,
Accept: 'application/json, text/javascript',
'Accept-Language' : 'en-US,en;q=0.8,th;q=0.6',
},
success: getData,
dataType: 'json',
});
},
dataType: 'json',
});
}
function getDataStat(respone) {
getUser('first');
document.getElementById("like").innerHTML = respone.items.completed
document.getElementById("status").innerHTML = '+'+respone.items.recent_update
document.getElementById("accountActive").innerHTML = respone.users.active
document.getElementById("numAll").innerHTML = respone.items.all
document.getElementById("numPhoto").innerHTML = respone.items.photos
}
function getData(respone) {
dataInstrument = dataInstrument.concat(respone.results)
var htmlText = ''
var modalText = ''
for (i=start;i'
+''
}
var tagsHtml = '';
for (var k=0; k${item.tags[k]}- `;
}
tagsHtml += '
';
htmlText = htmlText+""
// Author :: Apirak
+ '
'
+ '
'
+ imageInstrument
// end of left
+ '
'
+ '
'
+ dataInstrument[i].name+'
'
+ '#'+dataInstrument[i].custom_id+'
'
+ '@'+dataInstrument[i].caretaker+'
'
+ tagsHtml
+''// end of right
+ '
'
// End
+'
';
var backup = dataInstrument[i].clips;
dataInstrument[i].clips = addNullData(dataInstrument[i].clips);
if(dataInstrument[i].clips.length === 0) {
imageTag = ''
+'
'
+'
'
+'
'
+'
ไม่มีรูปภาพ
'
+'
'
+'
'
+'
'
+'
ไม่มีรูปภาพ
'
+'
'
+'
ไม่มีรูปภาพ
'
+'
'
+'
ไม่มีรูปภาพ
'
+'
'
+'
'
+'
'
+''
}
else{
var style4 = ''
if(dataInstrument[i].clips.length >= 2) {
var tagMiniImage = ''
var loopImage = dataInstrument[i].clips.length
if (loopImage > 4) {
loopImage = 4
}
for(j=1;j'
+'ไม่มีรูปภาพ
';
}
for(j=loopImage;j<4;j++) {
if (j==3) {
style4 = 'margin-top:5px'
}
tagMiniImage = tagMiniImage
+''
+'ไม่มีรูปภาพ
';
}
}
else{
var tagMiniImage = ''
for(j=1;j<4;j++) {
if (j==3) {
style4 = 'margin-top:5px'
}
tagMiniImage = tagMiniImage
+''
+'ไม่มีรูปภาพ
';
}
}
var imgUrl = '';
if (dataInstrument[i].clips[0].photo) {
imgUrl = dataInstrument[i].clips[0]['thumbnail']['sm'];
}
imageTag = ''
+'
'
+'
'
+'
'
+'
ไม่มีรูปภาพ
'
+'
'
+'
'
+tagMiniImage
+'
'
+'
'
+'
'
+''
}
var youtube_tag = getYoutubeTag(dataInstrument[i].model,i);
modalText = modalText+''
+'
'
+imageTag
+youtube_tag
+'
'
+'
ชื่อครุภัณฑ์วิทยาศาสตร์
'
+'
'
+'
'
+'
'
+'
จุดประสงค์และลักษณะการใช้งาน
'
+'
'
+'
'
+'
'
+'
'
+'
'
+'
'
+'
'
+'
'
+'
วีดีโอ(youtube)
'
+'
'
+'
'
+'
'
+''
+''
+''
+'
'
+'
'
+'
';
dataInstrument[i].clips = backup;
}
if(respone.next != null) {
nextUrl = ''
moreText = ''
moreText = ""
+"
More
"
+"";
nextUrl = respone.next
document.getElementById("more").innerHTML = moreText;
}
else{
nextUrl = ''
moreText = ''
document.getElementById("more").innerHTML = moreText;
}
processingNext = false;
start = dataInstrument.length
document.getElementById("modalAll").innerHTML = document.getElementById("modalAll").innerHTML+modalText
document.getElementById("card").innerHTML = document.getElementById("card").innerHTML+htmlText
reloadCardListener();
/* Apirak */
$("img[id*='myImg']").each(function() {
var id = $(this).attr('id');
var order = id.substring(5,6);
var idInstrument = id.substring(6,7);
if(idInstrument == 'a') {idInstrument='add';}
$("Edit").insertAfter( $(this) );
$(this).parent().css({"position": "relative"});
$(this).css({"margin-top": "0px"});
});
$("iframe").each(function() {
var index = $(this).attr('id');
index = index.replace("youtube-","");
$("img#myImg0"+index).addClass("img-with-youtube");
});
}
function uploadWithOrder(order,idInstrument) {
$("#modal"+idInstrument+" input[name='upload']").attr('order',order);
$("#modal"+idInstrument+" input[name='upload']").click();
}
function getYoutubeTag(link,index) {
//https://www.youtube.com/watch?v=hYgTVD-bxOY
//http://www.youtube.com/v/hYgTVD-bxOY
link = link.replace("https","http");
link = link.replace("watch?v=","v/");
var youtube_tag = '';
if( youtube_tag.indexOf("http") < 0 ) {return youtube_tag;}
if(link != '')
{
youtube_tag = '';
$("iframe").css("margin-left","13%");
$("iframe").css("margin-top","-13%");
}
else{
youtube_tag="";
}
return youtube_tag;
}
function getNextUrl(x) {
if (processingNext) {
return;
}
processingNext = true;
$('#moreText').html('');
token = sessionStorage.getItem('token')
$.ajax({
type: "GET",
url: nextUrl,
headers :{
Authorization: 'Bearer '+ token,
Accept: 'application/json, text/javascript',
'Accept-Language' : 'en-US,en;q=0.8,th;q=0.6',
},
success: getData,
dataType: 'json',
});
}
function addNullData(arr) {
var temp_arr = [{content_type:'',id:'',object_id:'',order:'',photo:"",user:''},
{content_type:'',id:'',object_id:'',order:'',photo:"",user:''},
{content_type:'',id:'',object_id:'',order:'',photo:"",user:''},
{content_type:'',id:'',object_id:'',order:'',photo:"",user:''}];
$.each(arr,function(index,obj) {
temp_arr[obj.order] = obj;
});
return temp_arr;
}
function getCoverImg(arr) {
var link="";
$.each(arr,function(index,obj) {
if(obj.order == 0)
{
link=obj.thumbnail.sm;
}
});
return link;
}
function modalItem(idCard) {
if (clickOnHideButton) {
return;
}
var modal = document.getElementById('modal'+idCard);
var btn = document.getElementById(idCard);
modal.style.display = "block";
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
if(idCard != 'search') {
removeImage(idCard)
}
dataUri = []
}
}
}
function uploadImage(idImage) {
var check = 0
var order = $("#modal"+idImage+" input[name='upload']").attr('order');
$(":file").change(function () {
if (this.files && this.files[0] && check===0) {
var reader = new FileReader();
// console.log(reader)
reader.onload = function(e) {
instrument = dataInstrument[parseInt(idImage)]
// console.log(instrument)
// console.log('-------------xxxx-------------')
var uriAdd = [{'uri':e.target.result,'order':parseInt(order)}];
dataUri = dataUri.concat(uriAdd);
if(idImage == 'add') {
loopImage = 0
}
else{
var loopImage = instrument.clips.length
for(idImg=0;idImg