/* --------------------------------------------------------------------
Chevereto
http://chevereto.com/
@author Rodolfo Berrios A.
Copyright (C) Rodolfo Berrios A. All rights reserved.
BY USING THIS SOFTWARE YOU DECLARE TO ACCEPT THE CHEVERETO EULA
http://chevereto.com/license
--------------------------------------------------------------------- */
$(function () {
// Window listeners
$(window).on("resize", function () {
CHV.fn.uploader.boxSizer();
if (typeof user_background_full_fix == "function") {
user_background_full_fix();
}
CHV.fn.bindSelectableItems();
CHV.fn.listingViewer.placeholderSizing();
});
if (window.opener) {
$(window).on("load", function (e) {
window.opener.postMessage(
{
id: window.name,
requestAction: "postSettings",
},
"*"
);
});
$(window).on("message", function (e) {
var data = e.originalEvent.data;
if (
typeof data.id == typeof undefined ||
typeof data.settings == typeof undefined
) {
return;
}
if (window.name !== data.id) {
return;
}
CHV.obj.opener.uploadPlugin[data.id] = data.settings;
});
}
// Landing fancy load
if ($("#home-cover, #maintenance-wrapper, #login").exists()) {
var landing_src = $("#maintenance-wrapper").exists()
? $("#maintenance-wrapper")
.css("background-image")
.slice(4, -1)
.replace(/^\"|\"$/g, "")
: $(".home-cover-img", "#home-cover-slideshow").first().attr("data-src");
function showHomeCover() {
$("body").addClass("load");
if (!$("#maintenance-wrapper").exists()) {
$(".home-cover-img", "#home-cover-slideshow")
.first()
.css("background-image", "url(" + landing_src + ")")
.addClass("animate-in--alt")
.removeAttr("data-src");
}
setTimeout(function () {
setTimeout(function () {
$("body").addClass("loaded");
}, 400 * 3);
setTimeout(function () {
showHomeSlideshow();
}, 7000);
}, 400 * 1.5);
}
var showHomeSlideshowInterval = function () {
setTimeout(function () {
showHomeSlideshow();
}, 8000);
};
function showHomeSlideshow() {
var $image = $(
".home-cover-img[data-src]",
"#home-cover-slideshow"
).first();
var $images = $(".home-cover-img", "#home-cover-slideshow");
if ($image.length == 0) {
if ($images.length == 1) return;
$images.first().removeClass("animate-in");
$("#home-cover-slideshow").append($images.first());
setTimeout(function () {
$(".home-cover-img:last", "#home-cover-slideshow").addClass(
"animate-in"
);
}, 20);
setTimeout(function () {
$(".home-cover-img:not(:last)", "#home-cover-slideshow").removeClass(
"animate-in"
);
}, 4000);
showHomeSlideshowInterval();
} else {
var src = $image.attr("data-src");
$("")
.attr("src", src)
.on("load error", function () {
$(this).remove();
$image
.css("background-image", "url(" + src + ")")
.addClass("animate-in")
.removeAttr("data-src");
setTimeout(function () {
$(
".home-cover-img:not(:last)",
"#home-cover-slideshow"
).removeClass("animate-end animate-in--alt");
}, 2000);
showHomeSlideshowInterval();
});
}
}
if (landing_src) {
$("")
.attr("src", landing_src)
.on("load error", function () {
$(this).remove();
showHomeCover();
});
} else {
showHomeCover();
}
}
// Set the anywhere objects, just for shorter calling in $.
var anywhere_upload = CHV.fn.uploader.selectors.root,
anywhere_upload_queue = CHV.fn.uploader.selectors.queue,
$anywhere_upload = $(anywhere_upload),
$anywhere_upload_queue = $(anywhere_upload_queue);
$(document).on("click", "[data-action=top-bar-upload]", function (e) {
if ($("body").is("#upload") || $(this).data("link") == 1) return;
CHV.fn.uploader.toggle();
});
var ThemeTone = {
tones: {
light: {
html: "tone-light top-bar-white",
top: "white",
},
dark: {
html: "tone-dark top-bar-black",
top: "black",
},
},
isDark: function () {
return $("html").hasClass("tone-dark");
},
toggle: function () {
var toTone = this.isDark() ? "light" : "dark";
$("html")
.removeClass(this.tones.light.html + " " + this.tones.dark.html)
.addClass(this.tones[toTone].html);
if (!$("body#index").exists()) {
$("#top-bar")
.removeClass(this.tones.light.top + " " + this.tones.dark.top)
.addClass(this.tones[toTone].top);
}
},
save: function () {
$.ajax({
type: "POST",
data: {
action: "toggleTone",
},
cache: false,
});
ThemeTone.aux = ThemeTone.isDark();
},
timeout: {},
aux: {},
};
ThemeTone.aux = ThemeTone.isDark();
$(document).on("click", "[data-action=top-bar-tone]", function (e) {
if (e.isPropagationStopped()) {
return;
}
if ($("[data-action=top-bar-menu-full]").is(":visible")) {
PF.fn.topMenu.hide();
}
clearTimeout(ThemeTone.timeout);
ThemeTone.toggle();
ThemeTone.timeout = setTimeout(function () {
if (ThemeTone.aux !== ThemeTone.isDark()) {
ThemeTone.save();
}
}, 750);
});
// Close upload box
$("[data-action=close-upload]", $anywhere_upload).click(function () {
if ($anywhere_upload.is(":animated")) {
return;
}
$("[data-action=top-bar-upload]", "#top-bar").click();
});
// Reset upload box
$("[data-action=reset-upload]", $anywhere_upload).click(function () {
if (CHV.fn.uploader.isUploading) {
$(
"[data-action=cancel-upload-remaining], [data-action=cancel-upload]",
$anywhere_upload
).trigger("click");
}
CHV.fn.uploader.reset();
});
// Cancel remaining uploads
$(
"[data-action=cancel-upload-remaining], [data-action=cancel-upload]",
$anywhere_upload
).click(function () {
CHV.fn.uploader.isUploading = false;
$("[data-action=cancel]", $anywhere_upload_queue).click();
if (Object.size(CHV.fn.uploader.results.success) > 0) {
CHV.fn.uploader.displayResults();
return;
} else {
CHV.fn.uploader.reset();
}
});
// Toggle upload privacy
$(document).on(
"click",
"[data-action=upload-privacy]:not(disabled)",
function (e) {
if (e.isDefaultPrevented()) return;
current_privacy = $(this).data("privacy");
target_privacy = current_privacy == "public" ? "private" : "public";
this_lock = $(".icon", this).data("lock");
this_unlock = $(".icon", this).data("unlock");
$(".icon", this)
.removeClass(this_lock + " " + this_unlock)
.addClass(current_privacy == "public" ? this_lock : this_unlock);
$(this).data("privacy", target_privacy);
$("[data-action=upload-privacy-copy]").html(
$("[data-action=upload-privacy]").html()
);
$upload_button = $("[data-action=upload]", $anywhere_upload);
$upload_button.text($upload_button.data(target_privacy));
$(this).tipTip("hide");
}
);
// Do the thing when the fileupload changes
$(CHV.fn.uploader.selectors.file + ", " + CHV.fn.uploader.selectors.camera)
.on("change", function (e) {
if (!$(CHV.fn.uploader.selectors.root).data("shown")) {
CHV.fn.uploader.toggle(
{
callback: function (e) {
CHV.fn.uploader.add(e);
},
},
e
);
} else {
CHV.fn.uploader.add(e);
}
})
.on("click", function (e) {
if ($(this).data("login-needed") && !PF.fn.is_user_logged()) {
return;
}
});
function isFileTransfer(e) {
var e = e.originalEvent,
isFileTransfer = false;
if (e.dataTransfer.types) {
for (var i = 0; i < e.dataTransfer.types.length; i++) {
if (e.dataTransfer.types[i] == "Files") {
isFileTransfer = true;
break;
}
}
}
return isFileTransfer;
}
// Enable uploader events
if ($(CHV.fn.uploader.selectors.root).exists()) {
$("body").on({
dragenter: function (e) {
e.preventDefault();
if (!isFileTransfer(e)) {
return false;
}
if (!$(CHV.fn.uploader.selectors.dropzone).exists()) {
$("body").append(
$(
''
).css({
width: "100%",
height: "100%",
position: "fixed",
/* opacity: 0.5, background: "red",*/
zIndex: 1000,
left: 0,
top: 0,
})
);
}
},
});
$(document).on(
{
dragover: function (e) {
e.preventDefault();
if (!isFileTransfer(e)) {
return false;
}
if (!$(CHV.fn.uploader.selectors.root).data("shown")) {
CHV.fn.uploader.toggle({
reset: false,
});
}
},
dragleave: function (e) {
$(CHV.fn.uploader.selectors.dropzone).remove();
if ($.isEmptyObject(CHV.fn.uploader.files)) {
CHV.fn.uploader.toggle();
}
},
drop: function (e) {
e.preventDefault();
CHV.fn.uploader.add(e);
$(CHV.fn.uploader.selectors.dropzone).remove();
},
},
CHV.fn.uploader.selectors.dropzone
);
}
//
$(document).on("keyup change", "[data-action=resize-combo-input]", function (
e
) {
var $parent = $(this).closest("[data-action=resize-combo-input]");
var $input_width = $("[name=form-width]", $parent);
var $input_height = $("[name=form-height]", $parent);
var ratio = $input_width.data("initial") / $input_height.data("initial");
var image = {
width: Math.round($input_width.prop("value") / ratio),
height: Math.round($input_height.prop("value") * ratio),
};
if ($(e.target).is($input_width)) {
$input_height.prop("value", Math.round(image.width));
} else {
$input_width.prop("value", Math.round(image.height));
}
});
// Edit item from queue
$(document).on(
"click",
anywhere_upload_queue + " [data-action=edit]",
function () {
var $item = $(this).closest("li"),
$queue = $item.closest("ul"),
id = $item.data("id"),
file = CHV.fn.uploader.files[id];
var modal = PF.obj.modal.selectors.root;
var queueObject = $.extend({}, file.formValues || file.parsedMeta);
// Inject global upload options if needed
var injectKeys = ["album_id", "category_id", "nsfw"];
for (var i = 0; i < injectKeys.length; i++) {
var key = injectKeys[i];
if (typeof queueObject[key] == typeof undefined) {
var $object = $(
"[name=upload-" + key.replace("_", "-") + "]",
CHV.fn.uploader.selectors.root
);
var value = $object.prop(
$object.is(":checkbox") ? "checked" : "value"
);
queueObject[key] = $object.is(":checkbox")
? value
? "1"
: null
: value;
}
}
// Resize before upload
PF.fn.modal.call({
type: "html",
template: $("#anywhere-upload-edit-item").html(),
callback: function () {
var imageMaxCfg = {
width:
CHV.obj.config.image.max_width != 0
? CHV.obj.config.image.max_width
: queueObject.width,
height:
CHV.obj.config.image.max_height != 0
? CHV.obj.config.image.max_height
: queueObject.height,
};
var imageMax = $.extend({}, imageMaxCfg);
var ratio = queueObject.width / queueObject.height;
imageMax.width = Math.round(imageMaxCfg.height * ratio);
imageMax.height = Math.round(imageMaxCfg.width / ratio);
if (imageMax.height > imageMaxCfg.height) {
imageMax.height = imageMaxCfg.height;
imageMax.width = Math.round(imageMax.height * ratio);
}
if (imageMax.width > imageMaxCfg.width) {
imageMax.width = imageMaxCfg.width;
imageMax.height = Math.round(imageMax.width / ratio);
}
$.each(queueObject, function (i, v) {
var name = "[name=form-" + i.replace(/_/g, "-") + "]";
var $input = $(name, modal);
if (!$input.exists()) return true;
// Input handler
if ($input.is(":checkbox")) {
$input.prop("checked", $input.attr("value") == v);
} else if ($input.is("select")) {
var $option = $input.find("[value=" + v + "]");
if (!$option.exists()) {
$option = $input.find("option:first");
}
$option.prop("selected", true);
} else {
$input.prop("value", v);
}
if (i == "width" || i == "height") {
var max = imageMax[i];
var value = file.parsedMeta[i] > max ? max : file.parsedMeta[i];
$input
.prop("max", value)
.data("initial", file.parsedMeta[i])
.prop("value", value);
}
});
// Warning on GIF images
if (file.parsedMeta.mimetype !== "image/gif") {
$("[ data-content=animated-gif-warning]", modal).remove();
}
// Canvas image preview
$(".image-preview", modal).append(
$("", {
class: "canvas",
})
);
var source_canvas = $(
".queue-item[data-id=" + id + "] .preview .canvas"
)[0];
var target_canvas = $(".image-preview .canvas", modal)[0];
target_canvas.width = source_canvas.width;
target_canvas.height = source_canvas.height;
var target_canvas_ctx = target_canvas.getContext("2d");
target_canvas_ctx.drawImage(source_canvas, 0, 0);
},
confirm: function () {
if (!PF.fn.form_modal_has_changed()) {
PF.fn.modal.close();
return;
}
// Validations (just in case)
var errors = false;
$.each(["width", "height"], function (i, v) {
var $input = $("[name=form-" + v + "]", modal);
var input_val = parseInt($input.val());
var min_val = parseInt($input.attr("min"));
var max_val = parseInt($input.attr("max"));
if (input_val > max_val || input_val < min_val) {
$input.highlight();
errors = true;
return true;
}
});
if (errors) {
PF.fn.growl.expirable(
PF.fn._s("Check the errors in the form to continue.")
);
return false;
}
if (typeof file.formValues == typeof undefined) {
// Stock formvalues object
file.formValues = {
title: null,
category_id: null,
width: null,
height: null,
nsfw: null,
expiration: null,
description: null,
album_id: null,
};
}
$(":input[name]", modal).each(function (i, v) {
var key = $(this)
.attr("name")
.replace("form-", "")
.replace(/-/g, "_");
if (typeof file.formValues[key] == typeof undefined) return true;
file.formValues[key] = $(this).is(":checkbox")
? $(this).is(":checked")
? $(this).prop("value")
: null
: $(this).prop("value");
});
CHV.fn.uploader.files[id].formValues = file.formValues;
return true;
},
});
}
);
// Remove item from queue
$(document).on(
"click",
anywhere_upload_queue + " [data-action=cancel]",
function () {
var $item = $(this).closest("li"),
$queue = $item.closest("ul"),
id = $item.data("id"),
queue_height = $queue.height(),
item_xhr_cancel = false;
if ($item.hasClass("completed") || $item.hasClass("failed")) {
return;
}
$("#tiptip_holder").hide();
$item.tipTip("destroy").remove();
if (queue_height !== $queue.height()) {
CHV.fn.uploader.boxSizer();
}
if (!$("li", $anywhere_upload_queue).exists()) {
$(
"[data-group=upload-queue-ready], [data-group=upload-queue], [data-group=upload-queue-ready]",
$anywhere_upload
).css("display", "");
}
if (
CHV.fn.uploader.files[id] &&
typeof CHV.fn.uploader.files[id].xhr !== "undefined"
) {
CHV.fn.uploader.files[id].xhr.abort();
item_xhr_cancel = true;
}
if (
typeof CHV.fn.uploader.files[id] !== typeof undefined &&
typeof CHV.fn.uploader.files[id].fromClipboard !== typeof undefined
) {
var c_md5 = CHV.fn.uploader.files[id].md5;
var c_index = CHV.fn.uploader.clipboardImages.indexOf(c_md5);
if (c_index > -1) {
CHV.fn.uploader.clipboardImages.splice(c_index, 1);
}
}
delete CHV.fn.uploader.files[id];
CHV.fn.uploader.queueSize();
if (Object.size(CHV.fn.uploader.files) == 0) {
// No queue left
// Null result ?
if (
!("success" in CHV.fn.uploader) ||
!("results" in CHV.fn.uploader) ||
(Object.size(CHV.fn.uploader.results.success) == 0 &&
Object.size(CHV.fn.uploader.results.error) == 0)
) {
CHV.fn.uploader.reset();
}
} else {
// Do we need to process the next item?
if (item_xhr_cancel && $("li.waiting", $queue).first().length !== 0) {
CHV.fn.uploader.upload($("li.waiting", $queue).first());
}
}
}
);
// Uploader
$(document).on("click", "[data-action=upload]", function () {
$(
"[data-group=upload], [data-group=upload-queue-ready]",
$anywhere_upload
).hide();
$anywhere_upload
.removeClass("queueReady")
.addClass("queueUploading")
.find("[data-group=uploading]")
.show();
CHV.fn.uploader.queueSize();
CHV.fn.uploader.canAdd = false;
$queue_items = $("li", $anywhere_upload_queue);
$queue_items.addClass("uploading waiting");
CHV.fn.uploader.timestamp = new Date().getTime();
CHV.fn.uploader.upload($queue_items.first("li"));
});
/*CHV.obj.image_viewer.$container.swipe({
swipe: function(event, direction, distance, duration, fingerCount) {
// right prev, left next
if(direction == "left" || direction == "right") {
var go = direction == "left" ? "next" : "prev",
$link = $("[data-action="+go+"]", ".image-viewer-navigation");
if($link.exists()) {
window.location = $link.attr("href");
return;
}
}
},
threshold: 100,
excludedElements: ".noSwipe",
allowPageScroll: "vertical"
});*/
// User page
if ($("body#user").exists()) {
if (PF.obj.listing.query_string.page > 1) {
var State = History.getState();
if (State.data && typeof State.data.scrollTop !== "undefined") {
if ($(window).scrollTop() !== State.data.scrollTop) {
$(window).scrollTop(State.data.scrollTop);
}
} else {
//var scrollTop = $(".follow-scroll").offset().top - $(".follow-scroll").height();
var scrollTop = $("#background-cover").height() - 160;
$("html, body").animate(
{
scrollTop: scrollTop,
},
0
);
}
}
}
if ($("#top-bar-shade").exists() && $("#top-bar-shade").css("opacity")) {
$("#top-bar-shade").data(
"initial-opacity",
Number($("#top-bar-shade").css("opacity"))
);
}
if (PF.fn.isDevice("phone")) {
// $("#top-bar-shade").css("opacity", 1);
}
$(window).on("scroll resize", function () {
// if (PF.fn.isDevice('phone')) {
// $("#background-cover-src").css("transform", "");
// $("#top-bar-shade").css("opacity", 1);
// return;
// }
var Y = $(window).scrollTop();
if (Y < 0) return;
var $top_bar = $("#top-bar");
var rate = Number(
Y /
($("#background-cover, [data-content=follow-scroll-opacity]").height() -
$top_bar.height())
);
if (rate > 1) rate = 1;
if ($("#top-bar-shade").data("initial-opacity")) {
rate += $("#top-bar-shade").data("initial-opacity");
}
$("#top-bar-shade").css({
opacity: rate,
});
// if (rate == 1) return;
$("#background-cover-src").css({
transform: "translate(0, " + Y * 0.8 + "px" + ")",
});
});
// Selectable list items
CHV.fn.bindSelectableItems();
// Image viewer page
if ($("body#image").exists()) {
// Data load detected
if ($(CHV.obj.image_viewer.selector + " [data-load=full]").length > 0) {
$(document).on("click", CHV.obj.image_viewer.loader, function (e) {
CHV.fn.viewerLoadImage();
});
if (
$(CHV.obj.image_viewer.loader).data("size") >
CHV.obj.config.image.load_max_filesize.getBytes()
) {
$(CHV.obj.image_viewer.loader).css("display", "block");
} else {
CHV.fn.viewerLoadImage();
}
// Fix viewer width when height changes and boom! a wild scrollbar appears
$(document).bind("DOMSubtreeModified", function () {
if (
$("html").height() > $(window).innerHeight() &&
!$("html").hasClass("scrollbar-y")
) {
$("html").addClass("scrollbar-y");
$(document).data({
width: $(this).width(),
height: $(this).height(),
});
CHV.fn.image_viewer_full_fix();
}
});
$(window).on("resize", function () {
CHV.fn.image_viewer_full_fix();
});
// Viewer navigation
$(document).on("keyup", function (e) {
var $this = $(e.target),
key = e.charCode || e.keyCode;
if ($this.is(":input")) {
return;
} else {
// Next 39, Prev 37
if (
CHV.obj.image_viewer.$navigation.exists() &&
(key == 37 || key == 39)
) {
var navigation_jump_url = $(
"[data-action=" + (key == 37 ? "prev" : "next") + "]",
CHV.obj.image_viewer.$navigation
).attr("href");
if (
typeof navigation_jump_url !== "undefined" &&
navigation_jump_url !== ""
) {
window.location = $(
"[data-action=" + (key == 37 ? "prev" : "next") + "]",
CHV.obj.image_viewer.$navigation
).attr("href");
}
}
}
});
} else {
CHV.fn.viewerImageZoomClass();
}
}
$(document)
.on("click", CHV.obj.image_viewer.container, function (e) {
if (
!(
$(this).hasClass("cursor-zoom-in") ||
$(this).hasClass("cursor-zoom-out")
)
)
return;
var zoom_in = $(this).hasClass("cursor-zoom-in");
$(this).removeClass("cursor-zoom-in cursor-zoom-out");
if (zoom_in) {
var width = $(this)[0].getBoundingClientRect().width,
height = $(this)[0].getBoundingClientRect().height,
ratio = $("img", this).attr("width") / $("img", this).attr("height"),
new_width;
if (typeof $(this).data("dimentions") == typeof undefined) {
$(this).data({
dimentions: {
width: width,
height: height,
},
ratio: ratio,
});
}
if ($("img", this).attr("width") > $(window).width()) {
$(this).css({
width: "100%",
});
new_width = $(this).width();
$(this).css({
width: width,
});
} else {
new_width = $("img", this).attr("width");
}
$(this)
.addClass("cursor-zoom-out")
.css({
width: new_width,
height: new_width / ratio + "px",
});
} else {
$(this).addClass("cursor-zoom-in").css($(this).data("dimentions"));
}
e.preventDefault();
})
.on("contextmenu", CHV.obj.image_viewer.container, function (e) {
if (!CHV.obj.config.image.right_click) {
e.preventDefault();
return false;
}
});
$(document).on(
"contextmenu",
"html.device-mobile a.image-container",
function (e) {
e.preventDefault();
return false;
}
);
$(document).on("keyup", "input[data-dashboard-tool]", function (e) {
if (e.keyCode == 13) {
var $button = $("[data-action=" + $(this).data("dashboard-tool") + "]");
$button.click();
}
});
$(document).on("click", "[data-action=dashboardTool]", function (e) {
e.preventDefault();
var tool = $(this).data("tool");
var dataSet = $(this).data("data");
var data = $.extend({}, dataSet);
var inputs = {};
for (var key in data) {
var val = $(data[key]).val();
if ($(data[key]).prop("disabled") || !val) {
return;
}
inputs[key] = $(data[key]);
data[key] = val;
}
data.action = tool;
var ajaxObj = {
type: "GET", // !
cache: false,
};
ajaxObj.data = data;
var $parent = $(this).closest(".input-label");
var validate = true;
var message;
if (validate == false) {
PF.fn.growl.expirable(message);
return;
}
for (var key in inputs) {
// inputs[key].prop("disabled", true);
}
PF.fn.loading.inline($(".loading", $parent), {
size: "small",
valign: "middle",
});
$parent.find(".btn .text").hide();
$.ajax(ajaxObj).complete(function (XHR) {
var response = XHR.responseJSON;
// inputs[key].prop("disabled", false);
$(".loading", $parent).empty();
$parent.find(".btn .text").show();
if (
response.status_code == 200 &&
typeof response.success.redirURL !== typeof undefined
) {
window.location.href = response.success.redirURL;
return;
}
PF.fn.growl.call(
response[response.status_code == 200 ? "success" : "error"].message
);
});
});
// Third-party plugin, magic comes in 3...
$(document).on("click", "[data-action=openerPostMessage]", function (e) {
if (!window.opener) return;
e.preventDefault();
var target_attr = "data-action-target";
var $target = $(
$(this).is("[" + target_attr + "]") ? $(this).attr(target_attr) : this
);
var val = $target[$target.is(":input") ? "val" : "html"]();
window.opener.postMessage(
{
id: window.name,
message: val,
},
"*"
);
});
/*
// Input copy
$(document).on("mouseenter mouseleave", ".input-copy", function(e){
if(navigator.userAgent.match(/(iPad|iPhone|iPod)/i)) {
return;
}
$(".btn-copy", this)[e.type == "mouseenter" ? "show" : "hide"]();
});
$(document).on("click", ".input-copy .btn-copy", function(){
var $input = $(this).closest(".input-copy").find("input");
$(this).hide();
$input.highlight();
});
*/
/**
* USER SIDE LISTING EDITOR
* -------------------------------------------------------------------------------------------------
*/
$(document).on("click", "[data-action=list-tools] [data-action]", function (
e
) {
var $this = $(e.target),
$list_item = $this.closest("[data-id]");
if (
$list_item &&
$list_item.find("[data-action=select]").exists() &&
(e.ctrlKey || e.metaKey) &&
e.altKey
) {
CHV.fn.list_editor.toggleSelectItem(
$list_item,
!$list_item.hasClass("selected")
);
e.preventDefault();
e.stopPropagation();
}
});
// On listing ajax, clear the "Clear selection" toggle
PF.fn.listing.ajax.callback = function (XHR) {
if (XHR.status !== 200) return;
CHV.fn.list_editor.listMassActionSet("select");
};
// Select all
$(document).on("click", "[data-action=list-select-all]", function () {
CHV.fn.list_editor.selectItem($(".list-item:visible:not(.selected)"));
CHV.fn.list_editor.listMassActionSet("clear");
});
// Clear all
$(document).on("click", "[data-action=list-clear-all]", function () {
PF.fn.close_pops();
CHV.fn.list_editor.clearSelection();
});
// List item tools action (single)
$(document).on("click", "[data-action=list-tools] [data-action]", function (
e
) {
if (e.isPropagationStopped()) return false;
var $list_item = $(this).closest(
PF.obj.listing.selectors.list_item + ", .viewer"
);
var id = $list_item.data("id");
if (typeof $list_item.data("type") !== "undefined") {
dealing_with = $list_item.data("type");
} else {
console.log("Error: data-type not defined");
return;
}
var $targets = $("[data-type=" + dealing_with + "][data-id=" + id + "]");
var $this_icon,
this_add_class,
this_remove_class,
this_label_text,
dealing_with;
switch ($(this).data("action")) {
case "select":
CHV.fn.list_editor.toggleSelectItem(
$list_item,
!$list_item.hasClass("selected")
);
break;
case "edit":
var modal_source = "[data-modal=form-edit-single]";
// Populate the modal before casting it
switch (dealing_with) {
case "image":
$("[name=form-image-title]", modal_source).attr(
"value",
$list_item.attr("data-title")
);
$("[name=form-image-description]", modal_source).html(
PF.fn.htmlEncode($list_item.data("description"))
);
$("[name=form-album-id]", modal_source)
.find("option")
.removeAttr("selected");
$("[name=form-album-id]", modal_source)
.find(
"[value=" +
$list_item.data(dealing_with == "image" ? "album-id" : "id") +
"]"
)
.attr("selected", true);
$("[name=form-category-id]", modal_source)
.find("option")
.removeAttr("selected");
$("[name=form-category-id]", modal_source)
.find("[value=" + $list_item.data("category-id") + "]")
.attr("selected", true);
$("[name=form-nsfw]", modal_source).attr(
"checked",
$list_item.data("flag") == "unsafe"
);
// Just in case...
$("[name=form-album-name]", modal_source).attr("value", "");
$("[name=form-album-description]", modal_source).html("");
$("[name=form-privacy]", modal_source)
.find("option")
.removeAttr("selected");
break;
case "album":
$("[data-action=album-switch]", modal_source).remove();
$("[name=form-album-name]", modal_source).attr(
"value",
$list_item.data("name")
);
$("[name=form-album-description]", modal_source).html(
PF.fn.htmlEncode($list_item.data("description"))
);
$("[name=form-privacy]", modal_source)
.find("option")
.removeAttr("selected");
$("[name=form-privacy]", modal_source)
.find("[value=" + $list_item.data("privacy") + "]")
.attr("selected", true);
if ($list_item.data("privacy") == "password") {
$("[data-combo-value=password]").show();
$("[name=form-album-password]", modal_source).attr(
"value",
$list_item.data("password")
);
} else {
$("[data-combo-value=password]").hide();
$("[name=form-album-password]", modal_source).attr("value", "");
}
break;
}
PF.fn.modal.call({
type: "html",
template: $(modal_source).html(),
ajax: {
url: PF.obj.config.json_api,
deferred: {
success: function (XHR) {
CHV.fn.list_editor.updateItem(
"[data-type=" + dealing_with + "][data-id=" + id + "]",
XHR.responseJSON[dealing_with],
"edit"
);
},
},
},
confirm: function () {
var $modal = $(PF.obj.modal.selectors.root);
if (
(dealing_with == "image" || dealing_with == "album") &&
$("[data-content=form-new-album]", $modal).is(":visible") &&
$("[name=form-album-name]", $modal).val() == ""
) {
PF.fn.growl.call(PF.fn._s("You must enter the album name."));
$("[name=form-album-name]", $modal).highlight();
return false;
}
if (!PF.fn.form_modal_has_changed()) {
PF.fn.modal.close();
return;
}
PF.obj.modal.form_data = {
action: "edit", // use the same method applied in viewer
edit: $list_item.data("type"),
single: true,
owner: CHV.obj.resource.user.id,
editing: {
id: id,
description: $(
"[name=form-" + dealing_with + "-description]",
$modal
).val(),
},
};
switch (dealing_with) {
case "image":
PF.obj.modal.form_data.editing.title = $(
"[name=form-image-title]",
$modal
).val();
PF.obj.modal.form_data.editing.category_id =
$("[name=form-category-id]", $modal).val() || null;
PF.obj.modal.form_data.editing.nsfw = $(
"[name=form-nsfw]",
$modal
).prop("checked")
? 1
: 0;
break;
case "album":
PF.obj.modal.form_data.editing.name = $(
"[name=form-album-name]",
$modal
).val();
PF.obj.modal.form_data.editing.privacy = $(
"[name=form-privacy]",
$modal
).val();
if (PF.obj.modal.form_data.editing.privacy == "password") {
PF.obj.modal.form_data.editing.password = $(
"[name=form-album-password]",
$modal
).val();
}
break;
}
PF.obj.modal.form_data.editing.new_album = $(
"[data-content=form-new-album]",
$modal
).is(":visible");
if (PF.obj.modal.form_data.editing.new_album) {
PF.obj.modal.form_data.editing.album_name = $(
"[name=form-album-name]",
$modal
).val();
PF.obj.modal.form_data.editing.album_privacy = $(
"[name=form-privacy]",
$modal
).val();
if (PF.obj.modal.form_data.editing.album_privacy == "password") {
PF.obj.modal.form_data.editing.album_password = $(
"[name=form-album-password]",
$modal
).val();
}
PF.obj.modal.form_data.editing.album_description = $(
"[name=form-album-description]",
$modal
).val();
} else {
PF.obj.modal.form_data.editing.album_id = $(
"[name=form-album-id]",
$modal
).val();
}
return true;
},
});
break;
case "move": // Move or create album
var modal_source = "[data-modal=form-move-single]";
// Fool the selected album
$("[name=form-album-id]", modal_source)
.find("option")
.removeAttr("selected");
$("[name=form-album-id]", modal_source)
.find(
"[value=" +
$list_item.data(dealing_with == "image" ? "album-id" : "id") +
"]"
)
.attr("selected", true);
// Just in case...
$("[name=form-album-name]", modal_source).attr("value", "");
$("[name=form-album-description]", modal_source).html("");
$("[name=form-privacy]", modal_source)
.find("option")
.removeAttr("selected");
PF.fn.modal.call({
type: "html",
template: $(modal_source).html(),
ajax: {
url: PF.obj.config.json_api,
deferred: {
success: function (XHR) {
CHV.fn.list_editor.updateMoveItemLists(
XHR.responseJSON,
dealing_with,
$targets
);
},
},
},
load: function () {
//$("[name=form-album-id]", PF.obj.modal.selectors.root).focus();
},
confirm: function () {
var $modal = $(PF.obj.modal.selectors.root);
if (
$("[data-content=form-new-album]", $modal).is(":visible") &&
$("[name=form-album-name]", $modal).val() == ""
) {
PF.fn.growl.call(PF.fn._s("You must enter the album name."));
$("[name=form-album-name]", $modal).highlight();
return false;
}
if (!PF.fn.form_modal_has_changed()) {
PF.fn.modal.close();
return;
}
PF.obj.modal.form_data = {
action: "edit", // use the same method applied in viewer
edit: $list_item.data("type"),
single: true,
owner: CHV.obj.resource.user.id,
editing: {
id: id,
},
};
PF.obj.modal.form_data.editing.new_album = $(
"[data-content=form-new-album]",
$modal
).is(":visible");
if (PF.obj.modal.form_data.editing.new_album) {
PF.obj.modal.form_data.editing.album_name = $(
"[name=form-album-name]",
$modal
).val();
PF.obj.modal.form_data.editing.album_privacy = $(
"[name=form-privacy]",
$modal
).val();
if (PF.obj.modal.form_data.editing.album_privacy == "password") {
PF.obj.modal.form_data.editing.album_password = $(
"[name=form-album-password]",
$modal
).val();
}
PF.obj.modal.form_data.editing.album_description = $(
"[name=form-album-description]",
$modal
).val();
} else {
PF.obj.modal.form_data.editing.album_id = $(
"[name=form-album-id]",
$modal
).val();
}
return true;
},
});
break;
case "delete":
PF.fn.modal.call({
type: "html",
template: $("[data-modal=form-delete-single]").html(),
button_submit: PF.fn._s("Confirm"),
ajax: {
url: PF.obj.config.json_api,
deferred: {
success: function (XHR) {
if (dealing_with == "album") {
$("[name=form-album-id]", "[data-modal]")
.find("[value=" + id + "]")
.remove();
CHV.fn.list_editor.updateUserCounters(
"image",
XHR.responseJSON.success.affected,
"-"
);
}
CHV.fn.list_editor.deleteFromList($list_item);
CHV.fn.queuePixel();
},
},
},
confirm: function () {
PF.obj.modal.form_data = {
action: "delete",
single: true,
delete: $list_item.data("type"),
deleting: {
id: id,
},
};
return true;
},
});
break;
case "flag":
$.ajax({
type: "POST",
data: {
action: "edit",
edit: "image",
single: true,
editing: {
id: id,
nsfw: $list_item.data("flag") == "unsafe" ? 0 : 1,
},
},
}).complete(function (XHR) {
var response = XHR.responseJSON;
var flag = response.image.nsfw == 1 ? "unsafe" : "safe";
$targets.attr("data-flag", flag).data("flag", flag);
// Remember me gansito
CHV.fn.list_editor.selectionCount();
});
break;
}
});
// Item action (multiple)
$(".pop-box-menu a", "[data-content=list-selection]").click(function (e) {
var $content_listing = $(PF.obj.listing.selectors.content_listing_visible);
if (typeof $content_listing.data("list") !== "undefined") {
dealing_with = $content_listing.data("list");
} else {
console.log("Error: data-list not defined");
return;
}
var $targets = $(
PF.obj.listing.selectors.list_item + ".selected",
$content_listing
),
ids = $.map($targets, function (e, i) {
return $(e).data("id");
});
$(this).closest(".pop-btn").click();
switch ($(this).data("action")) {
case "get-embed-codes":
// Prepare the HTML
var template = "[data-modal=form-embed-codes]";
var objects = [];
$("textarea", template).html("");
// Build the object
$targets.each(function () {
var aux = {
image: $.parseJSON(decodeURIComponent($(this).data("object"))),
};
if ("url" in aux.image) {
objects.push(aux);
}
});
CHV.fn.fillEmbedCodes(objects, template, "html");
PF.fn.modal.call({
type: "html",
template: $(template).html(),
buttons: false,
});
break;
case "clear":
CHV.fn.list_editor.clearSelection();
e.stopPropagation();
break;
case "move":
case "create-album":
var template =
$(this).data("action") == "move"
? "form-move-multiple"
: "form-create-album",
modal_source = "[data-modal=" + template + "]",
dealing_id_data = /image/.test(dealing_with) ? "album-id" : "id";
$("[name=form-album-id]", modal_source).find("[value=null]").remove();
// Fool the album selection
$("[name=form-album-id]", modal_source)
.find("option")
.removeAttr("selected");
// Just in case...
$("[name=form-album-name]", modal_source).attr("value", "");
$("[name=form-album-description]", modal_source).html("");
$("[name=form-privacy]", modal_source)
.find("option")
.removeAttr("selected");
// This is an extra step...
var album_id = $targets.first().data(dealing_id_data),
same_album = true;
$targets.each(function () {
if ($(this).data(dealing_id_data) !== album_id) {
same_album = false;
return false;
}
});
if (!same_album) {
$("[name=form-album-id]", modal_source).prepend(
'"
);
}
$("[name=form-album-id]", modal_source)
.find(
"[value=" +
(same_album ? $targets.first().data(dealing_id_data) : "null") +
"]"
)
.attr("selected", true);
PF.fn.modal.call({
type: "html",
template: $(modal_source).html(),
ajax: {
url: PF.obj.config.json_api,
deferred: {
success: function (XHR) {
CHV.fn.list_editor.updateMoveItemLists(
XHR.responseJSON,
dealing_with,
$targets
);
},
},
},
load: function () {
if (template == "form-move-multiple") {
//$("[name=form-album-id]", PF.obj.modal.selectors.root).focus();
}
},
confirm: function () {
var $modal = $(PF.obj.modal.selectors.root),
new_album = false;
if (
$("[data-content=form-new-album]", $modal).is(":visible") &&
$("[name=form-album-name]", $modal).val() == ""
) {
PF.fn.growl.call(PF.fn._s("You must enter the album name."));
$("[name=form-album-name]", $modal).highlight();
return false;
}
if ($("[data-content=form-new-album]", $modal).is(":visible")) {
new_album = true;
}
if (!PF.fn.form_modal_has_changed()) {
PF.fn.modal.close();
return;
}
var album_object = new_album ? "creating" : "moving";
PF.obj.modal.form_data = {
action: new_album ? "create-album" : "move",
type: dealing_with,
owner: CHV.obj.resource.user.id,
multiple: true,
album: {
ids: ids,
new: new_album,
},
};
if (new_album) {
PF.obj.modal.form_data.album.name = $(
"[name=form-album-name]",
$modal
).val();
PF.obj.modal.form_data.album.privacy = $(
"[name=form-privacy]",
$modal
).val();
if (PF.obj.modal.form_data.album.privacy == "password") {
PF.obj.modal.form_data.album.password = $(
"[name=form-album-password]",
$modal
).val();
}
PF.obj.modal.form_data.album.description = $(
"[name=form-album-description]",
$modal
).val();
} else {
PF.obj.modal.form_data.album.id = $(
"[name=form-album-id]",
$modal
).val();
}
return true;
},
});
break;
case "delete":
PF.fn.modal.call({
template: $("[data-modal=form-delete-multiple]").html(),
button_submit: PF.fn._s("Confirm"),
ajax: {
url: PF.obj.config.json_api,
deferred: {
success: function (XHR) {
// unificar
if (dealing_with == "albums") {
$targets.each(function () {
$("[name=form-album-id]", "[data-modal]")
.find("[value=" + $(this).data("id") + "]")
.remove();
});
CHV.fn.list_editor.updateUserCounters(
"image",
XHR.responseJSON.success.affected,
"-"
);
}
CHV.fn.list_editor.deleteFromList($targets);
CHV.fn.queuePixel();
},
},
},
confirm: function () {
PF.obj.modal.form_data = {
action: "delete",
from: "list",
delete: dealing_with,
multiple: true,
deleting: {
ids: ids,
},
};
return true;
},
});
break;
case "assign-category":
var category_id = $targets.first().data("category-id"),
same_category = true;
$targets.each(function () {
if ($(this).data("category-id") !== category_id) {
same_category = false;
return false;
}
});
PF.fn.modal.call({
type: "html",
template: $("[data-modal=form-assign-category]").html(),
forced: true,
ajax: {
url: PF.obj.config.json_api,
deferred: {
success: function (XHR) {
$targets.each(function () {
var response = XHR.responseJSON;
$(this).data("category-id", response.category_id);
});
CHV.fn.list_editor.clearSelection();
},
},
},
confirm: function () {
var $modal = $(PF.obj.modal.selectors.root),
form_category =
$("[name=form-category-id]", $modal).val() || null;
if (same_category && category_id == form_category) {
PF.fn.modal.close(function () {
CHV.fn.list_editor.clearSelection();
});
return false;
}
PF.obj.modal.form_data = {
action: "edit-category",
from: "list",
multiple: true,
editing: {
ids: ids,
category_id: form_category,
},
};
return true;
},
});
break;
case "flag-safe":
case "flag-unsafe":
var action = $(this).data("action"),
flag = action == "flag-safe" ? "safe" : "unsafe";
PF.fn.modal.call({
template: $("[data-modal=form-" + action + "]").html(),
button_submit: PF.fn._s("Confirm"),
ajax: {
url: PF.obj.config.json_api,
deferred: {
success: function (XHR) {
$targets.each(function () {
$(this)
.removeClass("safe unsafe")
.addClass(flag)
.removeAttr("data-flag")
.attr("data-flag", flag)
.data("flag", flag);
});
CHV.fn.list_editor.clearSelection();
},
},
},
confirm: function () {
PF.obj.modal.form_data = {
action: action,
from: "list",
multiple: true,
editing: {
ids: ids,
nsfw: action == "flag-safe" ? 0 : 1,
},
};
return true;
},
});
break;
}
if (PF.fn.isDevice(["phone", "phablet"])) {
return false;
}
});
// Image page
if ($("body#image").exists()) {
$(window).scroll(function () {
CHV.obj.topBar.transparencyScrollToggle();
});
}
$(document).on("click", "[data-action=disconnect]", function () {
var $this = $(this),
connection = $this.data("connection");
PF.fn.modal.confirm({
message: $this.data("confirm-message"),
ajax: {
data: {
action: "disconnect",
disconnect: connection,
user_id: CHV.obj.resource.user.id,
},
deferred: {
success: function (XHR) {
var response = XHR.responseJSON;
$("[data-connection=" + connection + "]").fadeOut(function () {
$($("[data-connect=" + connection + "]")).fadeIn();
$(this).remove();
if ($("[data-connection]").length == 0) {
$("[data-content=empty-message]").show();
}
PF.fn.growl.expirable(response.success.message);
});
},
error: function (XHR) {
var response = XHR.responseJSON;
PF.fn.growl.call(response.error.message);
},
},
},
});
});
$(document).on("click", "[data-action=delete-avatar]", function () {
var $parent = $(".user-settings-avatar"),
$loading = $(".loading-placeholder", $parent),
$top = $("#top-bar");
$loading.removeClass("hidden");
PF.fn.loading.inline($loading, {
center: true,
});
$.ajax({
type: "POST",
data: {
action: "delete",
delete: "avatar",
owner: CHV.obj.resource.user.id,
},
}).complete(function (XHR) {
$loading.addClass("hidden").empty();
if (XHR.status == 200) {
if (CHV.obj.logged_user.id == CHV.obj.resource.user.id) {
$("img.user-image", $top).hide();
$(".default-user-image", $top).removeClass("hidden");
}
$(".default-user-image", $parent).removeClass("hidden").css({
opacity: 0,
});
$(".btn-alt", $parent).closest("div").hide();
$("img.user-image", $parent).fadeOut(function () {
$(".default-user-image", $parent).animate({
opacity: 1,
});
});
} else {
PF.fn.growl.expirable(
PF.fn._s("An error occurred. Please try again later.")
);
}
});
});
$(document).on("change", "[data-content=user-avatar-upload-input]", function (
e
) {
e.preventDefault();
e.stopPropagation();
var $this = $(this),
$parent = $(".user-settings-avatar"),
$loading = $(".loading-placeholder", ".user-settings-avatar"),
$top = $("#top-bar"),
user_avatar_file = $(this)[0].files[0];
if ($this.data("uploading")) {
return;
}
if (/^image\/.*$/.test(user_avatar_file.type) == false) {
PF.fn.growl.call(PF.fn._s("Please select a valid image file type."));
return;
}
if (
user_avatar_file.size > CHV.obj.config.user.avatar_max_filesize.getBytes()
) {
PF.fn.growl.call(
PF.fn._s(
"Please select a picture of at most %s size.",
CHV.obj.config.user.avatar_max_filesize
)
);
return;
}
$loading.removeClass("hidden");
PF.fn.loading.inline($loading, {
center: true,
});
$this.data("uploading", true);
// HTML5 method
var user_avatar_fd = new FormData();
user_avatar_fd.append("source", user_avatar_file);
user_avatar_fd.append("action", "upload");
user_avatar_fd.append("type", "file");
user_avatar_fd.append("what", "avatar");
user_avatar_fd.append("owner", CHV.obj.resource.user.id);
user_avatar_fd.append("auth_token", PF.obj.config.auth_token);
avatarXHR = new XMLHttpRequest();
avatarXHR.open("POST", PF.obj.config.json_api, true);
avatarXHR.send(user_avatar_fd);
avatarXHR.onreadystatechange = function () {
if (this.readyState == 4) {
var response =
this.responseType !== "json"
? JSON.parse(this.response)
: this.response,
image = response.success.image;
$loading.addClass("hidden").empty();
if (this.status == 200) {
change_avatar = function (parent) {
$("img.user-image", parent)
.attr("src", image.url)
.removeClass("hidden")
.show();
};
hide_default = function (parent) {
$(".default-user-image", parent).addClass("hidden");
};
// Form
hide_default($parent);
$(".btn-alt", $parent).closest("div").show();
change_avatar($parent);
// Top
if (CHV.obj.logged_user.id == CHV.obj.resource.user.id) {
change_avatar($top);
hide_default($top);
}
PF.fn.growl.expirable(PF.fn._s("Profile image updated."));
} else {
PF.fn.growl.expirable(
PF.fn._s("An error occurred. Please try again later.")
);
}
$this.data("uploading", false);
}
};
});
$(document).on(
"change",
"[data-content=user-background-upload-input]",
function (e) {
e.preventDefault();
e.stopPropagation();
var $this = $(this),
$parent = $("[data-content=user-background-cover]"),
$src = $("[data-content=user-background-cover-src]"),
$loading = $(".loading-placeholder", $parent),
$top = $("#top-bar"),
user_file = $(this)[0].files[0];
if ($this.data("uploading")) {
return;
}
if (/^image\/.*$/.test(user_file.type) == false) {
PF.fn.growl.call(PF.fn._s("Please select a valid image file type."));
return;
}
if (
user_file.size > CHV.obj.config.user.background_max_filesize.getBytes()
) {
PF.fn.growl.call(
PF.fn._s(
"Please select a picture of at most %s size.",
CHV.obj.config.user.background_max_filesize
)
);
return;
}
$loading.removeClass("hidden");
PF.fn.loading.inline($loading, {
center: true,
size: "big",
color: "#FFF",
});
$this.data("uploading", true);
// HTML5 method
var user_picture_fd = new FormData();
user_picture_fd.append("source", user_file);
user_picture_fd.append("action", "upload");
user_picture_fd.append("type", "file");
user_picture_fd.append("what", "background");
user_picture_fd.append("owner", CHV.obj.resource.user.id);
user_picture_fd.append("auth_token", PF.obj.config.auth_token);
avatarXHR = new XMLHttpRequest();
avatarXHR.open("POST", PF.obj.config.json_api, true);
avatarXHR.send(user_picture_fd);
avatarXHR.onreadystatechange = function () {
if (this.readyState == 4) {
var response =
this.responseType !== "json"
? JSON.parse(this.response)
: this.response,
image = response.success.image;
if (this.status == 200) {
var $img = $("");
$img.attr("src", image.url).imagesLoaded(function () {
$loading.addClass("hidden").empty();
$src
.css("background-image", "url(" + image.url + ")")
.hide()
.fadeIn();
$("[data-content=user-change-background]", $parent).removeClass(
"hidden"
);
$parent.removeClass("no-background");
$("[data-content=user-upload-background]").hide();
$("[data-content=user-change-background]").show();
PF.fn.growl.expirable(
PF.fn._s("Profile background image updated.")
);
$img.remove();
if (typeof user_background_full_fix == "function") {
user_background_full_fix();
//PF.fn.follow_scroll_update();
}
});
} else {
$loading.addClass("hidden").empty();
PF.fn.growl.expirable(
PF.fn._s("An error occurred. Please try again later.")
);
}
$this.data("uploading", false);
}
};
}
);
CHV.fn.user_background = {
delete: {
submit: function () {
PF.obj.modal.form_data = {
action: "delete",
delete: "background",
owner: CHV.obj.resource.user.id,
};
return true;
},
deferred: {
success: {
before: function (XHR) {
$("[data-content=user-background-cover-src]").css(
"background-image",
"none"
);
$("[data-content=user-background-cover]")
.addClass("no-background")
.height("");
$("[data-content=user-upload-background]")
.removeClass("hidden")
.show();
$("[data-content=user-change-background]").hide();
$("#top-bar-shade").remove();
},
done: function (XHR) {
PF.fn.modal.close(function () {
PF.fn.growl.expirable(
PF.fn._s("Profile background image deleted.")
);
});
},
},
error: function (XHR) {
PF.fn.growl.expirable(
PF.fn._s("Error deleting profile background image.")
);
},
},
},
};
// Form things
CHV.str.mainform = "[data-content=main-form]";
CHV.obj.timezone = {
selector: "[data-content=timezone]",
input: "#timezone-region",
};
// Detect form changes
$(document).on("keyup change", CHV.str.mainform + " :input", function () {
if ($(this).is("[name=username]")) {
$("[data-text=username]").text($(this).val());
}
});
// Timezone handler
$(document).on("change", CHV.obj.timezone.input, function () {
var value = $(this).val(),
$timezone_combo = $("#timezone-combo-" + value);
$timezone_combo.find("option:first").prop("selected", true);
$(CHV.obj.timezone.selector).val($timezone_combo.val()).change();
});
$(document).on("change", "[id^=timezone-combo-]", function () {
var value = $(this).val();
$(CHV.obj.timezone.selector).val(value).change();
});
// Password match
$(document).on("keyup change blur", "[name^=new-password]", function () {
var $new_password = $("[name=new-password]"),
$new_password_confirm = $("[name=new-password-confirm]"),
hide = $new_password.val() == $new_password_confirm.val(),
$warning = $new_password_confirm
.closest(".input-password")
.find(".input-warning");
if ($warning.exists() == false) {
$warning = $("[data-message=new-password-confirm]");
}
if ($(this).is($new_password_confirm)) {
$new_password_confirm.data("touched", true);
}
if ($new_password_confirm.data("touched")) {
$warning
.text(!hide ? $warning.data("text") : "")
[!hide ? "removeClass" : "addClass"]("hidden-visibility");
}
});
// Submit form
$(document).on("submit", CHV.obj.mainform, function () {
switch ($(this).data("type")) {
case "password":
var $p1 = $("[name=new-password]", this),
$p2 = $("[name=new-password-confirm]", this);
if ($p1.val() !== "" || $p2.val() !== "") {
if ($p1.val() !== $p2.val()) {
$p1.highlight();
$p2.highlight();
PF.fn.growl.expirable(PF.fn._s("Passwords don't match"));
return false;
}
}
break;
}
});
$(document).on("click", "[data-action=check-for-updates]", function () {
PF.fn.loading.fullscreen();
CHV.fn.system.checkUpdates(function (XHR) {
PF.fn.loading.destroy("fullscreen");
if (XHR.status !== 200) {
PF.fn.growl.call(
PF.fn._s("An error occurred. Please try again later.")
);
return;
}
var data = XHR.responseJSON.software;
if (
PF.fn.versionCompare(
CHV.obj.system_info.version,
data.current_version
) == -1
) {
PF.fn.modal.simple({
title: PF.fn._s("Update available v%s", data.current_version),
message:
"
" +
PF.fn._s(
"There is an update available for your system. You can automatic download and install this update or go to %s to proceed to download the file.",
'' +
CHEVERETO.source.label +
""
) +
"
" +
PF.fn._s("The release notes for this update are:") +
"