Problems with my js/jquery code for application frames - javascript

My problem is that I have loaded up to application frame in js via the funtion i created called createFrame(). The fullScreen functionality isn't working properly ass it goes fullScreen nicely but if I move the application frame around the desktop it will not work properly even when I move it back to it's original position. The code changes the size of the application frame and temporally remove taskbar and user/time info pane at the top. It also doesn't seem to want to allow the use to touch the box that the minimize/exit/fullscreen buttons are in. It just hides everything and leave me with a blank screen.
The code works as follows: var app; creates new application frame(exit/fullscreen/minimize button + content). The width and height are defined by w and h in the functions first line which is called upon in my js file called front-end.js by calling createFrame(500,300); . Content_src should explain itself quit easily although the parameter in not used yet. The fullscreen button should be able to work as stated above but for some reason int wont work properly if I drag it around on the screen. That is ok for now as I have not worked out code to re-position it to 0,0 on the screen. But if I re-position it to 0,0 myself in the desktop it will not end up at 0,0 of the screen. I think this has something to do with me upsetting the position i.e. the application was not in draggable state till I moved it. Any ideas?
var isFullScreen;
function createFrame(w,h,content_src) {
var app = $('<div class = "application-frame ui-resizable-se" style="width:'+w+'px; height:'+h+'px;">\
<div class = "buttons-box">\
<div class = "exit-button">\
</div>\
<div class = "fullscreen-button">\
</div>\
<div class = "minimise-button">\
</div>\
</div>\
<div class = "content">\
</div>\
</div>');
var apps = [];
$(".desktop-box").append(app);
apps.push(app);
$(app).draggable({containment:"parent"}).resizable({containment:"parent",maxHeight: 678,maxWidth: 1361});
$(".exit-button").click(function() {
for(var i = 0; i <apps.length;i++) {
apps[i].pop($(app).fadeOut('very slow'));
for(var f = 0; f < fullscreenArray.length;f++) {
if(fullscreenArray[f]) {
$(".infoPane").show();
$(".info-dropdown").show();
$(".taskbar").show();
fullscreenArray[f] = false;
}
}
}
});
var fullscreenArray = [];
fullscreenArray.push(isFullScreen);
$(".minimisse-button").click(function() {
for(var i = 0; i <apps.length;i++) {
for(var f = 0; f < fullscreenArray.length;f++) {
if(fullscreenArray[f]) {
$(".infoPane").show();
$(".info-dropdown").show();
$(".taskbar").show();
fullscreenArray[f] = false;
apps[i].pop($(app).slideToggle('very slow'));
}
}
}
});
var fullScreenWidth = $(".desktop-box").width();
var fullScreenHeight = $("body").height()+5;
$(".fullscreen-button").click(function() {
for(var f = 0; f < fullscreenArray.length;f++) {
if(!fullscreenArray[f]) {
$(app).css({width:fullScreenWidth});
$(app).css({height:fullScreenHeight});
fullscreenArray[f] = true;
$(".infoPane").hide();
$(".info-dropdown").hide();
$(".taskbar").hide();
}else if(fullscreenArray[f]) {
$(app).css({width:w});
$(app).css({height:h});
fullscreenArray[f] = false;
$(".application-frame").draggable();
$(".infoPane").show();
$(".info-dropdown").show();
$(".taskbar").show();
}
}
});
}

I just found a solution to the problem if anybody else is trying to attempt something similar. All add is $(app).offset({top:0,left:0); in the if statement that makes isFullScreen true. and that will work!

Related

Javascript stops working when there is a second div with that class on the page

I have some code I am using to autoplay a Summary Block in Squarespace. It is a workaround done by automatically clicking the next button every three seconds. The code also has a shut off in case a viewer clicks the previous button.
This is working great except if there is more than one Summary Block on the page. As soon as there is more than one, the code doesn't run. I thought I was getting around more than one by having it loop through all of the Summary Blocks but it doesn't seem to be working (maybe I am misunderstanding the loop?).
var summaryCarousel = document.querySelectorAll(".sqs-block-summary-v2");
for (var i = 0; i < summaryCarousel.length; i++) {
var carouselNextArrow = summaryCarousel[i].querySelector(".summary-carousel-pager-next");
var carouselPrevArrow = summaryCarousel[i].querySelector(".summary-carousel-pager-prev");
carouselPrevArrow.addEventListener("click", carouselStopAutoplay, false);
}
function carouselClickNext() {
carouselNextArrow.click();
}
function carouselStopAutoplay() {
clearInterval(carouselAutoplay);
}
let carouselAutoplay = setInterval(carouselClickNext, 3000);
Can anyone see what I am doing wrong?
UPDATE
Thank you to charlietfl below for helping me out. The code is now working except it is making the Squarespace Editor open after every click. I came up with this work around to have it only apply the code on the live version of the site. However, if anyone has any ideas as to why the code is doing this, it would be greatly appreciated!!
if(window.location.href.indexOf("squarespace") < 0 ) {
var summaryCarousel = document.querySelectorAll(".sqs-block-summary-v2");
for (var i = 0; i < summaryCarousel.length; i++) {
initAutoClick(summaryCarousel[i]);
}
function initAutoClick(parent){
var carouselNextArrow = parent.querySelector(".summary-carousel-pager-next");
var carouselPrevArrow = parent.querySelector(".summary-carousel-pager-prev");
carouselPrevArrow.addEventListener("click", carouselStopAutoplay, false);
function carouselClickNext() {
carouselNextArrow.click();
}
function carouselStopAutoplay() {
clearInterval(carouselAutoplay);
}
let carouselAutoplay = setInterval(carouselClickNext, 3000);
}
}
The basic problem is after the loop completes the value of carouselNextArrow will be the one in the last summary block only. Also the setInterval would not be instance specific either
You could wrap this in a function to call for each summary block instance.
var summaryCarousel = document.querySelectorAll(".sqs-block-summary-v2");
for (var i = 0; i < summaryCarousel.length; i++) {
// call function for each instance
initAutoClick(summaryCarousel[i])
}
function initAutoClick(parent){
var carouselNextArrow = parent.querySelector(".summary-carousel-pager-next");
var carouselPrevArrow = parent.querySelector(".summary-carousel-pager-prev");
carouselPrevArrow.addEventListener("click", carouselStopAutoplay, false);
function carouselClickNext() {
carouselNextArrow.click();
}
function carouselStopAutoplay() {
clearInterval(carouselAutoplay);
}
let carouselAutoplay = setInterval(carouselClickNext, 3000);
}

Javascript/CSS, How to keep changes?

Basically I have this header that slides out of the way when people don't want it anymore and can be slid down again when they want it. I want it to be down by default however I don't want people to have to constantly click the header toggle every-time a new page loads. I have read about using cookies but my skill in javascript is rather limited. Here is the code I currently use that works well:
<div id="headtoggle" onclick="headertoggle()"></div>
<script>
function headertoggle() {
var top;
var pagetop;
if (document.getElementById("page-header").style.top === "-210px") {
top = "-11px";
pagetop = "275px";
} else {
top = "-210px";
pagetop = "80px";
}
document.getElementById("page-header").style.top = top;
document.getElementById("page-body").style.top = pagetop;
}
</script>
How do I change the code so that it "remembers" what the last setting was for each person? I am willing to also use jquery. Any help for this very novice coder would be more than appreciated.
Thanks,
Dylan
EDIT2: I changed the code given to me a bit and am having a new problem.
<div id="headtoggle" onclick="headertoggle()" onload="topposition()"></div>
<script>
function topposition() {
var top;
var pagetop;
if (localStorage.getItem("headerDown") == "false") {
top = "-11px";
pagetop = "275px";
} else {
top = "-210px";
pagetop = "80px";
}
document.getElementById("page-header").style.top = top;
document.getElementById("page-body").style.top = pagetop;
}
</script>
<script>
function headertoggle() {
var top;
var pagetop;
if (document.getElementById("page-header").style.top === "-210px") {
localStorage.setItem("headerDown", false);
top = "-11px";
pagetop = "275px";
} else {
localStorage.setItem("headerDown", true);
top = "-210px";
pagetop = "80px";
}
document.getElementById("page-header").style.top = top;
document.getElementById("page-body").style.top = pagetop;
}
</script>
The toggle works as intended however whenever I fire "function topposition ()" in the firefox console I get the following error:
SyntaxError: expected expression, got end of script data:,/*
EXPRESSION EVALUATED USING THE FIREBUG COMMAND LINE:
*/%0A%09function%20topposition() Line 2
You can create a cookie by using
document.cookie="headerDown=true";
Then when the header is moved up overwrite the cookie with the same code changed to false.
If you then add some code that reads the cookies on page load you will be able to determine what is need.
You can read cookies by accessing document.cookie which will be a string of any cookies available in the following format.
cookie1=value; cookie2=value; cookie3=value;
Look for your cookie and you should be able to set whether the header is up or down from there.
i would say local storage would be a good option for this. Just log an event or variable or whatever and then check for it each time you load the page.
https://developer.mozilla.org/en-US/docs/Web/API/Window.localStorage
Using local storage will allow you to save variables between page loads.
add localStorage.setItem(name,value) in to your if statements as below.
If you are only using the headerToggle function to position these elements then the if statement below the function should suffice. It requires two calls to the headertoggle function in the even that you want the header to be down.
You will need to make sure the if statement is placed after the html for the divs, most likely best place is the very bottom of the page.
function headertoggle() {
var top;
var pagetop;
if (document.getElementById("page-header").style.top === "-210px") {
localStorage.setItem("headerDown", true);
top = "-11px";
pagetop = "275px";
} else {
localStorage.setItem("headerDown", false);
top = "-210px";
pagetop = "80px";
}
document.getElementById("page-header").style.top = top;
document.getElementById("page-body").style.top = pagetop;
}
if (localStorage.getItem("headerDown") == "true") {
headertoggle();
}
headertoggle();

javascript crashing iPad browser

I have some javascript inside a function that creates and populates an image carousel. It works fine after activating it in a pop up window the first 5 or 6 times, but then it eventually crashes the browser. I think there's some kind of leak, like something inside of it needs to be deleted before it gets created again. I know it's the carousel because if I get rid of that part of the script, it no longer crashes.
Here's the carousel script:
/* carousel */
var carousel,
el,
i,
page,
slides;
carousel = new SwipeView('#wrapper', {
numberOfPages: slides.length,
hastyPageFlip: true
});
// Load initial data
for (i=0; i<3; i++) {
page = i==0 ? slides.length-1 : i-1;
el = document.createElement('span');
el.innerHTML = slides[page];
carousel.masterPages[i].appendChild(el)
}
carousel.onFlip(function () {
var el,
upcoming,
i;
for (i=0; i<3; i++) {
upcoming = carousel.masterPages[i].dataset.upcomingPageIndex;
if (upcoming != carousel.masterPages[i].dataset.pageIndex) {
el = carousel.masterPages[i].querySelector('span');
el.innerHTML = slides[upcoming];
}
}
});
This script runs every time I click a link that launches a floating window.
I found out that I needed to clear my wrapper div. In the beginning of my function call:
document.getElementById('wrapper').innerHTML = "";
Seems to work.

copy, drag & drop bug : item stops moving

I'm trying to drag and drop items on a page, but insteads of moving the item itself, I create a copy of it.
Here is my code. "copyDragDrop" is a div at the bottom of the page. It remains empty until the user strats dragging something.
function coordSouris()
{
return {
x:event.clientX + document.body.scrollLeft - document.body.clientLeft,
y:event.clientY + document.body.scrollTop - document.body.clientTop
};
}
function drag()
{
var pos = coordSouris(event);
copie = event.srcElement.cloneNode(true);
document.getElementById('copieDragDrop').appendChild(copie);
copie.style.position = 'absolute';
copie.style.display = 'block';
document.onmousemove = mouseMove;
document.onmouseup = drop;
}
function mouseMove()
{
if (copie != null)
{
var pos = coordSouris(event);
copie.style.left = pos.x;
copie.style.top = pos.y;
}
}
function drop()
{
var divCopie = document.getElementById('copieDragDrop');
if (divCopie.hasChildNodes() )
{
while ( divCopie.childNodes.length >= 1 )
{
divCopie.removeChild(divCopie.firstChild);
}
}
}
This code creates the copy, starts to move it, but after a few pixels, the copy stops following the mouse. If I release the mouse, "onmouseup" is not fired, but the copy starts to follow the mouse again ! I've tried the code on several items, the same bug occurs
I don't understand anything, any help is more than welcome.
UPDATE : I juste realised that all elements I tried the code on had something in common : they contained or were included in an ASP.net hyperlink control. The same code works well on regular HTML elements. There must be some auto-generated javascript for links that interferes with my code.
Couldn't find the auto-generated code responsible for the issue, so I simply solvec this by replacing Hyperlink controls by standard HTML anchors.

Dashcode webapp flickers upon transition, only in iPhone Safari

I created a simple RSS web app using the template in Dashcode. Problem is, when choosing items in the list from the feed the transition flickers (even with the default settings). I am guessing its because of the images in the posts.
I tried disabling the transitions completely but even then I get a flickering when returning to the list. This problem does not appear to affect safari on OSX only on the iphone.
Here is the code that I think is responsible:
var topStories = parseInt(attributes.topStories, 30);
function load()
{
dashcode.setupParts();
// set today's date
var todaysDate = document.getElementById("todaysDate");
todaysDate.innerText = createDateStr(new Date()).toUpperCase();
setupFilters("headlineList");
// This message checks for common errors with the RSS feed or setup.
// The handler will hide the split view and display the error message.
handleCommonErrors(attributes.dataSource,
function(errorMessage) {
var stackLayout = document.getElementById("StackLayout")
if (stackLayout) {
stackLayout.style.display = 'none';
}
showError(errorMessage);
});
// get notifications from the stack layout when the transition ends
document.getElementById("StackLayout").object.endTransitionCallback = function(stackLayout, oldView, newView) {
// clear selection of lists when navigating to the first view
var firstView = stackLayout.getAllViews()[0];
if (newView == firstView) {
document.getElementById("headlineList").object.clearSelection(true);
}
}
}
function articleClicked(event)
{
document.getElementById("StackLayout").object.setCurrentView("articlePage", false, true);
}
function backToArticlesClicked(event)
{
document.getElementById("StackLayout").object.setCurrentView("frontPage", true);
}
function readMoreClicked(event)
{
var headlineList = dashcode.getDataSource('headlineList');
var secondHeadlines = dashcode.getDataSource("secondHeadlines");
var selectedItem = null;
if (headlineList.hasSelection()) {
selectedItem = headlineList.selectedObjects()[0];
} else if (secondHeadlines.hasSelection()) {
selectedItem = secondHeadlines.selectedObjects()[0];
}
if (selectedItem) {
var link = selectedItem.valueForKeyPath('link');
// If the link is an object, not a string, then this may be an ATOM feed, grab the actual
// href from the href attr
if (typeof(link) == 'object') {
link = selectedItem.valueForKeyPath('link.$href');
// If the link is an array (there is more then one link), just grab the first one
if (DC.typeOf(link) == 'array') {
link = link[0];
}
}
window.location = link;
}
}
var headlineListDataSource = {
// The List calls this method once for every row.
prepareRow: function(rowElement, rowIndex, templateElements) {
if (rowIndex >= topStories) {
templateElements['headlineDescription'].style.display = 'none';
templateElements['headlineTitle'].style.fontSize = '15px';
}
}
};
The following CSS rule fixed all of my "-webkit-transition" animation flickering issues on the iPad:
body {-webkit-transform:translate3d(0,0,0);}
I am not sure how well that applies to your problem but in general you should set the backface visibility to hidden if not needed. That will most likely kill all flickering on a page.
-webkit-backface-visibility: hidden;

Categories