Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding time type #508

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions lib/assets/javascripts/best_in_place.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,91 @@ BestInPlaceEditor.forms = {
}
}
},
"time": {
activateForm: function () {
'use strict';
var output = jQuery(document.createElement('form'))
.addClass('form_in_place')
.attr('action', 'javascript:void(0);')
.attr('style', 'display:inline');
var input_elt = jQuery(document.createElement('input'))
.attr('type', 'time')
.attr('name', this.attributeName)
.val(this.display_value);
// Add class to form input
if (this.inner_class) {
input_elt.addClass(this.inner_class);
}

output.append(input_elt);
this.placeButtons(output, this);

this.element.html(output);
this.setHtmlAttributes();

this.element.find("input[type='time']")[0].select();
this.element.find("form").bind('submit', {editor: this}, BestInPlaceEditor.forms.input.submitHandler);
if (this.cancelButton) {
this.element.find("input[type='button']").bind('click', {editor: this}, BestInPlaceEditor.forms.input.cancelButtonHandler);
}
if (!this.okButton) {
this.element.find("input[type='time']").bind('blur', {editor: this}, BestInPlaceEditor.forms.input.inputBlurHandler);
}
this.element.find("input[type='time']").bind('keyup', {editor: this}, BestInPlaceEditor.forms.input.keyupHandler);
this.blurTimer = null;
this.userClicked = false;
},

getValue: function () {
'use strict';
return this.sanitizeValue(this.element.find("input").val());
},

// When buttons are present, use a timer on the blur event to give precedence to clicks
inputBlurHandler: function (event) {
'use strict';
if (event.data.editor.okButton) {
event.data.editor.blurTimer = setTimeout(function () {
if (!event.data.editor.userClicked) {
event.data.editor.abort();
}
}, 500);
} else {
if (event.data.editor.cancelButton) {
event.data.editor.blurTimer = setTimeout(function () {
if (!event.data.editor.userClicked) {
event.data.editor.update();
}
}, 500);
} else {
event.data.editor.update();
}
}
},

submitHandler: function (event) {
'use strict';
event.data.editor.userClicked = true;
clearTimeout(event.data.editor.blurTimer);
event.data.editor.update();
},

cancelButtonHandler: function (event) {
'use strict';
event.data.editor.userClicked = true;
clearTimeout(event.data.editor.blurTimer);
event.data.editor.abort();
event.stopPropagation(); // Without this, click isn't handled
},

keyupHandler: function (event) {
'use strict';
if (event.keyCode === 27) {
event.data.editor.abort();
event.stopImmediatePropagation();
}
}
},

"select": {
activateForm: function () {
Expand Down
1 change: 1 addition & 0 deletions lib/best_in_place/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def best_in_place(object, field, opts = {})
options[:data]['bip-cancel-button'] = opts[:cancel_button].presence
options[:data]['bip-cancel-button-class'] = opts[:cancel_button_class].presence
options[:data]['bip-original-content'] = html_escape(opts[:value] || value).presence
options[:data]['bip-original-content'] = options[:data]['bip-original-content'].try(:to_time, :utc).try(:strftime, "%H:%M") if opts[:as] == :time

options[:data]['bip-skip-blur'] = opts.has_key?(:skip_blur) ? opts[:skip_blur].presence : BestInPlace.skip_blur

Expand Down
19 changes: 19 additions & 0 deletions spec/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,25 @@
end
end

context "with a time attribute" do
before do
nk = Nokogiri::HTML.parse(helper.best_in_place @user, :birth_date, as: :time)
@span = nk.css("span")
end

it "should render the time as text" do
expect(@span.text).to eq(@user.birth_date.to_date.to_s)
end

it "should have a time data-bip-type" do
expect(@span.attribute("data-bip-type").value).to eq("time")
end

it "should have no data-bip-collection" do
expect(@span.attribute("data-bip-collection")).to be_nil
end
end

context "with a boolean attribute" do
before do
nk = Nokogiri::HTML.parse(helper.best_in_place @user, :receive_email, as: :checkbox)
Expand Down