-
Notifications
You must be signed in to change notification settings - Fork 31
Hiepluong dev #61
Hiepluong dev #61
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| $(document).ready(function() { | ||
| $('#btn_add_sidebar_item').click(function(e) { | ||
| e.preventDefault(); | ||
| var $select_option = $('#course_navbar_preference_course_id').find(':selected'); | ||
| if($select_option.val()){ | ||
| var data = { func : 'add', id : $select_option.val() }; | ||
| sidebar_update_values(data, this); | ||
| } | ||
| }); | ||
|
|
||
| $('.btn-remove-sidebar-item').click(function(e) { | ||
| e.preventDefault(); | ||
| var data = { func : 'remove', id : '0' }; | ||
| sidebar_update_values(data, this); | ||
| }); | ||
|
|
||
| $('input[id*="course_course_navbar_preferences_attributes"][id*="name"]').change(function(e) { | ||
| e.preventDefault(); | ||
| var data = { func : 'update_name', id : '0', name : $(this).val() }; | ||
| sidebar_update_values(data, this); | ||
|
|
||
| }); | ||
|
|
||
| $('input[id*="course_course_navbar_preferences_attributes"][id*="pos"]').each(function(e) { | ||
| $(this).data("old_value", $(this).val()); | ||
| }); | ||
|
|
||
| $('input[id*="course_course_navbar_preferences_attributes"][id*="pos"]').change(function(e) { | ||
| e.preventDefault(); | ||
| if(isNaN($(this).val())){ | ||
| $(this).val($(this).data("old_value")); | ||
| }else{ | ||
| var data = { func : 'update_pos', id : '0', pos : $(this).val() }; | ||
| sidebar_update_values(data, this); | ||
| } | ||
| }); | ||
|
|
||
| $('input[id*="course_course_navbar_preferences_attributes"][id*="is_displayed"]').change(function(e) { | ||
| e.preventDefault(); | ||
| var data = { func : 'update_is_displayed', id : '0', checked : $(this).is(":checked") }; | ||
| sidebar_update_values(data, $(this).parent()); | ||
| }); | ||
|
|
||
| $('input[id*="ip_display_st_level_ach"]').change(function(e) { | ||
| e.preventDefault(); | ||
| var id = $(this).attr('id').split('_')[$(this).attr('id').split('_').length - 1]; | ||
| var data = { func : 'update_display_st_level_ach',id : id, checked : $(this).is(":checked") }; | ||
| sidebar_update_values(data, this); | ||
| }); | ||
| }); | ||
|
|
||
| function update_layout_pos(handler, index, count){ | ||
| var $tr_to_change = $(handler).parent().parent(); | ||
| var item = $tr_to_change.find('input.sidebar-item-name').val(); | ||
| var $litag = $('ul#navbar_tabs span#badge_' + item).parent().parent().clone(); | ||
| $('ul#navbar_tabs span#badge_' + item).parent().parent().remove(); | ||
| if(parseInt(index) <= parseInt(count - 2)){ | ||
| $('ul#navbar_tabs li:eq(' + index + ')').before($litag); | ||
| }else { | ||
| $('ul#navbar_tabs li:eq(' + (parseInt(index)-1) + ')').after($litag); | ||
| } | ||
| $(handler).data("old_value", $(handler).val()); | ||
| } | ||
|
|
||
| function update_layout_name(handler){ | ||
| var $tr_to_change = $(handler).parent().parent(); | ||
| var item = $tr_to_change.find('input.sidebar-item-name').val(); | ||
| var $atag = $('ul#navbar_tabs span#badge_' + item).parent(); | ||
| var $children = $atag.children(); | ||
| $atag.empty(); | ||
| $atag.append($children.eq(0)); | ||
| $atag.append($(handler).val()); | ||
| $atag.append($children.eq(1)); | ||
| } | ||
|
|
||
| function update_layout_remove(handler){ | ||
| var $tr_to_hide = $(handler).parent().parent(); | ||
| $tr_to_hide.find('input[id*="is_enabled"]').prop('checked', false); | ||
| $tr_to_hide.addClass('hidden_navbar_tr'); | ||
| $('#course_navbar_preference_course_id').append($('<option>', { | ||
| value : $tr_to_hide.next().val(), | ||
| text : $tr_to_hide.children(':first').children(':first').val() | ||
| })); | ||
| var item = $tr_to_hide.find('input.sidebar-item-name').val(); | ||
| $('ul#navbar_tabs span#badge_' + item).parent().parent().hide(); | ||
|
|
||
| } | ||
|
|
||
| function update_layout_add(handler ,result){ | ||
| var $tr_to_show = $('input:hidden[value="'+ result.id +'"]').prev(); | ||
| $tr_to_show.find('input[id*="is_enabled"]').prop('checked', true); | ||
| $tr_to_show.removeClass('hidden_navbar_tr'); | ||
| $('#course_navbar_preference_course_id').find(':selected').remove(); | ||
|
|
||
| var litag = '<li>'; | ||
| litag += '<a href="' + result.url + '">'; | ||
| litag += '<span class="nav-icon">'; | ||
| litag += '<i class="' + result.icon + '"></i>'; | ||
| litag += '</span>'; | ||
| litag += result.name; | ||
| litag += '<span id="badge_' + result.item + '" class="sidenav-count" style="display: none"></span>'; | ||
| litag += '</a>'; | ||
| litag += '</li>'; | ||
|
|
||
| if(parseInt(result.index) <= parseInt(result.count) - 1){ | ||
| $('ul#navbar_tabs li:eq(' + result.index + ')').before(litag); | ||
| }else { | ||
| $('ul#navbar_tabs li:eq(' + (parseInt(result.index)-1) + ')').after(litag); | ||
| } | ||
| } | ||
|
|
||
| function sidebar_update_values(data, handler){ | ||
| var course_id = $('div.div-add-item-sidebar input.sidebar-course-id').val(); | ||
| var url = '/courses/' + course_id + '/preferences/sidebar_update_values'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I do for this is, I put the url as a hidden field in the html and get it's value. So in case of a url update, I don't need to find all affected js and fix them. |
||
| if(data.func != 'add' && data.func != 'update_display_st_level_ach'){ | ||
| data.id = $(handler).parent().parent().next().val(); | ||
| } | ||
| $.ajax({ | ||
| url : url, | ||
| type : 'POST', | ||
| dataType : 'json', | ||
| data : data, | ||
| success : function(result) { | ||
| if (data.func == 'add' && result != null){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of this whole if else chunk, you can do data.func(result), if you just store the function to handle in data.func ? |
||
| update_layout_add(handler ,result); | ||
| }else if (data.func == 'remove' && result.status == 'OK'){ | ||
| update_layout_remove(handler); | ||
| }else if (data.func == 'update_name' && result.status == 'OK'){ | ||
| update_layout_name(handler); | ||
| }else if (data.func == 'update_pos' && !isNaN(result.index)){ | ||
| update_layout_pos(handler,result.index, result.count); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ def edit | |
| else | ||
| @tab = 'Sidebar' | ||
| @ranking = @course.student_sidebar_ranking | ||
|
|
||
| end | ||
| end | ||
|
|
||
|
|
@@ -56,7 +57,67 @@ def update | |
| end | ||
| end | ||
| redirect_to params[:origin], :notice => "Updated successfully" | ||
| end | ||
|
|
||
| def sidebar_update_values | ||
| #dalli don't support regualr expression | ||
| Role.all.each do |role| | ||
| expire_fragment("sidebar/#{@course.id}/role/#{role.id}") | ||
| end | ||
| if params[:func] == 'update_display_st_level_ach' | ||
| curr_pref = @course.course_preferences.find(params[:id]) | ||
| curr_pref.display = params[:checked]=='true' ? 1 : 0 | ||
| else | ||
| cnp = CourseNavbarPreference.find(params[:id]) | ||
| case params[:func] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you post data is properly formatted, you can do something like cnp.update_attributes(..), check update_attributes out. And get rid of this case statement. |
||
| when 'add' | ||
| cnp.is_enabled = true | ||
| when 'remove' | ||
| cnp.is_enabled = false | ||
| when 'update_name' | ||
| cnp.name = params[:name] | ||
| when 'update_pos' | ||
| cnp.pos = params[:pos] | ||
| when 'update_is_displayed' | ||
| cnp.is_displayed = params[:checked]=='true' ? 1 : 0 | ||
| end | ||
| end | ||
|
|
||
| respond_to do |format| | ||
| if(params[:func] == 'update_display_st_level_ach') | ||
| if curr_pref.save | ||
| format.json { render json: { status: 'OK' }} | ||
| end | ||
| else | ||
| if cnp.save | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole part is kind of ugly. |
||
| if params[:func] == 'add' || params[:func] == 'update_pos' | ||
| tags = @course.course_navbar_preferences.where(is_enabled: true).order(:pos) | ||
| new_index = tags.find_index{ |item| item.id.to_s == params[:id] } | ||
| if params[:func] == 'add' | ||
| url_and_icon = get_url_and_icon(cnp.item); | ||
| format.json { render json: { index: new_index, | ||
| count: tags.count, | ||
| name: cnp.name, | ||
| id: cnp.id, | ||
| item: cnp.item, | ||
| url: url_and_icon.first, | ||
| icon: url_and_icon.last | ||
| } | ||
| } | ||
| else | ||
| format.json { render json: { index: new_index, count: tags.count }} | ||
| end | ||
| else | ||
| format.json { render json: { status: 'OK' }} | ||
| end | ||
| else | ||
| format.json { render json: {errors: 'Fail'}} | ||
| flash[:error] ='Update failed. You may entered invalid name or email.' | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| private | ||
| def authorize_preference_setting | ||
| authorize! :manage, :course_preference | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,15 @@ | ||
| <tr> | ||
| <tr class='<%= "hidden_navbar_tr" if !f.object.is_enabled? %>'> | ||
| <td><%= f.input_field :name %></td> | ||
| <td><%= f.input_field :pos %></td> | ||
| <td><%= f.input_field :is_displayed, as: :boolean %></td> | ||
| <td><%= f.input_field :is_enabled, as: :boolean %></td> | ||
| </tr> | ||
| <td class="hidden_navbar_td"><%= f.input_field :is_enabled, as: :boolean %></td> | ||
| <td> | ||
| <%= submit_tag 'Remove', :type => 'button', :class => "btn btn-remove-sidebar-item" %> | ||
| <input type="hidden" class="sidebar-item-name" value="<%= f.object.item %>"/> | ||
| </td> | ||
|
|
||
| </tr> | ||
|
|
||
|
|
||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This kind of code can be easily broken, what if we change the layout of the sidebar item. I think, a better way is, you give the sidebar li item an id (which would be the item name), then for remove and reorder, you don't need this ugly parent().parent() thing. Also, you can have a span wrapper around the name of the sidebar item, so to update name, you just find this span element and change it's content. yeah ?