Skip to content

Commit

Permalink
card now persist their order just like milestones
Browse files Browse the repository at this point in the history
  • Loading branch information
rauhryan committed Feb 10, 2012
1 parent fb9ccf6 commit 34ec134
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 9 deletions.
5 changes: 5 additions & 0 deletions lib/api.rb
Expand Up @@ -38,6 +38,11 @@ class API < Sinatra::Base
return json pebble.board(params[:user], params[:repo])
end

post '/:user/:repo/reorderissue' do
milestone = params["issue"]
json pebble.reorder_issue params[:user], params[:repo], milestone["number"], params[:index]
end

post '/:user/:repo/reordermilestone' do
milestone = params["milestone"]
json pebble.reorder_milestone params[:user], params[:repo], milestone["number"], params[:index]
Expand Down
9 changes: 9 additions & 0 deletions public/scripts/modules/board/models/card.js
Expand Up @@ -22,6 +22,15 @@ define(function(){
}, function (response) {
console.log("closed issue", data.index);
});
},
reorder: function(data) {
$.post("/api/" + this.user + "/" + this.repo + "/reorderissue",{
index: data.order,
issue: this.attributes
}, function (response) {
console.log("reordered issue", data.order);
});

}
});

Expand Down
9 changes: 7 additions & 2 deletions public/scripts/modules/board/views/cardView.js
Expand Up @@ -4,16 +4,18 @@ define(["text!../templates/card.html","../models/card", "../events/postal"],func
initialize: function ( params ) {
this.issue = new card({model:params.issue, user:params.user,repo: params.repo});
_.bind(this,'moved',this.moved);
_.bind(this,'drop',this.drop);
postal.subscribe("Filter.*", $.proxy(this.filter, this));
},
events: {
"moved" : "moved",
"click .milestone": "publishFilter",
"click .close": "closed"
"click .close": "closed",
"drop" : "drop"
},
tagName:"li",
render: function(){
$(this.el).html( _.template(template, this.issue.attributes)).addClass("drop-shadow");
$(this.el).html( _.template(template, this.issue.attributes)).addClass("drop-shadow").data("issue",this.issue.attributes);
return this;
},
moved: function(ev,index){
Expand All @@ -32,6 +34,9 @@ define(["text!../templates/card.html","../models/card", "../events/postal"],func
},
filter: function (shouldFilter) {
$(this.el).toggle(shouldFilter(this.issue.attributes));
},
drop: function(ev,order){
this.issue.reorder({order:order});
}
});

Expand Down
43 changes: 43 additions & 0 deletions public/scripts/modules/board/views/columnView.js
Expand Up @@ -5,6 +5,7 @@ define(["text!../templates/column.html","./cardView"],function(template, CardVie
this.column = params.column;
this.repo = params.repo;
this.user = params.user;
this.latched = false;
},
render: function(){
var column = $(_.template(template, this.column)),
Expand All @@ -23,6 +24,7 @@ define(["text!../templates/column.html","./cardView"],function(template, CardVie
receive: $.proxy(this.onReceive,this),
remove: $.proxy(this.onRemove, this),
over: $.proxy(this.onOver, this),
update: $.proxy(this.onStop, this),
out: $.proxy(this.onOut, this)
});

Expand All @@ -39,6 +41,47 @@ define(["text!../templates/column.html","./cardView"],function(template, CardVie
},
onOut: function (ev, ui){
$("ul",this.el).removeClass("ui-sortable-hover");
},
onStop : function(ev,ui){
var elements = $("li", this.el),
index = elements.index(ui.item);

if(index === -1) { return; }

var first = index === 0,
last = index === elements.size() - 1,
currentElement = $(ui.item),
currentData = currentElement.data("issue"),
beforeElement = elements.get(index ? index - 1 : index),
beforeIndex = elements.index(beforeElement),
beforeData = $(beforeElement).data("issue"),
afterElement = elements.get(elements.size() - 1 > index ? index + 1 : index),
afterIndex = elements.index(afterElement),
afterData = $(afterElement).data("issue"),
current = currentData._data.order || currentData.number,
before = beforeData._data.order || beforeData.number,
after = afterData._data.order || afterData.number;

if(first && last) {return;}

if(first) {
// dragged it to the top
currentData._data.order = (after || 1)/2;
currentElement
.trigger("drop", currentData._data.order)
.data("issue", currentData);
} else if (last) {
// dragged to the bottom
currentData._data.order = (before + 1);
currentElement
.trigger("drop", currentData._data.order)
.data("issue", currentData);
} else {
currentData._data.order = (((after + before) || 1)/2);
currentElement
.trigger("drop", currentData._data.order)
.data("issue", currentData);
}
}
});
return Column;
Expand Down
4 changes: 3 additions & 1 deletion public/scripts/modules/workqueue/views/milestones_list.js
Expand Up @@ -37,6 +37,8 @@ define(["./milestoneView","../collections/milestones"],function(milestoneView,mi
before = beforeData._data.order || beforeData.number,
after = afterData._data.order || afterData.number;

if(first && last) {return;}

if(first) {
// dragged it to the top
currentData._data.order = (after || 1)/2;
Expand All @@ -45,7 +47,7 @@ define(["./milestoneView","../collections/milestones"],function(milestoneView,mi
.data("milestone", currentData);
} else if (last) {
// dragged to the bottom
currentData._data.order = (before + 1)/2;
currentData._data.order = (before + 1);
currentElement
.trigger("drop", currentData._data.order)
.data("milestone", currentData);
Expand Down
28 changes: 22 additions & 6 deletions stint/stint/lib/stint/pebble.rb
Expand Up @@ -10,11 +10,12 @@ def board(user_name, repo)
all_labels = labels(user_name, repo)
all_labels = all_labels.each_with_index do |label, index|
x = issues_by_label[label[:name]]
label[:issues] = (x || []).sort_by{|issue| issue["number"].to_i}
label[:issues] = (x || []).sort_by { |i| i["_data"]["order"] || i["number"].to_f}
label
end

all_labels[0][:issues] = (issues_by_label["__nil__"] || []).concat(all_labels[0][:issues]).sort_by {|x| x["number"].to_i} unless all_labels.empty?
all_labels[0][:issues] = (issues_by_label["__nil__"] || []).concat(all_labels[0][:issues]).sort_by { |i| i["_data"]["order"] || i["number"].to_f} unless all_labels.empty?

{
labels: all_labels,
milestones: milestones(user_name, repo)
Expand All @@ -25,8 +26,23 @@ def get_issues(user_name, repo)
issues = github.get_issues user_name, repo
issues.each do |issue|
issue["current_state"] = current_state(issue)
issue["_data"] = embedded_data issue["body"]
end
issues.sort_by { |i| i["_data"]["order"] || i["number"].to_f}
end

def reorder_issue(user_name, repo, number, index)

post_data = {"number" => number}
issue = github.issue_by_id user_name, repo, number
_data = embedded_data issue["body"]
if _data.empty?
post_data["body"] = issue["body"].concat "\r\n@huboard:#{JSON.dump({:order => index.to_f})}"
else
post_data["body"] = issue["body"].gsub /@huboard:.*/, "@huboard:#{JSON.dump(_data.merge({"order" => index.to_f}))}"
end
issues

github.update_issue user_name, repo, post_data
end

def reorder_milestone(user_name, repo, number, index)
Expand Down Expand Up @@ -161,15 +177,15 @@ def milestones(user_name, repo)
m["pull_requests"] = m[:issues].select {|i| !i["pull_request"]["html_url"].nil?}
m[:issues] = m[:issues].delete_if {|i| !i["pull_request"]["html_url"].nil?}
m["open_issues"] = m[:issues].size
m["_data"] = milestone_data m
m["_data"] = embedded_data m["description"]
m
}
milestones.sort_by { |m| m["_data"]["order"] || m["number"].to_f}
end

def milestone_data(milestone)
def embedded_data(body)
r = /@huboard:(.*)/
match = r.match milestone["description"]
match = r.match body
return { } if match.nil?

JSON.load(match[1])
Expand Down

0 comments on commit 34ec134

Please sign in to comment.