dynamically change template by dragging with angularjs - javascript

I have the following setup in my application:
http://plnkr.co/edit/I2RCjYU17SDX65thUUqD?p=preview
<div class="container-fluid">
<div class="row-fluid">
<div class="col-xs-7">
<div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title">
Panel 1
</div>
</div>
<div class="panel-body" style="height: 250px;">
Panel 1
</div>
</div>
</div>
<div class="col-xs-5">
<div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title">
Panel 2
</div>
</div>
<div class="panel-body" style="height: 250px;">
Panel 2
</div>
</div>
</div>
</div>
<div class="row-fluid">
<div class="col-xs-7">
<div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title">
Panel 3
</div>
</div>
<div class="panel-body" style="height: 250px;">
Panel 3
</div>
</div>
</div>
<div class="col-xs-5">
<div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title">
Panel 4
</div>
</div>
<div class="panel-body" style="height: 250px;">
Panel 4
</div>
</div>
</div>
</div>
What I am trying to do is to be able to re-order the panels by dragging them.
For example, I want to be able to drag panel 1 to the bottom right and have it switch positions with panel 4. I'm relatively new to angularjs and I have no idea how to go about tackling a problem like this.
My initial idea was to create a different template for each possible combination of panels and dynamically include the template off of a variable I set, however this would require having 16 different templates and I feel like there must be a better solution.

You can use a external dependency (Angular-dragdrop) within Angularjs. Give each canvas a sequence and reorder by sequence when you drag a canvas in an other place.
Here you find more information: http://codef0rmer.github.io/angular-dragdrop/#/

Related

How to make columns/accordion open (or collapse) when the mouse moves over (or leaves) the panel?

I tried to incorporate solutions from experts all over but I still cannot get my columns to open when I hover the mouse over the panel.
What I'd like to achieve is:
When the mouse is moved to any panel area (it does not have to be over the actual anchor text), the panel body automatically opens (and subsequently all other panels close which the code already does).
When the mouse leaves the panel area the panel body automatically collapses.
Please review my code and let me know what I am doing wrong. Thank you very much for your time!
<div class="container">
<div class="row">
<div class="panel-group" id="Heyaccordion">
<div class="panel panel-default col-lg-2">
<div class="panel-heading">
<h4 class="panel-title">
Box 1 </h4>
</div>
<div class="panel-collapse collapse" id="collapseTopOneMore">
<div class="panel-body">
Boxy 1 body
</div>
</div>
</div>
<div class="panel panel-default col-lg-2">
<div class="panel-heading">
<h4 class="panel-title">
Box 2</h4>
</div>
<div class="panel-collapse collapse" id="collapseTopTwoMore">
<div class="panel-body">
Box 2 body
</div>
</div>
</div>
<div class="panel panel-default col-lg-2">
<div class="panel-heading">
<h4 class="panel-title">
Box 3 </h4>
</div>
<div class="panel-collapse collapse" id="collapseTopThreeMore">
<div class="panel-body">
Box 3 body
</div>
</div>
</div>
</div>
</div>
In jscript
$(".panel-heading").mouseenter(function () {
$(".panel-collapse").fadeIn();
});
$(".panel-collapse").mouseleave(function(){
$(".panel-collapse").fadeOut();
});
Oh, I did this
.panel-title > a {
width: 100%;
display: block;
}
and the whole panel is clickable... but it still requires a click.
I am not 100% sure of what class to give the one you don't want to collapse (e.g expanded?) But this should remove the 'collapsed' class from the only the one that raised the event:
$(".panel-heading").mouseenter(function () {
$(this).click();
});
$(".panel-collapse").mouseleave(function(){
$(this).click();
});
Hope this will work for you.

Bootstrap columns enlarge the more I type in a search bar

When typing in to an input search bar, my Bootstrap div .col-xs-4 enlarges, breaking out of it's container. What's odd is, Developer Tools doesn't seem to show any changes in the code that would affect this.
To replicate this problem, try typing the full name of one of the images in this JSFiddle gallery i.e., "Satcom 2016", and watch the div burst out of it's container. Clearing the input then shows that every div has expanded, not just the visible ones.
JSFiddle
https://jsfiddle.net/vadrfgvv/
jQuery
function rowBreak(input) {
$('.col-flex').hide();
if (input) {
$('.row-flex').replaceWith(function() {
return $(this).html();
});
$('.col-flex').wrapAll('<div class="row">');
}
$('.col-visible').each(function(i, e) {
if (((i + 1) % 3 == 0) && ($(this).siblings().length != 2)) {
var x = $('.col-visible:gt(' + i + ')');
$('<div class="row">').append(x).insertAfter(this.closest('div.row'));
}
});
$('.col-visible').show();
}
rowBreak(0);
$('#search').on('keyup', function() {
$('.col-flex').removeClass('col-visible').hide();
var s = $(this).val().toLowerCase();
$('.col-flex').filter(function() {
return $(this).find('h4').text().toLowerCase().indexOf(s) > -1;
}).show().addClass('col-visible');
rowBreak(s);
});
$('#results').on('click', '.panel-search', function() {
$(this).toggleClass('panel-default').toggleClass('panel-success');
$(this).find('.panel-body').toggleClass('bg-green');
var image = $(this).find('img');
var imageID = image.attr('id');
// alert(image.width());
if ($(this).hasClass('panel-success')) {
image.clone().appendTo('.panel-include').removeClass('img-thumbnail').css('margin-bottom', '20px').attr('id', 'x' + imageID).wrap('<p></p>');
} else {
$('.panel-include').find(('#x' + imageID)).closest('p').remove();
}
if ($('.panel-include > p').length == 0) {
$('.panel-include').find('em').show();
} else {
$('.panel-include').find('em').hide();
}
});
$('.btn-save').on('click', function(e) {
e.preventDefault();
var logos = '';
$('.panel-include').find('p').each(function() {
imageID = $(this).find('img').attr('id');
logos += imageID;
});
if (logos) {
logos = logos.substr(1);
}
$("input[name=ids]").val(logos);
$(this).closest('form').submit();
});
HTML
<div class="container-fluid">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-search"></i></span>
<input class="form-control" id="search" type="text" placeholder="Search...">
</div>
</div>
<div class="row">
<div class="col-xs-3">
<div class="panel panel-primary">
<div class="panel-heading">
<h4 class="panel-title">
Your Chosen Content
</h4>
</div>
<div class="panel-body panel-include">
<em class="text-muted">Click on some images to add them here.</em>
</div>
</div>
<form action="#" method="get">
<input type="hidden" name="ids" value="">
<p class="text-right">
<button class="btn btn-primary btn-save" type="submit">
<i class="fa fa-save"></i> Save
</button>
</p>
</form>
</div>
<div class="col-xs-9">
<div id="results">
<div class="row row-flex">
<div class="col-xs-4 col-flex col-visible">
<div class="panel panel-search panel-default">
<div class="panel-heading">
<h4 class="panel-title">
AEClim
</h4>
</div>
<div class="panel-body">
<img class="img-responsive img-thumbnail img-gallery" src="http://placehold.it/160x100/cccccc/9a9a9a/&text=AEClim" id="1">
</div>
</div>
</div>
<div class="col-xs-4 col-flex col-visible">
<div class="panel panel-search panel-default">
<div class="panel-heading">
<h4 class="panel-title">
AEMET
</h4>
</div>
<div class="panel-body">
<img class="img-responsive img-thumbnail img-gallery" src="http://placehold.it/160x100/cccccc/9a9a9a/&text=AEMET" id="2">
</div>
</div>
</div>
<div class="col-xs-4 col-flex col-visible">
<div class="panel panel-search panel-default">
<div class="panel-heading">
<h4 class="panel-title">
AME
</h4>
</div>
<div class="panel-body">
<img class="img-responsive img-thumbnail img-gallery" src="http://placehold.it/160x100/cccccc/9a9a9a/&text=AME" id="3">
</div>
</div>
</div>
<div class="col-xs-4 col-flex col-visible">
<div class="panel panel-search panel-default">
<div class="panel-heading">
<h4 class="panel-title">
APMG
</h4>
</div>
<div class="panel-body">
<img class="img-responsive img-thumbnail img-gallery" src="http://placehold.it/160x100/cccccc/9a9a9a/&text=APMG" id="4">
</div>
</div>
</div>
<div class="col-xs-4 col-flex col-visible">
<div class="panel panel-search panel-default">
<div class="panel-heading">
<h4 class="panel-title">
ATC Network
</h4>
</div>
<div class="panel-body">
<img class="img-responsive img-thumbnail img-gallery" src="http://placehold.it/160x100/cccccc/9a9a9a/&text=ATC+Network" id="5">
</div>
</div>
</div>
<div class="col-xs-4 col-flex col-visible">
<div class="panel panel-search panel-default">
<div class="panel-heading">
<h4 class="panel-title">
Meteomet
</h4>
</div>
<div class="panel-body">
<img class="img-responsive img-thumbnail img-gallery" src="http://placehold.it/160x100/cccccc/9a9a9a/&text=Meteomet" id="6">
</div>
</div>
</div>
<div class="col-xs-4 col-flex col-visible">
<div class="panel panel-search panel-default">
<div class="panel-heading">
<h4 class="panel-title">
MMC 2016
</h4>
</div>
<div class="panel-body">
<img class="img-responsive img-thumbnail img-gallery" src="http://placehold.it/160x100/cccccc/9a9a9a/&text=MMC+2016" id="7">
</div>
</div>
</div>
<div class="col-xs-4 col-flex col-visible">
<div class="panel panel-search panel-default">
<div class="panel-heading">
<h4 class="panel-title">
Satcom 2016
</h4>
</div>
<div class="panel-body">
<img class="img-responsive img-thumbnail img-gallery" src="http://placehold.it/160x100/cccccc/9a9a9a/&text=Satcom+2016" id="8">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
I initially thought it was the .img-responsive class that was causing problems (maybe the image was enlarging when the div was hidden?), but it seems like the image doesn't actually enlarge with the div, so that rules that out.
I'm not sure where else to start debugging.
Note: I have kept the entirety of the Javascript that affects these page elements in my question/Fiddle, in case it is something there that is causing the problem.
.row {
margin-right: -15px;
margin-left: -15px;
}
This CSS is the cause of your problem. i didn't see exactly how, but your js code nesting a row for every character typed. and with this css in bootstrap.css each nested row grows larger and larger. the images expand too.
what you can do is, overwrite this css in your html file with <style> tag. something like:
<style>
.row{
margin-right:0px;
margin-left:0px;
}
</style>
but you should know this is only a quick fix. a hack. if i was in your place, i'd look for the reason why your code is nesting a row for every character.

Traversing the DOM to toggle a panel footer?

I'm new to jquery and I'm trying to figure out how to traverse the dom in order to toggle the collapse option on panel footers? Any pointers?
<div id="colDealDiv" class="row col-md-8 col-md-offset-2 colDiv">
<div class="panel panel-default">
<div class="panel-heading">
<p>Retail & Services</p></div>
<div class="panel-body">
<div id="thumbnailDealDiv" class="thumbnail">
<div id="captionDealDiv" class="caption">
<p>$35 -- 'Best of NY': Painting Parties at City Bars, Reg. $65</p>
<p>Paint Nite</p>
<p>New York</p>
<p>NY</p><img src="https://api.sqoot.com/v2/deals/4344628/image?api_key=demo" class="img-thumbnail">
<button class="btn btn-info" data-toggle="collapse"></button>
</div>
</div>
</div>
<div class="panel-footer collapse">
<p>46% off at Paint Nite</p>
<p>35</p>
<p>2016-05-24T13:00:00Z</p>
</div>
</div>
$(".panel").on("click", ".btn-info", function() {
$(this).parent().next(".panel-footer").collapse("toggle");
});

Adding javascript transition to boostrap accordion

I have an accordion to hide blog posts in a webpage. I have a downwards arrow which I want to rotate to an upwards arrow when a blog post (accordion section) has been opened. These should be independent of other arrows attached to other posts obviously.
I am using bootstrap framework as a base. I tried following the instructions here http://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_js_collapse_togglebtn&stacked=h , along with what I already know/have used to try to add rotation. My problem is that, as I'm not using a button (the whole blog post is clickable to expand and collapse it), I can't work out what I need to put in the javascript where the question marks are.
(adding a data-target attribute to the tag breaks the expandability of the post).
$(" ??? ").on("hide.bs.collapse", function(){
$('.expand-image.text-center.glyphicon.glyphicon-chevron-down').addClass('turn-arrow');
});
$(" ??? ").on("show.bs.collapse", function(){
$('.expand-image.text-center.glyphicon.glyphicon-chevron-down').removeClass('turn-arrow');
});
expand-image.text-center.glyphicon.glyphicon-chevron-down.turn-arrow {
-webkit-transform: rotate(-90deg);
-ms-transform: rotate(-90deg);
transform: rotate(-90deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<a class="blog" data-toggle="collapse" data-parent="#accordion"
href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
<div class="panel">
<div class="panel-heading" role="tab" id="headingOne">
<h4 class="panel-title">
<div class="row">
<div class="container col-md-12 heading-container">
<div class="col-md-1 col-sm-2 text-center">
<h3 class="date-text">Jun 25th</h3>
</div>
<div class="col-md-11 col-sm-10">
<h3>This is where the post title goes</h3>
</div>
</div>
</div>
</h4>
</div>
<div class="panel-teaser panel-body" >
<div class="row">
<div class="col-md-1">
</div>
<div class="container col-md-11">
This is where the description goes
This should be the blog post's first paragraph (which needs to be catchy, no images here though)
</div>
</div>
</div>
<div id="collapseOne" class="panel-collapse collapse in"
role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<div class="row">
<div class="col-md-1">
</div>
<div class="container col-md-11">
This is where the main text body goes.
This should be the rest of the blog post, images and all)
</div>
</div>
</div>
</div>
<span class="expand-image text-center glyphicon glyphicon-chevron-down"></span>
<hr>
</div>
</a>
<a class="blog collapsed" data-toggle="collapse" data-parent="#accordion"
href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
<div class="panel">
<div class="panel-heading" role="tab" id="headingTwo">
<h4 class="panel-title">
<div class="row">
<div class="container col-md-12 heading-container">
<div class="col-md-1 col-sm-2 text-center">
<h3 class="date-text">Jun 26th</h3>
</div>
<div class="col-md-11 col-sm-10">
<h3>This is where the post title goes</h3>
</div>
</div>
</div>
</h4>
</div>
<div class="panel-teaser panel-body">
<div class="row">
<div class="col-md-1">
</div>
<div class="container col-md-11">
This is where the description goes. This should be the blog post's first paragraph (which needs to be catchy, no images here though)
</div>
</div>
</div>
<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
<div class="panel-body">
<div class="row">
<div class="col-md-1">
</div>
<div class="container col-md-11">
This is where the main text body goes.
This should be the rest of the blog post, images and all)
</div>
</div>
</div>
</div>
<span class="expand-image text-center glyphicon glyphicon-chevron-down"></span>
<hr>
</div>
</a>
</div>
You can attach the eventHandler on any Element, that is one of the parent Elements of .collapse. This is because the Event is triggered on the .collapse Element and bubbles all the way uppward. You can read more about event bubbling here.
You could, for example, attach the eventHandler to every Element with class "blog" as I did to solve your problem.
Relevant jsFiddle
$(".blog").each(function () {
$(this).on("hide.bs.collapse", function () {
$(this).find('.expand-image.text-center.glyphicon.glyphicon-chevron-down').removeClass('turn-arrow');
});
$(this).on("show.bs.collapse", function () {
$(this).find('.expand-image.text-center.glyphicon.glyphicon-chevron-down').addClass('turn-arrow');
});
});
Note the .each at the beginning. This attaches a different eventHandler to every Element matching the selector.
Now you can search the icon to be rotated in this Element --> $(this).find(
This will only find the one arrow inside the clicked blog element.
A very detailed explanation can also be found in this post.

Why the position of bootstrap panels is random on Meteorjs?

I created a project with Meteorjs and Bootstrap 3.
In this project, I can add dynamically bootstrap panels (or modals) to the Home page.
What bothers me is the fact that the position of theses panels (or modals) is random, I would like them to be organized. E.g.: when the screen of computer is medium, I want 3 panels (or modals) in each column.
Would you have an idea on how to do that?
The code is:
<template name="home">
{{> navigation}}
<div class="container-fluid lov">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3">
{{#each positions}}
{{> position}}
{{/each}}
</div>
</div>
</div>
</template>
And this is the JavaScript of this template:
Template.home.events ({
'dblclick .lov': function (e, tmpl) {
e.preventDefault();
e.stopPropagation();
if(e.target.className === 'container-fluid lov'){
var id = Positions.insert({ name:'Clique here to change the title', domaine:'ici tu ecris ton text en gros',footername:'le nom de pays et la ville si tu veux'});
Session.set('editing_table',id);
}
}
});
This is the collection:
Positions = new Meteor.Collection('positions');
This one is the code of position template that allow us to create our panel:
<template name="position">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3">
<div class="panel panel-default" id="{{_id}}" style="position:absolute;left:{{left}};top:{{top}}; width:350px">
<div class="panel-heading">
{{#if hadanata3ou}}
<a class="close" data-dismiss="modal">x</a>{{/if}}
{{#if editing_tablename}}
<input class="input input-medium tablename" value="{{name}}" type="text" />
{{else}}
<h6 class="tablename">{{name}}</h6>
{{/if}}
<!-- <h3 class="panel-title">Panel title</h3> -->
</div>
<div class="panel-body">
<div id="{{_id}}">
{{#if editing_field}}
<textarea class="input input-lg efield" name="efield" type="text">
{{domaine}}
</textarea>
{{else}}
<span>
{{domaine}}
</span>
{{/if}}
</div>
</div>
<div class="panel-footer">
{{#if editing_footer}}
<input class="input input-medium footer" value="{{footername}}" type="text" />
{{else}}
<p class="muted">{{footername}}</p>
{{/if}}
</div>
</div>
</div>
</div>
</template>
I'll be honest, I don't know a whole lot about meteorjs templating, but I can show you how the Bootstrap grid system works for rendering different amounts of panels based on the size of the view.
Bootstrap uses 4 different sizes:
Large lg (Desktop)
Medium md (Landscape Tablet)
Small sm (Portrait Tablet)
Extra Small xs (Mobile)
And using the classes .visible-SIZE and .hidden-SIZE, you can control what elements are shown/rendered at what sizes. For example:
<div class="container-fluid">
<div class="row visible-lg visible-md hidden-sm hidden-xs" >
<div class="col-lg-4 col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h4><span class="hidden-lg visible-md">Panel One - Medium</span> <span class="hiddn-md visible-lg">Panel One - Large</span></h4>
</div>
</div>
</div>
<div class="col-lg-4 col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h4><span class="hidden-lg visible-md">Panel Two - Medium</span> <span class="hiddn-md visible-lg">Panel Two - Large</span></h4>
</div>
</div>
</div>
<div class="col-lg-4 col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h4><span class="hidden-lg visible-md">Panel Three - Medium</span> <span class="hiddn-md visible-lg">Panel Three - Large</span></h4>
</div>
</div>
</div>
</div>
<div class="row hidden-lg hidden-md visible-sm visible-xs">
<div class="col-sm-6 col-xs-6">
<div class="panel panel-default">
<div class="panel-heading">
<h4><span class="hidden-sm visible-xs">Panel One - Extra Small</span> <span class="hiddn-xs visible-sm">Panel One - Small</span></h4>
</div>
</div>
</div>
<div class="col-sm-6 col-xs-6">
<div class="panel panel-default">
<div class="panel-heading">
<h4><span class="hidden-sm visible-xs">Panel Two - Extra Small</span> <span class="hiddn-xs visible-sm">Panel Two - Small</span></h4>
</div>
</div>
</div>
</div>
</div>
This code renders a row of .panel panel-default <div> elements. When the view is md or lg, it renders 3 <div>'s, each with the class col-md-4 and col-lg-4. The total amount of columns you can have is 12, and a class with col-*-4 takes up 4 of those 12 slots. Therefore, 3 * 4 is 12, or 1 complete row. Same with 2 * 6 for the col-sm-6 and col-xs-6.
In your meterojs templates, you need to decide how to use these classes to render the correct amount of columns. It's not too difficult, but spend some time playing with it to get the layout you want. If you want to see the test layout in effect, check this link:
Bootply
And try resizing your browser window.
Hope that can provide you some insight into Bootstrap's grid layout, and how to render different columns depending on the size of your browser.

Categories