How to rearrange the items of a list and the DOM with MooTools? - dom

I'm trying to rearrange the items of a list:
<ul>
<li>1 <button><li>
<li>2 <button><li>
<li>3 <button><li>
<li>4 <button><li>
</ul>
using MooTools:
document.getElements('button').addEvent('click', function() {
var toSort = new Fx.Sort(this.getParent(),this.getParent().getNext());
toSort.swap();
toSort = toSort.rearrangeDOM();
}
When I click the button of the first list element y expect:
<ul>
<li>2 <button><li>
<li>1 <button><li>
<li>3 <button><li>
<li>4 <button><li>
</ul>
but I get:
<ul>
<li>2 <button><li>
<li>3 <button><li>
<li>4 <button><li>
<li>1 <button><li>
</ul>
What I'm doing wrong?
Thanks in advance

depends what the effect is that you want to achieve. what is the purpose of 'button'? move up? move to top? move to bottom?
here's an example of a Fx.Sort use that sends a row up (if not first already)
http://jsfiddle.net/dimitar/ZQhgF/
var sort = new Fx.Sort($$("ul li"), {
transition: Fx.Transitions.linear.easeInOut,
link: "chain",
duration: 500,
mode: "vertical",
onComplete: function() {
this.rearrangeDOM();
}
});
document.getElements('button').addEvent('click', function(e) {
e.stop();
var el = this.getParent(), prev = el.getPrevious();
if (!prev)
return;
sort.swap(el, prev);
});
which works on this dom:
<ul id="sorter">
<li>1 <button>up</button></li>
<li>2 <button>up</button></li>
<li>3 <button>up</button></li>
<li>4 <button>up</button></li>
</ul>
it should give you some ideas. what you could also do is a serialize function that can allow you to set any custom order you want and / or save the order after.

Related

How to trigger mapbox resize on bootstrap-tab click

I have a mapbox map inside a bootstrap-3 tab like so:
<div id='resizeMap' class='button'>Resize map</div>
<div class="tabs">
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">
<a href="#tab-87" aria-controls="tab-87" role="tab" data-toggle="tab">
Tab-1
</a>
</li>
<li role="presentation">
<a href="#tab-1034" aria-controls="tab-1034" role="tab" data-toggle="tab">
Tab-2
</a>
</li>
</ul>
<div class="tab-content">
<div role="tabpanel" id="tab-87" class="tab-pane in active fade">
Some other content
</div>
<div role="tabpanel" id="tab-1034" class="tab-pane fade">
<div class="map-container" style="position:relative;width:100%; height:500px;">
</div>
</div>
</div>
The problem is that when I click on the 2nd tab (where the map is, the map does not have the right dimensions).
I can add a button to trigger resize as mentioned here but I can't figure out how to trigger resize automatically upon clicking a tab.
I also tried to "simulate" clicking as in:
$('a[data-toggle="tab"]').on( "click", function() {
$("#resizeMap").click();
});
This works, but only when I click the tab twice: if tab-1 is active, I click tab-2, the tab-content appears with the map not sized correctly, if I click on tab-2 for a second time, it resizes.
Thanks
For anyone else that may face this:
Not having access to the map instance, means that we have to define the call to map.resize() from within map.on('load', function(){...}).
Also, we need to make sure to resize after tab-switching. The following code resizes the map after the bootstrap animation finishes. Like so:
var map = new mapboxgl.Map({
container: 'site-map', // container id
style: 'mapbox://styles/mapbox/outdoors-v10',
center: [1, 49],
zoom: 5,
});
map.on('load', function () {
map.addSource('data',{
type: 'geojson',
data: someData,
cluster:true
})
$('a[data-toggle="tab"]').on('shown.bs.tab', function(){
map.resize();
});
});
If you have access to the map instance (the return value of new mapboxgl.Map()), you can just call the resize method of the the map, after changing tabs: https://www.mapbox.com/mapbox-gl-js/api/#map#resize

magnific popup autoplay slideshow

I am using magnific popup for popup image slideshow. Below is what i have done.
$('.parent-container').magnificPopup({
type:'image',
delegate: 'a',
gallery: {
// options for gallery
enabled: true
},
image: {
// options for image content type
titleSrc: 'title'
}
});
And this is my HTML Markup
<div class="parent-container">
<ul class="row carousel-inner" style="margin:0; padding:0; background:red;">
<li class="active item gallery-photo" style="margin:0; padding:0; background:red;">
<img class="img-responsive" src="images/gallery-1.jpg">
</li>
<li class="item">
<img class="img-responsive" src="images/gallery-2.jpg">
</li>
<li class="item">
<img class="img-responsive" src="images/gallery-3.jpg">
</li>
</ul>
</div>
Now, everything is working fine, but i want the image to change automatically, like when i launch the popup first image will be displayed and then it should change automatically. I am not able to fine anything to make it auto in the documentation .
Your efforts will be greatly apprecialted.
I have found it, i am adding this as an answer so that someone else also can benefit out of it.
You just need to add callbacks.
I have added below code after magnificPopup({}) function. And that will solve the problem images will start moving automatically.
callbacks: {
open: {
setInterval(function() {
$.magnificPopup.instance.next();
}, 2000);
}
}

angularjs socials share buttons troubles the display is only in one page

I'm going mad with a simple embedded of social buttons.
Put it in this way I've got two states home and blog
with this code
home
<div id="socials-bar" class=" clearfix">
<ul class="pull-right">
<li>
<div class="fb-like" data-href="http://mydomain.com" data-layout="standard" data-action="like" data-show-faces="false" data-share="true"></div>
</li>
<li>
Tweet
</li>
<li>
<div class="g-plusone" data-size="small" data-annotation="inline" data-width="120" data-href="http://mydomain.it"></div>
</li>
</ul>
</div>
//other html
<script>
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');
</script>
<script>
window.___gcfg = {lang: 'it'};
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/platform.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
blog
<div class="clearfix">
<ul class="article-socials pull-right">
<li>
<div class="fb-like" data-href="http://mydomain.it/blog/{{article.id}}/{{article.slug}}" data-layout="standard" data-action="like" data-show-faces="false" data-share="true"></div>
</li>
<li>
Tweet
</li>
<li>
<div class="g-plusone" data-size="small" data-annotation="inline" data-width="120" data-href="http://mydomain.it/blog/{{article.id}}/{{article.slug}}"></div>
</li>
</ul>
</div>
<script>
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');
</script>
<script>
window.___gcfg = {lang: 'it'};
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/platform.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
The problem is that the I see them only in the home
page if I get rid off of them in home I can see them in the blog state
Do you know what could be the problem ?
UPDATE: I've updated the below directives and registered them on bower with the package name 'angulike', for full details and a working demo go to http://jasonwatmore.com/post/2014/08/01/AngularJS-directives-for-social-sharing-buttons-Facebook-Like-GooglePlus-Twitter-and-Pinterest.aspx
The problem is that the social buttons only get initialized on page load by default, so in an angularjs app you need to re-initialize the buttons yourself when the view changes. I got them working by creating a directive for each, below is the code:
Google Plus Button Directive
app.directive('googlePlus', ['$window', function ($window) {
return {
restrict: 'A',
template: '<div class="g-plusone" data-size="medium"></div>',
link: function (scope, element, attrs) {
scope.$watch(function () { return !!$window.gapi; },
function (gapiIsReady) {
if (gapiIsReady) {
$window.gapi.plusone.go(element.parent()[0]);
}
});
}
};
}]);
Tweet Button Directive
app.directive('tweet', ['$window', function ($window) {
return {
restrict: 'A',
template: 'Tweet',
link: function (scope, element, attrs) {
scope.$watch(function () { return !!$window.twttr; },
function (twttrIsReady) {
if (twttrIsReady) {
$window.twttr.widgets.load(element.parent()[0]);
}
});
}
};
}]);
Facebook Like Button Directive
app.directive('fbLike', ['$window', function ($window) {
return {
restrict: 'A',
template: '<div class="fb-like" data-layout="button_count" data-action="like" data-show-faces="true" data-share="true"></div>',
link: function (scope, element, attrs) {
scope.$watch(function () { return !!$window.FB; },
function (fbIsReady) {
if (fbIsReady) {
$window.FB.XFBML.parse(element.parent()[0]);
}
});
}
};
}]);
HTML to use the directives
<div data-fb-like=""></div>
<div data-tweet=""></div>
<div data-google-plus=""></div>
If I understand, probably you should remove the script in blog state template.
Anyway, I can share how I do in my sample project:
index.html
directive for social buttons
template for
social buttons
basically in index there are scripts for social buttons available for all states

routing with redirect and automatic child route transition with unexpected behavior

I have a routing related question.
App.Router.map(function(){
this.resource('whiteBoards',function(){
this.resource('whiteBoard',{path:'/:whiteBoard_id'},function(){
this.resource('image');
this.resource('video');
});
});
this.resource('dummy');
});
App.WhiteBoardsRoute = Ember.Route.extend({
model: function(){
return this.store.find('whiteBoard');
}
});
App.ImageRoute = Ember.Route.extend({
model: function(){
return this.store.find('image');
}
});
If I click all navigational links manually all things works as expected.
my templates:
<script type="text/x-handlebars" data-template-name="application">
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="navbar-header">
<!-- Be sure to leave the brand out there if you want it shown -->
<a class="navbar-brand" href="/index.html">Routing Test</a>
</div>
<ul class="nav navbar-nav">
<li>{{#link-to 'whiteBoards'}}WhiteBoard{{/link-to}}</li>
<li>{{#link-to 'dummy'}}Dummy{{/link-to}}</li>
</ul>
</div>
</nav>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="whiteBoards">
<ul class="nav nav-tabs whiteBoards">
{{#each whiteBoard in controller.content}}
<li>
{{#link-to 'whiteBoard' whiteBoard}}{{whiteBoard.title}} {{/link-to}}
</li>
{{/each}}
</ul>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="whiteBoard">
<div class="whiteBoard">
<H3>WhiteBoard {{controller.content.name}}</H3>
</div>
<ul class="nav nav-tabs">
<li>
{{#link-to 'image'}}Images{{/link-to}}
</li>
<li>
{{#link-to 'video'}}Videos{{/link-to}}
</li>
</ul>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="image">
<div class="imagelist">
<H3>Images</H3>
<ul>
{{#each img in controller.content}}
<li>{{img.name}}</li>
{{/each}}
</ul>
</div>
</script>
The problem occurs if I try to activate the image route (as default child route) in combination with a whiteBoard navigation click.
This mean If I select a whiteBoard a list of images should also be shown (here only a simplified image list).
To achieve this I insert:
App.WhiteBoardIndexRoute = Ember.Route.extend({
redirect: function(){
this.transitionTo('image');
}
});
The first time it works as expected (the selected whiteBoard is shown and the image list).
If I try to navigate to another whiteBoard the problems begins.
After inserting WhiteBoardIndexRoute I can't navigate to another whiteBoard.
What is the problem ?
I'm working with ember.js verion 1.0
I known that I can implement the display of the image list using the renderTemplate method in the WhiteBoardRoute but I think that it should be possible to use the "default child route concept".

jQuery toggle only works on second click after scrollTo event

I have some mobile navigation that uses a scrollTo function, that once clicked, makes my toggle function only work on the second click. It may have to do the the binding of the scrollTo click, but I can't figure out how to fix it.
HTML:
<div class="nav-bar">
<ul class="nav">
<li>Section 1</li>
<li>Section 2</li>
<li>Section 3</li>
</ul>
<a class="nav-toggle">Menu</a>
</div>
<div class="section-wrap">
<div id="section1" class="section">Section 1</div>
<div id="section2" class="section">Section 2</div>
<div id="section3" class="section">Section 3</div>
</div>
JS:
$('ul.nav a').bind('click',function(event){
$('ul.nav a').removeClass('on');
$(this).addClass('on');
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top - 30
}, 200, function() {
$('.nav-bar').removeClass('open');
});
event.preventDefault();
});
$('a.nav-toggle').toggle(function() {
$('.nav-bar').addClass('open');
}, function () {
$('.nav-bar').removeClass('open');
});
See this for a working example of the issue: http://jsfiddle.net/MhSKC/9/
At the end of your animate, you remove the open class. Then on the next click of the menu bar, the toggle function tries to remove the open class again, because that's the one of the two toggle functions that needs to execute next. Here's a slightly reworked solution:
http://jsfiddle.net/P4mtC/
$('a.nav-toggle').click(function() {
$('.nav-bar').toggleClass('open');
});
instead of using
$('a.nav-toggle').toggle(function() {
$('.nav-bar').addClass('open');
}, function () {
$('.nav-bar').removeClass('open');
});
use
$('a.nav-toggle').click(function() {
$('.nav-bar').toggleClass('open');
});