Skip to content

Commit

Permalink
Merge pull request #2 from pedm/master
Browse files Browse the repository at this point in the history
interest tags
  • Loading branch information
Evan David Light committed Aug 29, 2011
2 parents ea4a86a + 1ca783b commit aa516e5
Show file tree
Hide file tree
Showing 7 changed files with 604 additions and 2 deletions.
412 changes: 412 additions & 0 deletions app/assets/javascripts/jquery-ui.min.js

Large diffs are not rendered by default.

139 changes: 139 additions & 0 deletions app/assets/javascripts/jquery.tagify.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,139 @@
/* Author: Alicia Liu */

(function ($) {

$.widget("ui.tagify", {
options: {
delimiters: [13, 188], // what user can type to complete a tag in char codes: [enter], [comma]
outputDelimiter: ',', // delimiter for tags in original input field
cssClass: 'tagify-container', // CSS class to style the tagify div and tags, see stylesheet
addTagPrompt: 'add tags' // placeholder text
},

_create: function() {
var self = this,
el = self.element,
opts = self.options;

this.tags = [];

// hide text field and replace with a div that contains it's own input field for entering tags
this.tagInput = $("<input type='text'>")
.attr( 'placeholder', opts.addTagPrompt )
.keypress( function(e) {
var $this = $(this),
pressed = e.which;

for ( i in opts.delimiters ) {

if (pressed == opts.delimiters[i]) {
self.add( $this.val() );
return false;
}
}

// if backspace is hit with no input, remove the last tag
if (pressed == 8) { // backspace
if ( $this.val() == "" ) {
self.remove();
return false;
}
return;
}

});

this.tagDiv = $("<div></div>")
.addClass( opts.cssClass )
.click( function() {
$(this).children('input').focus();
})
.append( this.tagInput )
.insertAfter( el.hide() );

// if the field isn't empty, parse the field for tags, and prepopulate existing tags
var initVal = el.val();

if ( initVal ) {
var initTags = initVal.split( opts.outputDelimiter );
$.each( initTags, function(i, tag) {
self.add( tag );
});
}
},

_setOption: function( key, value ) {
options.key = value;
},

// add a tag, public function
add: function(text) {
var self = this;
text = text || self.tagInput.val();
if (text) {
var self = this;
var tagIndex = self.tags.length;

var removeButton = $("<a href='#'>x</a>")
.click( function() {
self.remove( tagIndex );
return false;
});
var newTag = $("<span></span>")
.text( text )
.append( removeButton );

self.tagInput.before( newTag );
self.tags.push( text );
self.tagInput.val('');
}
},

// remove a tag by index, public function
// if index is blank, remove the last tag
remove: function( tagIndex ) {
var self = this;
if ( tagIndex == null || tagIndex === (self.tags.length - 1) ) {
this.tagDiv.children("span").last().remove();
self.tags.pop();
}
if ( typeof(tagIndex) == 'number' ) {
// otherwise just hide this tag, and we don't mess up the index
this.tagDiv.children( "span:eq(" + tagIndex + ")" ).hide();
// we rely on the serialize function to remove null values
delete( self.tags[tagIndex] );
}
},

// serialize the tags with the given delimiter, and write it back into the tagified field
serialize: function() {
var self = this;
var delim = self.options.outputDelimiter;
var tagsStr = self.tags.join( delim );

// our tags might have deleted entries, remove them here
var dupes = new RegExp(delim + delim + '+', 'g'); // regex: /,,+/g
var ends = new RegExp('^' + delim + '|' + delim + '$', 'g'); // regex: /^,|,$/g
var outputStr = tagsStr.replace( dupes, delim ).replace(ends, '');

self.element.val(outputStr);
return outputStr;
},

inputField: function() {
return this.tagInput;
},

containerDiv: function() {
return this.tagDiv;
},

// remove the div, and show original input
destroy: function() {
$.Widget.prototype.destroy.apply(this);
this.tagDiv.remove();
this.element.show();
},
});

})(jQuery);
36 changes: 36 additions & 0 deletions app/assets/stylesheets/tagify-style.css
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Tagify styles
Author: Alicia Liu
*/

.tagify-container {
width: 300px;
height: 100px;
border: 1px solid #CCC;
background-color: #FFF;
border-radius: 2px;
font-family: Verdana, sans-serif;
font-size: 12px;
overflow: auto;
}
.tagify-container > span {
display: inline-block;
padding: 3px 5px;
margin: 3px;
border-radius: 2px;
border: 1px solid #EEE;
background-color: #FFEBA1;
color: #888;
}
.tagify-container > span > a {
padding-left: 5px;
color: #EB7E14;
text-decoration: none;
font-weight: bold;
}
.tagify-container > input {
border: 0 none;
width: 100px;
}
.tagify-container > input:focus {
outline: none;
}
3 changes: 3 additions & 0 deletions app/helpers/profiles_helper.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,2 +1,5 @@
module ProfilesHelper module ProfilesHelper
def link_terms_to_search(term)
link_to term, :controller => "home", :action => "search", :utf8 => "&x2713;", :query => "#{term.split(" ").map{|x| x.strip}.join("+")}"
end
end end
6 changes: 6 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
<head> <head>
<title><%= yield(:title) || "RubyPair.com" %></title> <title><%= yield(:title) || "RubyPair.com" %></title>
<%= stylesheet_link_tag :application %> <%= stylesheet_link_tag :application %>
<%= stylesheet_link_tag :"tagify-style" %>

<!--[if IE]> <!--[if IE]>
<link href="/stylesheets/ie.css" media="screen, projection" rel="stylesheet" type="text/css" /> <link href="/stylesheets/ie.css" media="screen, projection" rel="stylesheet" type="text/css" />
<![endif]--> <![endif]-->

<%= javascript_include_tag :application %> <%= javascript_include_tag :application %>
<%= csrf_meta_tag %> <%= csrf_meta_tag %>
<%= yield(:head) %> <%= yield(:head) %>
</head> </head>
Expand Down Expand Up @@ -39,5 +44,6 @@
<%= yield %> <%= yield %>
</div> </div>
</div> </div>

</body> </body>
</html> </html>
7 changes: 6 additions & 1 deletion app/views/users/edit.html.haml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
= f.label :remote_local_preference, "Pairing in person, remotely, or both?" = f.label :remote_local_preference, "Pairing in person, remotely, or both?"
= f.select :remote_local_preference, User::REMOTE_LOCAL_PREFERENCES = f.select :remote_local_preference, User::REMOTE_LOCAL_PREFERENCES
%p %p
= f.label :interests, "Describe your pairing interests" = f.label :interests, "Describe your pairing interests (click enter after each tag)"
= f.text_area :interests = f.text_area :interests
%p %p
= f.submit = f.submit
:javascript
$('#user_interests').tagify({addTagPrompt: 'add tags'});
$('form').submit( function() {
$('#user_interests').tagify('serialize');
});
3 changes: 2 additions & 1 deletion app/views/users/show.html.haml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
= @user.remote_local_preference = @user.remote_local_preference
%li.interests %li.interests
%label Interests %label Interests
= @user.interests - @user.interests.split(",").each do |term|
= link_terms_to_search term

0 comments on commit aa516e5

Please sign in to comment.