Rails 'Best in Place' Gem - Flash success message? - ruby-on-rails

I'm using the best_in_place gem (https://github.com/bernat/best_in_place) for in place editing in a Rails app. My controller has the line:
format.json { respond_with_bip(#user) }
The gem generates purr notifications for any validation error, but is there any way for me to also flash a notice for a successful update? I can't seem to find this anywhere in the documentation, and the fact that bip responds with JSON makes the whole thing seem very black box to me.
Any help would be greatly appreciated.

$('.best_in_place').best_in_place().bind('ajax:success', function(evt, data, status, xhr) {
console.dir(evt);
console.dir(data);
console.dir(status);
console.dir(xhr);
// Use them how ever you need eg $("#yourdiv").text(data["message"]);
});
In your controller
format.json { render :json => { :message => "Successfully Saved your Model" } }

It depends on what you are doing in your controller, but couldn't you have an event that gets triggered upon update to display a notice - on ajax:success for example.
Upon re-reading your question, I don't think this'll help you actually. But check out Flash Render Gem just in case.
Here's the info on how to implement the gem.

I ran into a similar issue. What I did was make my own bip.purr.js file and removed the best_in_place.purr require from my application.js file:
//application.js
//= require jquery
//= require best_in_place
//= require jquery_ujs
//= require best_in_place.jquery-ui
//= require jquery.purr
//REMOVED / COMMENTED OUT THE NEXT LINE:
// require best_in_place.purr
// ADDED THIS LINE:
//= require bip.purr
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .
My 'error' function OOB was not working, so I changed that as well. If it works for you, you should look at the original source. You should do that anyway before making your own file, but this is what mine looks like (assets/javascripts/bp.purr.js):
/*
* BestInPlace 3.0.0.alpha (2014)
*
* Depends:
* best_in_place.js
* jquery.purr.js
*/
/*global BestInPlaceEditor */
BestInPlaceEditor.defaults.purrErrorContainer = "<span class='bip-flash-error'></span>";
BestInPlaceEditor.defaults.purrSuccessContainer = "<span class='bip-flash-success'></span>";
//edited this binding to stop showing 'Error Object object'
jQuery(document).on('best_in_place:error', function (event, request, error) {
'use strict';
// Display all error messages from server side validation
var errors_string = "";
jQuery.each(jQuery.parseJSON(request.responseText), function (index, value) {
if (typeof value === "object") {
$.each(value, function (i, v) {
errors_string += i[0].toUpperCase() + i.slice(1) + " " + v + ". ";
});
} else {
errors_string = value;
}
var container = jQuery(BestInPlaceEditor.defaults.purrErrorContainer).html(errors_string);
container.purr();
});
});
//added this binding for success messages:
jQuery(document).on('best_in_place:success', function (event, request, error) {
'use strict';
// Display all success messages from server side validation
var msg = "Updated Successfully";
var container = jQuery(BestInPlaceEditor.defaults.purrSuccessContainer).html(msg);
container.purr();
});
And, just for fun, some basic SCSS to get things looking different:
// for best in place / purr styling
.purr {
position: fixed;
top: 60px;
right: 100px;
width: 500px;
padding: 20px;
&.bip-flash-error {
background-color: red;
}
&.bip-flash-success {
background-color: green;
}
border: solid 1px #dadada;
border-radius: 6px;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
-webkit-box-shadow: 4px 4px 15px 0px rgba(0, 0, 0, 0.75);
-moz-box-shadow: 4px 4px 15px 0px rgba(0, 0, 0, 0.75);
box-shadow: 4px 4px 15px 0px rgba(0, 0, 0, 0.75);
&:first-letter {
text-transform: uppercase
}
;
font-size: 20px;
color: white;
font-weight: bolder;
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}

Since you are responding with JSON, there is no flash message. Flashes are on HTML pages (for which you can user flash[:message] = "Success! Hooray!"), but there is no place from them to display on a JSON request.

Related

How to Save a page assembled using GrapeJS to a database using ASP.NET MVC

Well, I am developing my own Content Management System using ASP.NET MVC and for a page builder I've decided to use GrapeJS. Now, since this is new to me, I can't seem to find a way to save the pages I would have assembled using GrapeJS. I have used the gjs-preset-webpage GrapeJS plugin and the only JavaScript for the assembling area I have is as shown below:
<script type="text/javascript">
var editor = grapesjs.init({
height: '100%',
showOffsets: 1,
noticeOnUnload: 0,
storageManager: { autoload: 0 },
container: '#gjs',
fromElement: true,
plugins: ['gjs-preset-webpage'],
pluginsOpts: {
'gjs-preset-webpage': {}
}
});
</script>
The HTML I've used is as follows as well:
<div id="gjs" style="height:0px; overflow:hidden">
<div class="panel">
<h1 class="welcome">Welcome to</h1>
<div class="big-title">
<svg class="logo" viewBox="0 0 100 100">
<path d="M40 5l-12.9 7.4 -12.9 7.4c-1.4 0.8-2.7 2.3-3.7 3.9 -0.9 1.6-1.5 3.5-1.5 5.1v14.9 14.9c0 1.7 0.6 3.5 1.5 5.1 0.9 1.6 2.2 3.1 3.7 3.9l12.9 7.4 12.9 7.4c1.4 0.8 3.3 1.2 5.2 1.2 1.9 0 3.8-0.4 5.2-1.2l12.9-7.4 12.9-7.4c1.4-0.8 2.7-2.2 3.7-3.9 0.9-1.6 1.5-3.5 1.5-5.1v-14.9 -12.7c0-4.6-3.8-6-6.8-4.2l-28 16.2" />
</svg>
<span>GrapesJS</span>
</div>
<div class="description">
This is a demo content from index.html. For the development, you shouldn't edit this file, instead you can
copy and rename it to _index.html, on next server start the new file will be served, and it will be ignored by git.
</div>
</div>
<style>
.panel {
width: 90%;
max-width: 700px;
border-radius: 3px;
padding: 30px 20px;
margin: 150px auto 0px;
background-color: #d983a6;
box-shadow: 0px 3px 10px 0px rgba(0,0,0,0.25);
color: rgba(255,255,255,0.75);
font: caption;
font-weight: 100;
}
.welcome {
text-align: center;
font-weight: 100;
margin: 0px;
}
.logo {
width: 70px;
height: 70px;
vertical-align: middle;
}
.logo path {
pointer-events: none;
fill: none;
stroke-linecap: round;
stroke-width: 7;
stroke: #fff
}
.big-title {
text-align: center;
font-size: 3.5rem;
margin: 15px 0;
}
.description {
text-align: justify;
font-size: 1rem;
line-height: 1.5rem;
}
</style>
</div>
Now that I have embedded it into my project and can assembled pages using it, I can't seem to find how to save or where would the assembled page be to be saved. Can anyone help me?
You need a endpoint to allow grapes' storage manager to send your site current status information. I save the json and html just in case, but I think json is enough. Well then you setup your storage manager.
const editor = grapesjs.init({
storageManager:{
type: 'remote',
autosave: true, // Store data automatically
urlStore: 'YOUR_ENDPOINT_URL',
}
});
You will get a call to your endpoint every time something changes with the following parameterts:
gjs-assets: Assets array
gjs-components: Object with markup definition of your site
gjs-styles: Object with styles definition
gjs-html: your site HTML
gjs-css: your CSS
Just be sure to inject this definitions when initialising grapes as well:
const editor = grapesjs.init({
components: "YOUR_STORED_COMPONENTS_HERE",
style: "YOUR_STORED_CSS_HERE",
storageManager:{
type: 'remote',
autosave: true, // Store data automatically
urlStore: 'YOUR_ENDPOINT_URL',
}
});
For further information: Docs

MVC ListBoxFor multiselection without ctrl button

I have a listboxfor in my application which works greats. But to make multiple selection I need to press ctrl and click on the item. How can I change this behavior, so every time I click on the item in the list it selects it and when press again it deselects but only one of the selected items.
My listbox:
#Html.ListBoxFor(m => m.selectedCharts, new SelectList(#Model.Graphs.ToList() )
, new { #class = "select_change" }
)
Or maybe I should use different control for this purpose?
Many thanks
I've used Bootstrap Multiselect before, with great success.
If it cannot be a drop down, use this Checkbox list instead.
I have managed to complete a nice looking listbox where user don't need to press ctrl button to select multiple elements. I want to share with you my work. I have used suggested by MartinHN an amazing extension CheckBoxListFor. You can find it on this website MvcCheckBoxList.
My View Code
#{
var htmlListInfo = new HtmlListInfo(HtmlTag.vertical_columns, 0 , new { #class="styled_list" });
#Html.CheckBoxListFor(model => model.ReportSettings.Ids,
model => model.AvailableGraphs,
graph => graph.Id,
graph => graph.Name,
model => model.SelectedGraphs,
new { #class="styled_checkbox" },
htmlListInfo,
null,
x => x.Name)
}
My CSS:
input.styled_checkbox, input[type="checkbox"]
{
visibility: hidden;
width: 0px;
}
.styled_list
{
background: #aeeefb;
border-radius:5px;
padding: 10px 10px 10px 0;
color: White;
font-weight: bold;
}
.styled_checkbox
{
background: #69D2E7;
cursor: pointer;
display: inline-block;
vertical-align: middle;
margin: 3px 0 3px 0;
padding: 5px;
position: relative;
width: 250px;
border-radius:5px;
}
.styled_checkbox:hover
{
opacity: 0.7;
}
.styled_checkbox:checked+label
{
background: #1a9eed;
}
The check boxes are hidden and labels indicates that element is selected or not. I can't insert pictures but it looks good, but you can off course edit style to your own needs.
Hope that helps anyone

creating tags in textarea using jquery

i want to create tags for input data.(http://textextjs.com/manual/examples/ajax-with-filter-tags-and-autocomplete.html hear they creating tags using auto complete text box, but i don't want auto complete one)
hear is my code
<!DOCTYPE html>
<html>
<head>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#textBox").keyup(function() {
$("#message").val($(this).val());
});
});
</script>
</head>
<body>
<div>
TextBox 1 : <input type="textbox" id="textBox"></input>
TextBox 2 : <input type="textarea" id="message"></input>
</div>
</body>
</html>
hear it reflect data of textbox1 to textbox2.
now what i want is : if user enter any data(words) in textbox1 followed by space then that word should convert into tags in textbox2
First of all type=textarea is wrong. There's no such input like that. You must be using <textarea> instead of that. Secondly, why dont you use contentditable attribute? It works just like a text area but can take HTML, is supported in all browsers, and you can use it on any block element! So replace your second input with this:
TextBox 2 : <div class="target" contenteditable="true"></div>
Then, in your code,
$("#textBox").keypress(function (e) {
if (e.which === 32) {
$(".target").append("<a href='#' class='tag'>" + this.value + "</a>");
this.value = "";
}
});
(Disclaimer) I used the styles from SO's tags, like this :
body {
font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;
}
.tag {
color: #3E6D8E;
background-color: #E0EAF1;
border-bottom: 1px solid #b3cee1;
border-right: 1px solid #b3cee1;
padding: 3px 4px 3px 4px;
margin: 2px 2px 2px 0;
text-decoration: none;
font-size: 90%;
line-height: 2.4;
white-space: nowrap;
}
.tag:hover {
background-color: #c4dae9;
border-bottom: 1px solid #c4dae9;
border-right: 1px solid #c4dae9;
text-decoration: none;
}
Demo : http://jsfiddle.net/hungerpain/Wky2Z/
To add the tags to an array, have a variable called tags outside the keypress function :
var tags = [];
Then, in the keypress, you've got this if loop right? Push the new value into the array :
if (e.which === 32) {
$(".target").append("<a href='#' class='tag'>" + this.value + "</a>");
tags.push(this.value); //push the value in array
this.value = "";
}
Then, when you need to save it to DB, just join them :
tags.join("");
Then later, when you to retrieve them from DB next time, you could wrap those with the a (what we did in the keypress function)
Demo : http://jsfiddle.net/hungerpain/Wky2Z/1/
Try This:
$(document).ready(function () {
$("#textBox").keyup(function (e) {
if (e.keyCode == 32) {
$("#message").val($(this).val());
}
if ($(this).val() == '') {
$("#message").val('');
}
});
});
JSFIDDLE DEMO

How to create a printable Twitter-Bootstrap page

I'm using Twitter-Bootstrap and I need to be able to print the page the way it looks on the browser. I'm able to print other pages made with Twitter-Bootstrap just fine but I can't seem to print my page that uses purely Twitter-Bootstrap. Am I missing a tag somewhere?
Official TB page when printed:
My page when printed:
What my page actually looks like:
Bootstrap 3.2 update: (current release)
Current stable Bootstrap version is 3.2.0.
With version 3.2 visible-print deprecated, so you should use like this:
Class Browser Print
-------------------------------------------------
.visible-print-block Hidden Visible (as block)
.visible-print-inline Hidden Visible (as inline)
.visible-print-inline-block Hidden Visible (as inline-block)
.hidden-print Visible Hidden
Bootstrap 3 update:
Print classes are now in documents: http://getbootstrap.com/css/#responsive-utilities-print
Similar to the regular responsive classes,
use these for toggling content for print.
Class Browser Print
----------------------------------------
.visible-print Hidden Visible
.hidden-print Visible Hidden
Bootstrap 2.3.1 version:
After adding bootstrap.css file into your HTML,
Find the parts that you don't want to print and add hidden-print class into tags.
Because css file includes this:
#media print {
.visible-print { display: inherit !important; }
.hidden-print { display: none !important; }
}
Be sure to have a stylesheet assigned for printing.
It could be a separate stylesheet:
<link rel="stylesheet" type="text/css" media="print" href="print.css">
or one you share for all devices:
<link rel="stylesheet" type="text/css" href="bootstrap.min.css"> # Note there's no media attribute
Then, you can write your styles for printers in the separate stylesheets or in the shared one using media queries:
#media print {
/* Your styles here */
}
Replace every col-md- with col-xs-
eg: replace every col-md-6 to col-xs-6.
This is the thing that worked for me to get me rid of this problem you can see what you have to replace.
There's a section of #media print code in the css file (Bootstrap 3.3.1 [UPDATE:] to 3.3.5), this strips virtually all the styling, so you get fairly bland print-outs even when it is working.
For now I've had to resort to stripping out the #media print section from bootstrap.css - which I'm really not happy about but my users want direct screen-grabs so this'll have to do for now. If anyone knows how to suppress it without changes to the bootstrap files I'd be very interested.
Here's the 'offending' code block, starts at line #192:
#media print {
*,
*:before,enter code here
*:after {
color: #000 !important;
text-shadow: none !important;
background: transparent !important;
-webkit-box-shadow: none !important;
box-shadow: none !important;
}
a,
a:visited {
text-decoration: underline;
}
a[href]:after {
content: " (" attr(href) ")";
}
abbr[title]:after {
content: " (" attr(title) ")";
}
a[href^="#"]:after,
a[href^="javascript:"]:after {
content: "";
}
pre,
blockquote {
border: 1px solid #999;
page-break-inside: avoid;
}
thead {
display: table-header-group;
}
tr,
img {
page-break-inside: avoid;
}
img {
max-width: 100% !important;
}
p,
h2,
h3 {
orphans: 3;
widows: 3;
}
h2,
h3 {
page-break-after: avoid;
}
select {
background: #fff !important;
}
.navbar {
display: none;
}
.btn > .caret,
.dropup > .btn > .caret {
border-top-color: #000 !important;
}
.label {
border: 1px solid #000;
}
.table {
border-collapse: collapse !important;
}
.table td,
.table th {
background-color: #fff !important;
}
.table-bordered th,
.table-bordered td {
border: 1px solid #ddd !important;
}
}
Best option I found was http://html2canvas.hertzen.com/
http://jsfiddle.net/nurbsurf/1235emen/
html2canvas(document.body, {
onrendered: function(canvas) {
$("#page").hide();
document.body.appendChild(canvas);
window.print();
$('canvas').remove();
$("#page").show();
}
});
In case someone is looking for a solution for Bootstrap v2.X.X here. I am leaving the solution I was using. This is not fully tested on all browsers however it could be a good start.
1) make sure the media attribute of bootstrap-responsive.css is screen.
<link href="/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen" />
2) create a print.css and make sure its media attribute print
<link href="/css/print.css" rel="stylesheet" media="print" />
3) inside print.css, add the "width" of your website in html & body
html,
body {
width: 1200px !important;
}
4.) reproduce the necessary media query classes in print.css because they were inside bootstrap-responsive.css and we have disabled it when printing.
.hidden{display:none;visibility:hidden}
.visible-phone{display:none!important}
.visible-tablet{display:none!important}
.hidden-desktop{display:none!important}
.visible-desktop{display:inherit!important}
Here is full version of print.css:
html,
body {
width: 1200px !important;
}
.hidden{display:none;visibility:hidden}
.visible-phone{display:none!important}
.visible-tablet{display:none!important}
.hidden-desktop{display:none!important}
.visible-desktop{display:inherit!important}
2 things FYI -
For now, they've added a few toggle classes. See what's available in the latest stable release - print toggles in responsive-utilities.less
New and improved solution coming in Bootstrap 3.0 - they're adding a separate print.less file. See separate print.less
To make print view look like tablet or desktop include bootstrap as .less, not as .css and then you can overwrite bootstrap responsive classes in the end of bootstrap_variables file for example like this:
#container-sm: 1200px;
#container-md: 1200px;
#container-lg: 1200px;
#screen-sm: 0;
Don't worry about putting this variables in the end of the file. LESS supports lazy loading of variables so they will be applied.
If you want to keep columns on A4 print (which is around 540px) this is a good idea
#media print {
.make-grid(print-A4);
}
.make-print-A4-column(#columns) {
#media print {
float: left;
width: percentage((#columns / #grid-columns));
}
}
You can use it like this:
<div class="col-sm-4 col-print-A4-4">

Jquery RemoveClass not working for bootstrap buttons

I am having trouble using the Jquery removeClass functionality to disable a certain class. The class definition is given below
.btn-purple
{
background-color: #F1B2E1;
background-image: -moz-linear-gradient(top, #F1B2E1, #E986D0);
background-image: -ms-linear-gradient(top, #F1B2E1, #E986D0);
background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#F1B2E1), to(#E986D0));
background-image: -webkit-linear-gradient(top, #F1B2E1, #E986D0);
background-image: -o-linear-gradient(top, #F1B2E1, #E986D0);
background-image: linear-gradient(top, #F1B2E1, #E986D0);
background-repeat: repeat-x;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#F1B2E1', endColorstr='#E986D0', GradientType=0);
border-color: #0055cc #0055cc #003580;
border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
filter: progid:dximagetransform.microsoft.gradient(enabled=false);
}
.btn-purple:hover,
.btn-purple:active,
.btn-purple.active,
.btn-purple.disabled,
.btn-purple[disabled] {
background-color: #E986D0;
}
In the javascript file I have the following
$(function(){
$("#1").click(function(){
$("#1").removeClass("btn-purple", 1000);
return false
});
});
And here is the HTML markup for the section I wish to remove the class definition from
<div id="1" class="span3 well homeBox opaque btn-purple">
<span class="siteBoxContent">What do we do?</span>
</div>
And here is the error I'm getting while debugging in the Webkit Error Console
TypeError: 'undefined' is not an object (evaluating 'fx.end[0]') jquery.effects.core.js:32
If I try to remove a class like 'homeBox', the removeClass works fine. Here is the definition for the homeBox class definition
.homeBox
{
position:relative;
width:26.666666666667%;
height:200px;
}
So I'm guessing jQuery may have a problem with removing classes which have background-image, colour or even the filter attributes. If you require any more information, please let me know. Thanks in advance for your help.
Use:
$("#1").removeClass("btn-purple");
Instead of:
$("#1").removeClass("btn-purple", 1000);
since there is not second argument available for it and 1000 doesn't make any sense there.
Two possible signatures of it are:
.removeClass( [className] )
.removeClass( function(index, class) )
More Info:
http://api.jquery.com/removeClass/
You use a function signature that doesn't exist:
removeClass("classname", integer) // WRONG
There's no such possibility in JQuery AFAIK, you can do:
.removeClass( [className] )
.removeClass( function(index, class) ) // from version 1.4
I don't understand what 1000 stands for, but I bet that removing it should fix the issue.

Resources