thedesk/app/js/ui/img.js

329 lines
9.5 KiB
JavaScript
Raw Permalink Normal View History

2018-01-28 23:22:43 +11:00
/*イメージビューワー*/
//postのimg.jsとは異なります。
2018-02-14 03:19:44 +11:00
function imgv(id, key, acct_id) {
2019-11-09 00:52:54 +11:00
$('#imgprog').text(0)
2019-11-09 01:46:12 +11:00
$('#imgsec').text(0)
2019-11-09 00:52:54 +11:00
$('#imgmodal').hide()
2021-01-23 21:17:55 +11:00
rotate(true)
2019-11-09 00:52:54 +11:00
$('#imgmodal').attr('src', '../../img/loading.svg')
var murl = $('#' + id + '-image-' + key).attr('data-url')
2019-11-09 01:46:12 +11:00
var ourl = $('#' + id + '-image-' + key).attr('data-original')
2019-11-09 02:09:39 +11:00
if (!ourl || ourl == 'null') {
ourl = murl
}
2019-11-09 00:52:54 +11:00
var type = $('#' + id + '-image-' + key).attr('data-type')
$('#imagemodal').attr('data-id', id)
$('#imagemodal').attr('data-acct', acct_id)
2019-11-09 01:46:12 +11:00
$('#imagemodal').attr('data-original', ourl)
$('#imagemodal').attr('data-image', murl)
//表示はリモートを使うか(どちらにしろコピーはオリジナル)
var remote_img = localStorage.getItem('remote_img')
if (remote_img == 'yes') {
murl = ourl
}
2019-11-09 00:52:54 +11:00
$(document).ready(function() {
if (type == 'image') {
$('#imagemodal').modal('open')
2019-11-09 01:46:12 +11:00
imageXhr(id, key, murl)
2019-11-09 00:52:54 +11:00
$('#imagewrap').dragScroll() // ドラッグスクロール設定
$('#imgmodal').show()
$('#imagemodal').attr('data-key', key)
$('#imagemodal').attr('data-id', id)
} else if (type == 'video' || type == 'gifv') {
$('#video').attr('src', murl)
$('#videomodal').modal('open')
$('#imgmodal').show()
2018-01-28 23:22:43 +11:00
}
2019-11-09 00:52:54 +11:00
})
2018-01-28 23:22:43 +11:00
}
//イメージビューワーの送り
function imgCont(type) {
2019-11-09 00:52:54 +11:00
var key = $('#imagemodal').attr('data-key')
var id = $('#imagemodal').attr('data-id')
if (type == 'next') {
key++
} else if (type == 'prev') {
key = key * 1 - 1
2018-01-28 23:22:43 +11:00
}
2019-11-09 00:52:54 +11:00
var murl = $('#' + id + '-image-' + key).attr('data-url')
2019-11-09 01:46:12 +11:00
var ourl = $('#' + id + '-image-' + key).attr('data-original')
var type = $('#' + id + '-image-' + key).attr('data-type')
$('#imagemodal').attr('data-id', id)
$('#imagemodal').attr('data-acct', acct_id)
$('#imagemodal').attr('data-original', ourl)
$('#imagemodal').attr('data-image', murl)
//表示はリモートを使うか(どちらにしろコピーはオリジナル)
var remote_img = localStorage.getItem('remote_img')
2019-12-08 02:51:49 +11:00
if (remote_img == 'yes' && ourl != 'null') {
2019-11-09 01:46:12 +11:00
murl = ourl
}
2019-05-19 17:39:30 +10:00
if (murl) {
2019-11-09 16:40:46 +11:00
$('#imgprog').text(0)
$('#imgsec').text(0)
2019-11-09 00:52:54 +11:00
$('#imgmodal').attr('src', '../../img/loading.svg')
var type = $('#' + id + '-image-' + key).attr('data-type')
$(document).ready(function() {
if (type == 'image') {
2019-11-09 01:46:12 +11:00
imageXhr(id, key, murl)
2019-11-09 00:52:54 +11:00
$('#imagewrap').dragScroll() // ドラッグスクロール設定
$('#imagemodal').attr('data-key', key)
$('#imagemodal').attr('data-id', id)
} else if (type == 'video' || type == 'gifv') {
$('#video').attr('src', murl)
$('#videomodal').modal('open')
2019-05-19 17:39:30 +10:00
}
2019-11-09 01:46:12 +11:00
})
}
}
function imageXhr(id, key, murl) {
2019-11-09 02:09:39 +11:00
var startTime = new Date()
2019-11-09 01:46:12 +11:00
xhr = new XMLHttpRequest()
xhr.open('GET', murl, true)
xhr.responseType = 'arraybuffer'
xhr.addEventListener(
'progress',
function(event) {
if (event.lengthComputable) {
var total = event.total
var now = event.loaded
var per = (now / total) * 100
$('#imgprog').text(Math.floor(per))
}
},
false
)
xhr.addEventListener(
'loadend',
function(event) {
var total = event.total
2019-12-12 02:17:22 +11:00
$('#imgbyte').text(Math.floor(total / 1024))
2019-11-09 01:46:12 +11:00
var now = event.loaded
var per = (now / total) * 100
$('#imgprog').text(Math.floor(per))
},
false
)
2020-02-08 03:10:45 +11:00
xhr.addEventListener(
'error',
function(event) {
$('#imgmodal').attr('src', murl)
},
false
)
2019-11-09 01:46:12 +11:00
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
r = new FileReader()
r.readAsDataURL(this.response)
r.onload = function() {
var b64 = r.result
var element = new Image()
var width
element.onload = function() {
var width = element.naturalWidth
var height = element.naturalHeight
2021-01-23 21:17:55 +11:00
calcNiceAspect(width, height)
$('#imagemodal').attr('data-naturalWidth', width)
$('#imagemodal').attr('data-naturalHeight', height)
2018-02-17 00:08:43 +11:00
}
2019-11-09 01:46:12 +11:00
if ($('#' + id + '-image-' + (key * 1 + 1)).length == 0) {
$('#image-next').prop('disabled', true)
} else {
$('#image-next').prop('disabled', false)
}
if ($('#' + id + '-image-' + (key * 1 - 1)).length == 0) {
$('#image-prev').prop('disabled', true)
} else {
$('#image-prev').prop('disabled', false)
}
element.src = b64
2019-11-09 02:09:39 +11:00
var endTime = new Date()
2019-11-09 01:46:12 +11:00
var proctime = endTime.getTime() - startTime.getTime()
$('#imgsec').text(proctime)
$('#imgmodal').attr('src', b64)
2018-02-14 03:19:44 +11:00
}
2019-11-09 01:46:12 +11:00
}
2018-01-29 01:01:18 +11:00
}
2019-11-09 01:46:12 +11:00
xhr.responseType = 'blob'
xhr.send()
2018-01-28 23:22:43 +11:00
}
2021-01-23 21:17:55 +11:00
function calcNiceAspect( width, height ) {
if(width < 650) {
width = 650
}
var windowH = $(window).height()
var windowW = $(window).width()
$('#imagemodal').css('bottom', '0')
$('#imagemodal img').css('width', 'auto')
if (height < windowH) {
$('#imagemodal').css('height', height + 100 + 'px')
$('#imagemodal img').css('height', height + 'px')
if (width > windowW * 0.8) {
$('#imagemodal').css('width', '80vw')
$('#imagemodal img').css('width', 'auto')
var heightS = ((windowW * 0.8) / width) * height
$('#imagemodal').css('height', heightS + 100 + 'px')
} else {
$('#imagemodal').css('width', width + 'px')
}
} else {
$('#imagemodal img').css('width', 'auto')
var widthS = (windowH / height) * width
if (widthS < windowW) {
$('#imagemodal').css('width', widthS + 'px')
} else {
$('#imagemodal').css('width', '100vw')
}
$('#imagemodal').css('height', '100vh')
$('#imagemodal img').css('height', 'calc(100vh - 60px)')
}
}
2018-01-28 23:22:43 +11:00
//ズームボタン(z:倍率)
function zoom(z) {
2019-11-09 00:52:54 +11:00
var wdth = $('#imagewrap img').width()
var wdth = wdth * z
$('#imagewrap img').css('width', wdth + 'px')
var hgt = $('#imagewrap img').height()
var hgt = hgt * z
$('#imagewrap img').css('height', hgt + 'px')
2018-01-28 23:22:43 +11:00
}
//スマホ対応ドラッグ移動システム
2020-08-02 17:08:07 +10:00
(function() {
2019-11-09 00:52:54 +11:00
$.fn.dragScroll = function() {
var target = this
$(this)
.mousedown(function(event) {
$(this)
.data('down', true)
.data('x', event.clientX)
.data('y', event.clientY)
.data('scrollLeft', this.scrollLeft)
.data('scrollTop', this.scrollTop)
return false
})
.css({
overflow: 'hidden', // スクロールバー非表示
cursor: 'move'
})
2018-01-28 23:22:43 +11:00
// ウィンドウから外れてもイベント実行
2019-11-09 00:52:54 +11:00
$(document)
.mousemove(function(event) {
if ($(target).data('down') == true) {
// スクロール
target.scrollLeft($(target).data('scrollLeft') + $(target).data('x') - event.clientX)
target.scrollTop($(target).data('scrollTop') + $(target).data('y') - event.clientY)
return false // 文字列選択を抑止
}
})
.mouseup(function(event) {
$(target).data('down', false)
})
$(this)
.on('touchstart', function(event) {
$(this)
.data('down', true)
.data('x', getX(event))
.data('y', getY(event))
.data('scrollLeft', this.scrollLeft)
.data('scrollTop', this.scrollTop)
return false
})
.css({
overflow: 'hidden', // スクロールバー非表示
cursor: 'move'
}) //指が触れたか検知
$(this).on('touchmove', function(event) {
2019-03-08 05:19:26 +11:00
if ($(target).data('down') === true) {
2018-01-28 23:22:43 +11:00
// スクロール
2019-11-09 00:52:54 +11:00
target.scrollLeft($(target).data('scrollLeft') + $(target).data('x') - getX(event))
target.scrollTop($(target).data('scrollTop') + $(target).data('y') - getY(event))
return false // 文字列選択を抑止
} else {
}
}) //指が動いたか検知
$(this).on('touchend', function(event) {
$(target).data('down', false)
})
2018-01-28 23:22:43 +11:00
2019-11-09 00:52:54 +11:00
return this
2018-01-28 23:22:43 +11:00
}
2019-11-09 00:52:54 +11:00
})(jQuery)
2018-01-28 23:22:43 +11:00
function getX(event) {
2019-11-09 00:52:54 +11:00
return event.originalEvent.touches[0].pageX
2018-01-28 23:22:43 +11:00
}
function getY(event) {
2019-11-09 00:52:54 +11:00
return event.originalEvent.touches[0].pageY
2018-01-28 23:22:43 +11:00
}
//マウスホイールで拡大
2019-11-09 00:52:54 +11:00
var element = document.getElementById('imagemodal')
element.onmousewheel = function(e) {
var delta = e.wheelDelta
2018-01-28 23:22:43 +11:00
if (delta > 0) {
zoom(1.1)
} else {
zoom(0.9)
}
}
2021-01-23 21:17:55 +11:00
function rotate(reset) {
if (reset) {
$('#imagewrap img').removeClass('rotate-90')
$('#imagewrap img').removeClass('rotate-180')
$('#imagewrap img').removeClass('rotate-270')
$('#imagemodal').attr('data-naturalWidth', null)
$('#imagemodal').attr('data-naturalWidth', null)
return true
}
var width = $('#imagemodal').attr('data-naturalWidth')
var height = $('#imagemodal').attr('data-naturalHeight')
if ($('#imagewrap img').hasClass('rotate-90')) {
$('#imagewrap img').removeClass('rotate-90')
$('#imagewrap img').addClass('rotate-180')
calcNiceAspect(width, height)
} else if ($('#imagewrap img').hasClass('rotate-180')) {
$('#imagewrap img').removeClass('rotate-180')
$('#imagewrap img').addClass('rotate-270')
calcNiceAspect(height, width)
} else if ($('#imagewrap img').hasClass('rotate-270')) {
$('#imagewrap img').removeClass('rotate-270')
calcNiceAspect(width, height)
} else {
$('#imagewrap img').addClass('rotate-90')
calcNiceAspect(height, width)
}
}
2018-03-31 13:39:06 +11:00
2018-02-18 16:43:11 +11:00
//当該トゥート
2019-05-19 17:39:30 +10:00
function detFromImg() {
2019-11-09 00:52:54 +11:00
var id = $('#imagemodal').attr('data-id')
var acct_id = $('#imagemodal').attr('data-acct')
$('#imagemodal').modal('close')
details(id, acct_id)
2018-02-17 00:08:43 +11:00
}
2018-02-18 16:43:11 +11:00
//画像保存
2019-05-19 17:39:30 +10:00
function dlImg() {
2019-11-09 01:46:12 +11:00
var ourl = $('#imagemodal').attr('data-original')
var murl = $('#imagemodal').attr('data-image')
var remote_img = localStorage.getItem('remote_img')
if (remote_img == 'yes') {
murl = ourl
}
2019-12-12 02:17:22 +11:00
var save = localStorage.getItem('savefolder')
if (!save || save == 'null') {
2019-11-09 00:52:54 +11:00
var save = ''
2018-09-06 02:47:27 +10:00
}
2019-11-09 01:46:12 +11:00
postMessage(['generalDL', [murl, save, false]], '*')
2019-04-03 14:59:29 +11:00
}
2019-05-19 17:39:30 +10:00
function openFinder(dir) {
2019-11-09 00:52:54 +11:00
postMessage(['openFinder', dir], '*')
2019-10-20 21:09:52 +11:00
}
2019-11-09 00:52:54 +11:00
function stopVideo() {
2019-10-20 21:09:52 +11:00
document.getElementById('video').pause()
2019-11-09 00:52:54 +11:00
}
2019-11-09 01:46:12 +11:00
function copyImgUrl() {
var murl = $('#imagemodal').attr('data-original')
execCopy(murl)
M.toast({ html: lang.lang_img_copyDone, displayLength: 1500 })
}