Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remember device positions #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ <h2>Updates</h2>
</div>
</section>
<script src="//code.jquery.com/jquery-latest.js"></script>
<script src="//code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<script src="//code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(document).ready(function() {
// Change iFrame on a Button Click Event
Expand All @@ -115,8 +115,57 @@ <h2>Updates</h2>
$(document).ready(function() {
var a = 3;
$('.desktop,.laptop,.tablet,.mobile').draggable({
start: function(event, ui) { $(this).css("z-index", a++); }
});
start: function(event, ui) { $(this).css("z-index", a++); },
stop: function(event, ui){
var pos = JSON.parse(localStorage.getItem('amiresponsivedesign.devicepositions'));
var name = $(this).attr('class').split(' ',1)[0];
pos[name].left = Math.round( $(this).position().left );
pos[name].top = Math.round( $(this).position().top );
pos[name].zindex = $(this).css('z-index');
localStorage.setItem('amiresponsivedesign.devicepositions', JSON.stringify(pos));
return true;
}
});

function isLocalStorageSupported(){
var mod = 'test';
try {
localStorage.setItem(mod, mod);
localStorage.removeItem(mod);
return true;
} catch(e) {
return false;
}
}

function createDefaultDevicePositionsStorage(){
console.log("creating default values");
var x = {};
x.desktop = {}; x.desktop.left = 220; x.desktop.top = 0; x.desktop.zindex = 0;
x.laptop = {}; x.laptop.left = 560; x.laptop.top = 264; x.laptop.zindex = 0;
x.tablet = {}; x.tablet.left = 120; x.tablet.top = 230; x.tablet.zindex = 0;
x.mobile = {}; x.mobile.left = 300; x.mobile.top = 375; x.mobile.zindex = 0;

localStorage.setItem('amiresponsivedesign.devicepositions', JSON.stringify(x));
}

function moveDevicesToOldPositions(){
if (! isLocalStorageSupported()) return true;

if(localStorage.getItem('amiresponsivedesign.devicepositions')) {
var x = JSON.parse(localStorage.getItem('amiresponsivedesign.devicepositions'));
$('.desktop,.laptop,.tablet,.mobile').each(
function(index){
var name = $(this).attr('class').split(' ',1)[0];
$(this).css({top: x[name].top, left: x[name].left, zIndex: x[name].zindex});
});
}else{
createDefaultDevicePositionsStorage();
}
}
moveDevicesToOldPositions();


$('.display div').click(function() {
$(this).addClass('top').removeClass('bottom');
$(this).siblings().removeClass('top').addClass('bottom');
Expand Down