Commit 3277f8d4 by tady

select initial post after create/update.

parent 441de0a5
......@@ -22,7 +22,7 @@ gem 'coffee-rails', '~> 4.0.0'
# gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
......@@ -52,6 +52,8 @@ gem 'omniauth-google-oauth2'
gem 'redcarpet'
group :development do
gem "better_errors"
gem "binding_of_caller"
gem 'better_errors'
gem 'binding_of_caller'
gem 'thin'
end
......@@ -42,6 +42,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.6.3)
daemons (1.1.9)
debug_inspector (0.0.2)
devise (3.2.2)
bcrypt-ruby (~> 3.0)
......@@ -50,6 +51,7 @@ GEM
thread_safe (~> 0.1)
warden (~> 1.2.3)
erubis (2.7.0)
eventmachine (1.0.3)
execjs (2.0.2)
faraday (0.8.8)
multipart-post (~> 1.2.0)
......@@ -125,6 +127,10 @@ GEM
activesupport (>= 3.0)
sprockets (~> 2.8)
sqlite3 (1.3.8)
thin (1.6.1)
daemons (>= 1.0.9)
eventmachine (>= 1.0.0)
rack (>= 1.0.0)
thor (0.18.1)
thread_safe (0.1.3)
atomic
......@@ -132,8 +138,6 @@ GEM
treetop (1.4.15)
polyglot
polyglot (>= 0.3.1)
turbolinks (1.3.1)
coffee-rails
tzinfo (0.3.38)
uglifier (2.3.1)
execjs (>= 0.3.0)
......@@ -156,5 +160,5 @@ DEPENDENCIES
sass-rails (~> 4.0.0)
sdoc
sqlite3
turbolinks
thin
uglifier (>= 1.3.0)
......@@ -12,80 +12,9 @@
//
// require jquery
// require jquery_ujs
//= require turbolinks
// require turbolinks
//= require_tree ./lib
//= require_tree .
// Automaticaly change textarea height.
$(function(){
'use strict';
$('textarea.autosize').autosize();
// Preview post.
var load_preview = function(){
var text = $('#post_body').val();
var csrfToken = $("meta[name='csrf-token']").attr('content');
$.post('/posts/preview.api', {
'text': text,
'authenticity_token': csrfToken
})
.done(function(data){
$('#post_preview').html(data);
});
};
$('#post_body').on('keyup mouseup', load_preview);
load_preview();
// post list
$('.post-list').on('click', function(e){
e.preventDefault();
var $this = $(this);
$this.siblings().removeClass('active');
$this.addClass('active');
var id = $this.data('postId');
$.get('/posts/show_fragment', {
'id': id,
})
.done(function(data){
$('#list_post').html(data);
});
});
// search form
$('#app-search-form input').on('focus', function(){
$(this).parents('#app-search-form').animate({width: '600px'});
}).on('blur', function(){
$(this).parents('#app-search-form').animate({width: '200px'});
});
// disable tab key
$('.disable-tab').on('keydown', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 9) {
e.preventDefault();
var start = $(this).get(0).selectionStart;
var end = $(this).get(0).selectionEnd;
// set textarea value to: text before caret + tab + text after caret
$(this).val($(this).val().substring(0, start) +
'\t' +
$(this).val().substring(end));
// put caret at right position again
$(this).get(0).selectionStart =
$(this).get(0).selectionEnd = start + 1;
}
});
// new post tags
$('#post_tags').select2();
});
......@@ -91,8 +91,10 @@
font-weight: bold;
font-size: 25px;
}
.input-group-addon label {
margin: 0;
.input-group-addon {
label {
margin: 0;
}
}
#post_body {
......
......@@ -52,6 +52,7 @@ class PostsController < ApplicationController
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to root_path(id: @post.id), notice: 'Post was successfully updated.' }
......@@ -81,6 +82,18 @@ class PostsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :body)
@post_params ||= begin
_param_hash = params.require(:post).permit(:title, :body, :tags).to_hash
# tags_text == 'Javascript,Ruby'
tags_text = _param_hash.delete('tags')
tags = tags_text.split(',').map do |_tag_name|
Tag.find_or_create_by(name: _tag_name)
end
_param_hash["tag_ids"] = tags.map(&:id)
_param_hash
end
end
end
......@@ -29,9 +29,83 @@
<hr>
<footer>
<p>&copy; tady</p>
</footer>
</div><!--/.container-->
</div>
<% content_for :footer_js do %>
<script type="text/javascript">
$(function(){
'use strict';
// Preview post.
var load_preview = function(){
var text = $('#post_body').val();
var csrfToken = $("meta[name='csrf-token']").attr('content');
$.post('/posts/preview.api', {
'text': text,
'authenticity_token': csrfToken
})
.done(function(data){
$('#post_preview').html(data);
});
};
$('#post_body').on('keyup mouseup', load_preview);
// load_preview();
// post list
$('.post-list').on('click', function(e){
e.preventDefault();
var $this = $(this);
$this.siblings().removeClass('active');
$this.addClass('active');
var id = $this.data('postId');
$.get('/posts/show_fragment', {
'id': id,
})
.done(function(data){
$('#list_post').html(data);
});
});
// initial post selection.
(function(){
// open post when `id` parameter set.
// /?a=b&c=123 ==> {"a": "b", "c": "123"}
var getQueryParams = function() {
var query = location.search.split("+").join(" "),
params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(query)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}
return params;
}
var id_param = getQueryParams()["id"];
if(id_param) {
$(".post-list[data-post-id='" + id_param + "']").addClass('active');
$.get('/posts/show_fragment', {
'id': id_param,
})
.done(function(data){
$('#list_post').html(data);
});
}
})();
// search form
$('#app-search-form input').on('focus', function(){
$(this).parents('#app-search-form').animate({width: '600px'});
}).on('blur', function(){
$(this).parents('#app-search-form').animate({width: '200px'});
});
});
</script>
<% end %>
......@@ -3,7 +3,7 @@
<head>
<title>Rendezvous</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= stylesheet_link_tag "application", media: "all" %>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<!-- Optional theme -->
......@@ -21,7 +21,8 @@
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application" %>
<%= yield :footer_js %>
</body>
</html>
......@@ -25,15 +25,7 @@
<div class="field">
<div class="input-group">
<span class="input-group-addon"><%= f.label :tags %></span>
<select id="post_tags" multiple>
<% Tag.all.each do |tag| %>
<% if @post.tag_ids.include?(tag.id) %>
<option value="<%= tag.name %>" selected><%= tag.name %></option>
<% else %>
<option value="<%= tag.name %>"><%= tag.name %></option>
<% end %>
<% end %>
</select>
<%= hidden_field :post, :tags, id: 'post_tags', style: 'width:300px', value: @post.tags.pluck(:name).join(',') %>
</div>
</div>
</div>
......@@ -71,5 +63,41 @@
</div><!--/row-->
<% end %>
<% content_for :footer_js do %>
<script type="text/javascript">
$(function(){
'use strict';
// Automaticaly change textarea height.
$('textarea.autosize').autosize();
// disable tab key
$('.disable-tab').on('keydown', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 9) {
e.preventDefault();
var start = $(this).get(0).selectionStart;
var end = $(this).get(0).selectionEnd;
// set textarea value to: text before caret + tab + text after caret
$(this).val($(this).val().substring(0, start) +
'\t' +
$(this).val().substring(end));
// put caret at right position again
$(this).get(0).selectionStart =
$(this).get(0).selectionEnd = start + 1;
}
});
// new post tags
$('#post_tags').select2({
tags: <%= raw Tag.all.pluck(:name).to_json %>
});
});
</script>
<% end %>
......@@ -11,7 +11,6 @@
<hr>
<footer>
<p>&copy; tady</p>
</footer>
</div>
......
......@@ -11,7 +11,6 @@
<hr>
<footer>
<p>&copy; tady</p>
</footer>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment