var refreshIntervalId = null;
// Prefer same-origin relative paths so HTTPS doesn't break due to mixed-content / upgrade rules.
var imgurl = "img/";
function buildThumbLi(filename) {
var imgfile = imgurl + encodeURIComponent(filename);
return (
"
" +
" × " +
" " +
" " +
" " +
"
" +
" "
);
}
function removeFromImageList(filename) {
if (!window.DREAMGIRL_IMAGES || !window.DREAMGIRL_IMAGES.length) return;
var idx = window.DREAMGIRL_IMAGES.indexOf(filename);
if (idx >= 0) window.DREAMGIRL_IMAGES.splice(idx, 1);
}
function deleteImageFile(filename, onSuccess, onError) {
var fd = new FormData();
fd.append('filename', filename);
fetch('api/delete_image.php', {
method: 'POST',
body: fd,
credentials: 'same-origin'
})
.then(function (r) { return r.json(); })
.then(function (data) {
if (!data || !data.ok) {
throw new Error((data && data.error) ? data.error : '삭제 실패');
}
if (typeof onSuccess === 'function') onSuccess(data);
})
.catch(function (err) {
if (typeof onError === 'function') {
onError(err);
} else {
alert('에러: ' + (err && err.message ? err.message : '삭제 실패'));
}
});
}
function bindThumbDeleteHandler() {
$('#thumbs').off('click.thumbDelete', '.thumb-delete');
$('#thumbs').on('click.thumbDelete', '.thumb-delete', function (e) {
e.preventDefault();
e.stopPropagation();
var $li = $(this).closest('li');
var filename = $li.attr('data-filename');
if (!filename) return;
var $btn = $(this);
$btn.prop('disabled', true);
deleteImageFile(filename, function () {
removeFromImageList(filename);
if (window.dreamgirlGallery) {
var gallery = window.dreamgirlGallery;
var index = $li.index();
var wasCurrent = gallery.currentImage && gallery.currentImage.index === index;
if (typeof gallery.removeImageByIndex === 'function') {
gallery.removeImageByIndex(index);
} else {
$li.remove();
}
if (wasCurrent && gallery.data && gallery.data.length) {
var nextIndex = Math.min(index, gallery.data.length - 1);
if (typeof gallery.gotoIndex === 'function') {
gallery.gotoIndex(nextIndex);
}
}
} else {
$li.remove();
}
}, function (err) {
$btn.prop('disabled', false);
alert('에러: ' + (err && err.message ? err.message : '삭제 실패'));
});
});
}
function getApiUrl() {
// Use scheme-relative URL so it matches http/https of the current page.
// NOTE: if the API does not support https, consider proxying it via Apache and pointing to a same-origin endpoint.
return "//api.martn.ncue.net/dreamgirl.ncue";
}
function getRandomIndex(n) {
var arr = new Array();
var temp;
var rnum;
for(var i=0; i";
$("#thumbs-list").html(text);
initGallery();
bindThumbDeleteHandler();
}
function tryLoadFromLocalListApi() {
return $.ajax({
url: 'api/list_images.php',
type: 'GET',
dataType: 'json',
cache: false,
timeout: 8000
});
}
function fallbackToLocalManifest() {
if (window.DREAMGIRL_IMAGES && window.DREAMGIRL_IMAGES.length) {
buildThumbsFromList(window.DREAMGIRL_IMAGES);
return true;
}
// Minimal fallback if manifest is missing for some reason
buildThumbsFromList(['0.jpg','1.jpg','2.jpg','3.jpg','20.jpg','24.jpg','25.jpg','26.jpg','28.jpg','36.jpg','66.jpg']);
return true;
}
// Prefer same-origin dynamic list (so newly uploaded images appear on refresh).
tryLoadFromLocalListApi()
.done(function(resp) {
if (resp && resp.ok && resp.images && resp.images.length) {
buildThumbsFromList(resp.images);
} else {
fallbackToLocalManifest();
}
})
.fail(function() {
// If local list API is unavailable, try remote JSONP API, then fall back to static manifest.
$.ajax({
url : getApiUrl(),
type: 'GET',
data : "id=user",
dataType : "jsonp",
jsonp : "callback",
cache : false,
timeout: 15000,
success: function(data) {
try {
var filelist = data && data.filelist ? data.filelist : null;
if (!filelist || !filelist.length) return fallbackToLocalManifest();
var names = [];
for (var i=0; i