Skip to content

Commit

Permalink
lml#347: Convert native confirm to dialog based confirm.
Browse files Browse the repository at this point in the history
- Add user_settings migration, model and controller.
- Add JS to intercept for confirmation.
- Add dialog HTML.
  • Loading branch information
navilan committed Jun 13, 2014
1 parent 2c65099 commit 3f1dff0
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 11 deletions.
36 changes: 35 additions & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ function open_message_dialog(is_modal, height, width, title, body) {
open_specified_dialog('message', is_modal, height, width, title, body);
}

function open_maybe_confirm_dialog(body) {
open_specified_dialog('maybe_confirm', true, 200, 400, "Are you sure?", body);
}

function get_os_color(color) {
return $('#os_' + color).css('background-color');
}
Expand All @@ -68,9 +72,39 @@ function sum(array) {
return sum;
}

function initMaybeConfirm() {
$("#maybe_confirm_button_cancel").click(function () {
$("#maybe_confirm_button_ok").off();
$("#maybe_confirm_dialog").dialog('close');
});
$('.maybe_confirm').click(function (e) {
var submit = $(e.currentTarget);
var setting = $(e.currentTarget).data('setting');
if (!OSTUserSettings[setting]) {
var message = $(e.currentTarget).data('maybeconfirm');
var check = $("#skip_maybe_confirm_dialog");
check.attr("name", "settings[" + setting + "]");
check.data("setting", setting);
$("#maybe_confirm_button_ok").one('click', function() {
$("#maybe_confirm_dialog").dialog('close');
submit.closest('form').submit();
});
open_maybe_confirm_dialog(message);
return false;
}
return true;
});
$("#skip_maybe_confirm_dialog").click(function() {
// Get value and set.
OSTUserSettings[$(this).data('setting')] = $(this).is(":checked");
$(this).closest('form').submit();
});
}

// Open all non-local links in a new tab/window
// http://stackoverflow.com/questions/4086988/how-do-i-make-link-to-open-external-urls-in-a-new-window
$(document).ready(function() {
initMaybeConfirm();
$("a").on("click", function() {
link_host = this.href.split("/")[2];
document_host = document.location.href.split("/")[2];
Expand All @@ -80,4 +114,4 @@ $(document).ready(function() {
return false;
}
});
});
});
23 changes: 23 additions & 0 deletions app/controllers/user_settings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2011-2014 Rice University. Licensed under the Affero General Public
# License version 3 or later. See the COPYRIGHT file for details.

class UserSettingsController < ApplicationController
skip_before_filter :authenticate_user!

def index
@settings = UserSettings.for(present_user)
end

def update
@settings = UserSettings.for(present_user)
respond_to do |format|
if @settings.update_attributes(params[:settings])
format.json { render json: {:success => true, message: 'Settings were successfully updated.'}}
else
Rails.logger.info(@settings.errors.messages.inspect)
format.json { render json: {:success => false, message: 'Unable to update settings.'} }
end
end
end

end
26 changes: 26 additions & 0 deletions app/models/user_settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2011-2014 Rice University. Licensed under the Affero General Public
# License version 3 or later. See the COPYRIGHT file for details.

class UserSettings < ActiveRecord::Base
store :settings

store_typed_accessor :settings, :boolean, :skip_answer_lockin_message

attr_accessible :skip_answer_lockin_message, :user_id

after_initialize :supply_missing_values

def self.for (user)
if user
find_or_create_by_user_id(:user_id => user.id)
else
nil
end
end

def supply_missing_values
self.skip_answer_lockin_message ||= false
end


end
23 changes: 23 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@

<%= render :partial => 'layouts/common_head_includes' %>
<% if current_user.present? %>
<% ost_user_settings = UserSettings.for(present_user) %>
<script type="text/javascript">
window.OSTUserSettings = <%= raw ost_user_settings.settings.to_json %>;
</script>
<% end %>
<% if Rails.env.production? %>
<script type="text/javascript">

Expand Down Expand Up @@ -204,6 +212,21 @@
<div id="message_dialog_errors"></div>
<div id="message_dialog_body"></div>
</div>

<% if current_user.present? %>
<div id="maybe_confirm_dialog" style="display:none" title="">
<div id="maybe_confirm_dialog_errors"></div>
<div id="maybe_confirm_dialog_body"></div>
<%= form_for ost_user_settings, :remote => true do |f| %>
<input type="checkbox" value="true" id="skip_maybe_confirm_dialog">
<label for="maybe_confirm_skip">Don't show this again.</label>
<% end %>
<div id="maybe_confirm_button_panel">
<button id="maybe_confirm_button_cancel" class="button dialog-cancel secondary">Cancel</button>
<button id="maybe_confirm_button_ok" class="button dialog-ok primary">Continue</button>
</div>
</div>
<% end %>

<div id="help_dialog" style="display:none" title="">
<div id="help_dialog_errors"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@
</div>

<center>

<% confirm_message = "After you turn in your answer, you can no longer change it. " +
"Are you sure you want to turn in your answer?" %>

<div class="actions" style="padding-top:10px">
<%= f.submit "Turn in my answer",
<%= f.submit "Turn in my answer",
:data => {maybeConfirm: confirm_message, setting: 'skip_answer_lockin_message'},
:name => "save_and_lock",
:class => 'link_button',
:confirm => "After you turn in your answer, you can no longer change it. " +
"Are you sure you want to turn in your answer?" %>
:class => 'link_button maybe_confirm' %>

</div>

</center>
Expand Down
9 changes: 5 additions & 4 deletions app/views/student_exercises/_multiple_choice_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@

<center>
<div class="actions">
<%= f.submit "Turn in my choice",
:class => 'link_button',
:confirm => "After you turn in your selected answer, you can no longer change it. " +
<% confirm_message = "After you turn in your selected answer, you can no longer change it. " +
"Are you sure you want to turn it in?" %>
<%= f.submit "Turn in my choice",
:data => {maybeConfirm: confirm_message, setting: 'skip_answer_lockin_message'},
:class => 'link_button maybe_confirm' %>
</div>
</center>
<% end %>
Expand All @@ -53,4 +54,4 @@
});
})
</script>
<% end %>
<% end %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
end

resources :assignment_exercises, :only => [:show]
resources :user_settings, :only => [:show, :update]

resources :external_assignments, :shallow => true, :only => [ ] do
collection do
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20140613110759_add_user_settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2011-2014 Rice University. Licensed under the Affero General Public
# License version 3 or later. See the COPYRIGHT file for details.

class AddUserSettings < ActiveRecord::Migration
def change
create_table(:user_settings) do |t|
t.integer :user_id, :null => false
t.text :settings, :null => true
t.timestamps
end

add_index :user_settings, :user_id
end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20140218235334) do
ActiveRecord::Schema.define(:version => 20140613110759) do

create_table "assignment_coworkers", :force => true do |t|
t.integer "student_assignment_id"
Expand Down Expand Up @@ -588,6 +588,15 @@
add_index "topics", ["name", "learning_plan_id"], :name => "index_topics_on_name_scoped", :unique => true
add_index "topics", ["number", "learning_plan_id"], :name => "index_topics_on_number_scoped", :unique => true

create_table "user_settings", :force => true do |t|
t.integer "user_id", :null => false
t.text "settings"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "user_settings", ["user_id"], :name => "index_user_settings_on_user_id"

create_table "users", :force => true do |t|
t.string "email", :null => false
t.string "encrypted_password", :default => "", :null => false
Expand Down

0 comments on commit 3f1dff0

Please sign in to comment.