Popup

The Awesome Popup component is used to open a popup window or a dropdown, it can be initialized in advance, or opened directly with javascript. The popup content can be defined as a static string, loaded from the server by setting an url, or use a js function that will set content, that function could also load the content via ajax or generate it, grab it from another part of the page. It can beinitialized using Html.Awe().InitPopup() and after opened using awe.open js function.
It can also be opened directly with awe.open without initializing it in advance.

Initialize and open popup on button click

Popup content loaded from server by setting url
PopupDemo/Index.cshtml
@Html.Awe().InitPopup(new PopupOpt
{
Name = "popup1",
Url = Url.Action("Popup1"),
Title = "popup title"
})

@Html.Awe().Button().Text("Open Popup").OnClick(Html.Awe().OpenPopup("popup1"))

<button type="button" class="awe-btn" onclick="awe.open('popup1')">Open, without html helper</button>
Demos/Helpers/PopupDemoController.cs
public IActionResult Popup1()
{
return PartialView();
}
PopupDemo/Popup1.cshtml
Popup content loaded via ajax at @DateTime.UtcNow.ToLongTimeString() UTC

Position top, modal, close on click outside

Popup with modal, and position top, will close when clicking outside outside of the popup.
PopupDemo/Index.cshtml
@(Html.Awe().InitPopup(new PopupOpt
{
Name = "popupTop",
Content = "top modal popup content",
Width = 1000,
Title = "top modal popup title",
LoadOnce = true,
Modal = true,
Top = true,
OutClickClose = true
}))

@Html.Awe().Button().Text("Open top modal").OnClick(Html.Awe().OpenPopup("popupTop"))

Open without initialization

Open the popup in javascript without prior initialization.
Popup content is set by a js function which could make an ajax request to the server to get the content if needed.
PopupDemo/Index.cshtml
@Html.Awe().Button().Text("Open Popup").OnClick("openPopupAwe()")

<button type="button" class="awe-btn" onclick="openDropdown(event)">Dropdown Popup</button>

<script>
function openPopupAwe() {
awe.open("popupAwe",
{
init: ({sp, cont}) => {
//console.log(aweUtils.getParams(sp)); // load url/func parameters
cont.html('<div style="padding: 0 3em;">Are you sure you want to perform this action?</div>');
},
btns: [
{
text: 'Yes',
ok: 1, // add okbtn class
action: function () {
$(this).data('api').close();
}
},
{
text: 'No',
cssClass: 'dashed',
action: function () {
$(this).data('api').close();
}
}],
height: 200,
width: 700,
title: 'please confirm',
modal: true,
//content: 'hello world'
//top: true,
//close: function() { console.log('popup closed'); },
//url: '@Url.Action("Popup1")', // works if init and content not set
//params: { p: 123 }
//outClickClose: true,
//popupClass: 'class1',
//fullscreen: true,
//dropdown: true
});
}

function openDropdown(event) {
awe.open('dropDown1', {
content: 'hello world',
dropdown: true
}, event);
// event needed for the popup to know where to drop down from
}
</script>

Dropdown Popup

Open popup as a dropdown.
The Dropdown will use the eventpassed to awe.open to determine where to drop down from, however the opener can be also defined manually.
PopupDemo/Index.cshtml
@(Html.Awe().InitPopup(new PopupOpt
{
Name = "popupdd1",
Url = Url.Action("DropdownContent"),
LoadOnce = true,
Dropdown = true
}))

@Html.Awe().Button().Text("open popup").OnClick(Html.Awe().OpenPopup("popupdd1"))
<button type="button" class="awe-btn" onclick="awe.open('popupdd1', {}, event)">open the same popup</button>


<script>
function dropdownFromHeader(){
awe.open({
init: function ({cont}) {
var list = '<ul style="height:calc(100vh - 150px);">';
for (let i = 0; i < 100; i++) {
list += '<li> item ' + i + '</li>';
}
list += '</ul>';
cont.append(list);
},
dropdown: true,
opener: $('#header'),
modal: true,
popupClass: 'o-p-0', // padding: 0
width: 200
});
}
</script>
<button type="button" class="awe-btn" onclick="dropdownFromHeader()">dropdown from header</button>

Open dropdown popup on div click

click here
PopupDemo/Index.cshtml
<script>
$(function () {
// touchstart for ipad
$(document).on('click touchstart', '.area', function (e) {
var ev = e.type == 'touchstart' ? e.originalEvent.touches[0] : e;
awe.open({
id: 'areaPopup',
content: 'popup positioned at XY:<br/> ' + ev.clientX + ' : ' + ev.clientY,
dropdown: true,
width: 200,
xy: { x: ev.clientX, y: ev.clientY } },
ev);
});
});
</script>
<div class="area">
<div class="msg">click here</div>
</div>

Sending client side parameters to server on content load

Value sent to the server action that returns the popup content:
PopupDemo/Index.cshtml
<input type="text" id="txtParam1" value="textbox value abc" />

<br />
@(Html.Awe().InitPopup(new PopupOpt
{
Name = "PopupParams",
Url = Url.Action("PopupWithParameters"),
ParameterFunc = "setParams"
}
.Parent("txtParam1")
.Parameter("p1", 15)))

@(Html.Awe().Button().Text("Open Popup")
.OnClick(Html.Awe().OpenPopup("PopupParams").Params(new { Id = 123 })))
<script>
function setParams() {
return { a: "hi", b: "how are you" };
}
</script>
Demos/Helpers/PopupDemoController.cs
public IActionResult PopupWithParameters(string parent, int p1, string a, string b, int id)
{
ViewData["parent"] = parent;
ViewData["p1"] = p1;
ViewData["a"] = a;
ViewData["b"] = b;
ViewData["id"] = id;

return PartialView();
}
PopupDemo/PopupWithParameters.cshtml
parameter set in the OpenPopup call: id = @ViewData["id"]
<br />
value of the parent: @ViewData["parent"] <br/>
<br/>
value of the p1 parameter: @ViewData["p1"] <br/>
<br />
parameters sent by js function set using ParameterFunc:<br/>
a = @ViewData["a"] <br/>
b = @ViewData["b"] <br/>

Full screen popup

PopupDemo/Index.cshtml
@(Html.Awe().InitPopup(new PopupOpt
{
Name = "psize",
Url = Url.Action("PopupSize"),
Fullscreen = true,
Modal = true
}))

@Html.Awe().Button().Text("Open").OnClick(Html.Awe().OpenPopup("psize"))

Buttons

PopupDemo/Index.cshtml
<div id="p3Cont"></div>

@(Html.Awe().InitPopup(new PopupOpt
{
Name = "p3",
Url = Url.Action("PopupBtns"),
OnClose = "onClose",
Buttons = new []
{
new PopupButtonOpt{ Text = "Add hi text to div", Action = "addHi" },
new PopupButtonOpt{ Text = "Close", Action = "closePopup" },
}
}))

@Html.Awe().Button().Text("Open").OnClick(Html.Awe().OpenPopup("p3"))
<div id="p3Log"></div>
<script type="text/javascript">
function closePopup() {
$(this).data('api').close();
}

function addHi() {
$(this).prepend('<p>hi</p>');
$(this).data('api').lay();
}

function onClose() {
awe.flash($('#p3Log').html('popup was closed at ' + site.getFormattedTime()));
}

</script>

Confirm popup close

PopupDemo/Index.cshtml
@(Html.Awe().InitPopup(new PopupOpt
{
Name = "popConfirmClose",
PopupClass = "needConfirm",
Title = "Confirm close example",
Content = "closing this popup will open a confirm popup"
}))

<button type="button" class="awe-btn" onclick="awe.open('popConfirmClose')">Open popup with confirm close</button>
<script type="text/javascript">
$(function () {
// registering a global confirm close for all popups that have "needConfirm" popup class
$(document).on('awebfclose', function (e, opt) {
var pdiv = $(e.target);
if (!pdiv.closest('.needConfirm').length || opt.force) return;
opt.noclose = true;

var mainApi = pdiv.data('o').cx.api;

awe.open("confirm",
{
content: '<div style="padding: 0 3em;">Are you sure you want to close this popup?</div>',
btns: [
{
text: 'Yes',
ok: 1, // add okbtn class
action: function () {
$(this).data('api').close();
mainApi.close({ force: true });
}
},
{
text: 'No',
action: function () {
$(this).data('api').close();
}
}],
height: 200,
modal: true,
title: 'Confirm close'
});
});
});
</script>

Autosize

Open any popup and try resizing the browser (try to make it smaller than the popup), works for popups from other helpers as well.

Notes

When there is more Popup helpers declared that have the same .Name(string), they will share the same popup window, so opening one will close the other; if you need to have popups with different names that close the other popups on open use .Group(string).


Popup Mods

See also the PopupForm helper used in Grid Popup CRUD demo, and in the Wizard Demo


Comments