Nerbert
Active member
I'm working on a project that involves using a custom JavaScript popup function that assigns action functions to any of several buttons defined in the arguments of the popup function. I want to be able to fire either ordinary functions or methods depending on the context. I declare the context to be either window or this in the arguments. This worked both ways in a test in an admin CP project (where popup() was a method) but isn't working for window in a forum project. In both cases all the code was inside the usual anonymous function I see in all XenForo JavaScript. Here's part of the code I'm using:
As I said above, context is set as one of the of popup function arguments and actions are assigned to the buttons in an object
The problem I'm having is I can't use an ordinary function definition function myFunction(e) { ...} but have to use window.myFunction = function(e) { ....}. I suspect this has to do with the anonymous function that assigns window as an argument. So what's this anonymous function for anyway? And is there a clean way to fix this?
JavaScript:
$popup.one("click", ":button", $.proxy(function(e) {
action = $(e.target).data("action");
context = typeof context != 'undefined' ? context : this ;
if(typeof context[action] == "function"){
fade = context[action](e);
}
if(fade !== false) {
$overlay.fadeOut(1000, function(e) {
$overlay.remove();
if(typeof overlayResize != "undefined") $(window).off("resize", overlayResize);
});
}
}, this));
As I said above, context is set as one of the of popup function arguments and actions are assigned to the buttons in an object
Code:
{buttonValue1 : action1, buttonValue2 : action2, .... }
The problem I'm having is I can't use an ordinary function definition function myFunction(e) { ...} but have to use window.myFunction = function(e) { ....}. I suspect this has to do with the anonymous function that assigns window as an argument. So what's this anonymous function for anyway? And is there a clean way to fix this?
JavaScript:
function openPopup($parent, formId, html, buttons, width, top, context, event) {
var $overlay, $popup, buttonString = "", value, type, action, $form, fade = true, overlayResize, left;
$("#overlay").remove();
if(typeof overlayResize != "undefined") $(window).off("resize", overlayResize);
$parent = typeof $parent == "string" ? $($parent) : $parent ;
width = typeof width == "number" ? width + "px" : "200px" ;
top = typeof top == "number" ? top + "px" : "200px" ;
if(typeof buttons != "udefined" && Object.keys(buttons).length > 0) {
$.each(buttons, function(buttonValue, action) {
type = action == "submit" || action == "reset" ? action : "button" ;
action = action == "submit" || action == "reset" ? "" : action ;
value = typeof xenPhrase == "undefined" || typeof xenPhrase[buttonValue] == "undefined" ? buttonValue : xenPhrase[buttonValue] ;
buttonString += '<input type="'+type+'" class="button" value="'+value+'" data-action="'+action+'" />';
});
}
$popup = $(
'<div id="popup" style="position:fixed;top:'+top+';">'+
'<form id="'+formId+'" class="xenForm formOverlay" style="width:'+width+';">'+
'<div>'+
html +
'<div class="buttons">'+buttonString+'</div>'+
'</div>'+
'</form>'+
'</div>'
);
$overlay = $('<div id="overlay" class="xenOverlay" style="height: ' + $parent.innerHeight() + 'px; margin-bottom: -' + $parent.innerHeight() + 'px;"></div>');
$overlay.prependTo($parent).append($popup).fadeIn(1000);
$form = $popup.find("form");
$popup.css("left", ($(document).innerWidth() - 17 - $popup.outerWidth())/2 + "px");
$(window).on("resize", overlayResize = function() {
$overlay.css({"height": $parent.innerHeight()+"px", "marginBottom": "-" + $parent.innerHeight() + "px"});
$popup.css("left", (($(document).innerWidth() - 17 - $popup.outerWidth())/2) + "px")
});
if(buttonString != "") {
$popup.one("click", ":button", $.proxy(function(e) {
action = $(e.target).data("action");
context = typeof context != 'undefined' ? context : this ;
if(typeof context[action] == "function"){
fade = context[action](e);
}
if(fade !== false) {
$overlay.fadeOut(1000, function(e) {
$overlay.remove();
if(typeof overlayResize != "undefined") $(window).off("resize", overlayResize);
});
}
}, this));
$form.on("submit", function() {
$overlay.fadeOut(1000, function(e) {
$overlay.remove();
if(typeof overlayResize != "undefined") $(window).off("resize", overlayResize);
});
});
}
}
Last edited: