Commit 1e689bfba39525ead225eaf611948cfbe8ac34cf

Authored by Dmitriy Zaporozhets
1 parent f0f14c8e

fixed notes logic

app/assets/javascripts/application.js
@@ -23,9 +23,6 @@ $(document).ready(function(){ @@ -23,9 +23,6 @@ $(document).ready(function(){
23 $(this).select(); 23 $(this).select();
24 }); 24 });
25 25
26 - $('select#branch').selectmenu({style:'popup', width:200});  
27 - $('select#tag').selectmenu({style:'popup', width:200});  
28 -  
29 $(".account-box").mouseenter(showMenu); 26 $(".account-box").mouseenter(showMenu);
30 $(".account-box").mouseleave(resetMenu); 27 $(".account-box").mouseleave(resetMenu);
31 28
@@ -45,6 +42,9 @@ $(document).ready(function(){ @@ -45,6 +42,9 @@ $(document).ready(function(){
45 } 42 }
46 }); 43 });
47 44
  45 + /**
  46 + * Focus search field by pressing 's' key
  47 + */
48 $(document).keypress(function(e) { 48 $(document).keypress(function(e) {
49 if( $(e.target).is(":input") ) return; 49 if( $(e.target).is(":input") ) return;
50 switch(e.which) { 50 switch(e.which) {
@@ -52,27 +52,12 @@ $(document).ready(function(){ @@ -52,27 +52,12 @@ $(document).ready(function(){
52 e.preventDefault(); 52 e.preventDefault();
53 } 53 }
54 }); 54 });
55 -  
56 }); 55 });
57 56
58 function focusSearch() { 57 function focusSearch() {
59 $("#search").focus(); 58 $("#search").focus();
60 } 59 }
61 60
62 -function taggifyForm(){  
63 - var tag_field = $('#tag_field').tagify();  
64 -  
65 - tag_field.tagify('inputField').autocomplete({  
66 - source: '/tags.json'  
67 - });  
68 -  
69 - $('form').submit( function() {  
70 - var tag_field = $('#tag_field')  
71 - tag_field.val( tag_field.tagify('serialize') );  
72 - return true;  
73 - });  
74 -}  
75 -  
76 function updatePage(data){ 61 function updatePage(data){
77 $.ajax({type: "GET", url: location.href, data: data, dataType: "script"}); 62 $.ajax({type: "GET", url: location.href, data: data, dataType: "script"});
78 } 63 }
@@ -84,5 +69,3 @@ function showMenu() { @@ -84,5 +69,3 @@ function showMenu() {
84 function resetMenu() { 69 function resetMenu() {
85 $(this).removeClass("hover"); 70 $(this).removeClass("hover");
86 } 71 }
87 -  
88 -  
app/assets/javascripts/note.js
@@ -13,46 +13,54 @@ init: @@ -13,46 +13,54 @@ init:
13 this.notes_path = path + ".js"; 13 this.notes_path = path + ".js";
14 this.target_id = tid; 14 this.target_id = tid;
15 this.target_type = tt; 15 this.target_type = tt;
16 - this.target_params = "&target_type=" + this.target_type + "&target_id=" + this.target_id  
17 - this.refresh(); 16 + this.target_params = "&target_type=" + this.target_type + "&target_id=" + this.target_id;
  17 +
  18 + // get notes
  19 + this.getContent();
  20 +
  21 + // get new notes every n seconds
18 this.initRefresh(); 22 this.initRefresh();
19 - this.initLoadMore();  
20 - },  
21 23
22 -getOld:  
23 - function() {  
24 - $('.loading').show();  
25 - $.ajax({  
26 - type: "GET",  
27 - url: this.notes_path,  
28 - data: "first_id=" + this.first_id + this.target_params,  
29 - complete: function(){ $('.loading').hide()},  
30 - dataType: "script"}); 24 + $('.delete-note').live('ajax:success', function() {
  25 + $(this).closest('li').fadeOut(); });
  26 +
  27 + $("#new_note").live("ajax:before", function(){
  28 + $("#submit_note").attr("disabled", "disabled");
  29 + })
  30 +
  31 + $("#new_note").live("ajax:complete", function(){
  32 + $("#submit_note").removeAttr("disabled");
  33 + })
  34 +
  35 + $("#note_note").live("click", function(){
  36 + $(this).css("height", "100px");
  37 + $('.attach_holder').show();
  38 + });
  39 +
31 }, 40 },
32 41
33 -append:  
34 - function(id, html) {  
35 - if(this.first_id == id) {  
36 - this.disable = true;  
37 - } else {  
38 - this.first_id = id;  
39 - $("#notes-list").append(html);  
40 - } 42 +
  43 +/**
  44 + * Load new notes to fresh list called 'new_notes_list':
  45 + * - Replace 'new_notes_list' with new list every n seconds
  46 + * - Append new notes to this list after submit
  47 + */
  48 +
  49 +initRefresh:
  50 + function() {
  51 + // init timer
  52 + var intNew = setInterval("NoteList.getNew()", 10000);
41 }, 53 },
42 54
43 replace: 55 replace:
44 - function(fid, lid, html) {  
45 - this.first_id = fid;  
46 - this.last_id = lid;  
47 - $("#notes-list").html(html);  
48 - this.initLoadMore(); 56 + function(html) {
  57 + $("#new_notes_list").html(html);
49 }, 58 },
50 59
51 prepend: 60 prepend:
52 function(id, html) { 61 function(id, html) {
53 if(id != this.last_id) { 62 if(id != this.last_id) {
54 - this.last_id = id;  
55 - $("#notes-list").prepend(html); 63 + $("#new_notes_list").prepend(html);
56 } 64 }
57 }, 65 },
58 66
@@ -76,13 +84,66 @@ refresh: @@ -76,13 +84,66 @@ refresh:
76 dataType: "script"}); 84 dataType: "script"});
77 }, 85 },
78 86
79 -initRefresh: 87 +
  88 +/**
  89 + * Init load of notes:
  90 + * 1. Get content with ajax call
  91 + * 2. Set content of notes list with loaded one
  92 + */
  93 +
  94 +
  95 +getContent:
  96 + function() {
  97 + $.ajax({
  98 + type: "GET",
  99 + url: this.notes_path,
  100 + data: "?" + this.target_params,
  101 + dataType: "script"});
  102 + },
  103 +
  104 +setContent:
  105 + function(fid, lid, html) {
  106 + this.last_id = lid;
  107 + this.first_id = fid;
  108 + $("#notes-list").html(html);
  109 +
  110 + // Init infinite scrolling
  111 + this.initLoadMore();
  112 + },
  113 +
  114 +
  115 +/**
  116 + * Paging for old notes when scroll to bottom:
  117 + * 1. Init scroll events with 'initLoadMore'
  118 + * 2. Load onlder notes with 'getOld' method
  119 + * 3. append old notes to bottom of list with 'append'
  120 + *
  121 + */
  122 +
  123 +
  124 +getOld:
80 function() { 125 function() {
81 - // init timer  
82 - var intNew = setInterval("NoteList.getNew()", 15000);  
83 - var intRefresh = setInterval("NoteList.refresh()", 90000); 126 + $('.loading').show();
  127 + $.ajax({
  128 + type: "GET",
  129 + url: this.notes_path,
  130 + data: "first_id=" + this.first_id + this.target_params,
  131 + complete: function(){ $('.status').removeClass("loading")},
  132 + beforeSend: function() { $('.status').addClass("loading") },
  133 + dataType: "script"});
  134 + },
  135 +
  136 +append:
  137 + function(id, html) {
  138 + if(this.first_id == id) {
  139 + this.disable = true;
  140 + } else {
  141 + this.first_id = id;
  142 + $("#notes-list").append(html);
  143 + }
84 }, 144 },
85 145
  146 +
86 initLoadMore: 147 initLoadMore:
87 function() { 148 function() {
88 $(document).endlessScroll({ 149 $(document).endlessScroll({
app/assets/stylesheets/notes.scss
@@ -2,13 +2,18 @@ @@ -2,13 +2,18 @@
2 * Notes 2 * Notes
3 * 3 *
4 */ 4 */
5 -#notes-list { 5 +#notes-list,
  6 +#new_notes_list {
6 display:block; 7 display:block;
7 list-style:none; 8 list-style:none;
8 margin:0px; 9 margin:0px;
9 padding:0px; 10 padding:0px;
10 } 11 }
11 12
  13 +#new_notes_list li:last-child{
  14 + border-bottom:1px solid #aaa;
  15 +}
  16 +
12 .issue_notes { 17 .issue_notes {
13 .note_content { 18 .note_content {
14 float:left; 19 float:left;
app/controllers/notes_controller.rb
@@ -16,7 +16,7 @@ class NotesController < ApplicationController @@ -16,7 +16,7 @@ class NotesController < ApplicationController
16 when "snippet" 16 when "snippet"
17 then project.snippets.find(params[:target_id]).notes 17 then project.snippets.find(params[:target_id]).notes
18 when "wall" 18 when "wall"
19 - then project.common_notes.order("created_at DESC").fresh.limit(60) 19 + then project.common_notes.order("created_at DESC").fresh.limit(10)
20 when "issue" 20 when "issue"
21 then project.issues.find(params[:target_id]).notes.inc_author.order("created_at DESC").limit(20) 21 then project.issues.find(params[:target_id]).notes.inc_author.order("created_at DESC").limit(20)
22 when "merge_request" 22 when "merge_request"
app/views/notes/_load.js.haml
1 - unless @notes.blank? 1 - unless @notes.blank?
2 -  
3 - - if params[:last_id] && params[:first_id]  
4 - :plain  
5 - NoteList.replace(#{@notes.last.id}, #{@notes.first.id}, "#{escape_javascript(render(:partial => 'notes/notes_list'))}");  
6 -  
7 - - elsif params[:last_id] 2 + - if params[:last_id]
8 :plain 3 :plain
9 - NoteList.prepend(#{@notes.first.id}, "#{escape_javascript(render(:partial => 'notes/notes_list'))}"); 4 + NoteList.replace("#{escape_javascript(render(:partial => 'notes/notes_list'))}");
10 5
11 - elsif params[:first_id] 6 - elsif params[:first_id]
12 :plain 7 :plain
@@ -14,6 +9,7 @@ @@ -14,6 +9,7 @@
14 9
15 - else 10 - else
16 :plain 11 :plain
  12 + NoteList.setContent(#{@notes.last.id}, #{@notes.first.id}, "#{escape_javascript(render(:partial => 'notes/notes_list'))}");
17 13
18 - else 14 - else
19 - if params[:first_id] 15 - if params[:first_id]
app/views/notes/_notes.html.haml
@@ -2,27 +2,12 @@ @@ -2,27 +2,12 @@
2 = render "notes/form" 2 = render "notes/form"
3 .clear 3 .clear
4 %hr 4 %hr
  5 +%ul#new_notes_list
5 %ul#notes-list 6 %ul#notes-list
6 -.loading 7 +.status
7 8
8 9
9 :javascript 10 :javascript
10 - $('.delete-note').live('ajax:success', function() {  
11 - $(this).closest('li').fadeOut(); });  
12 -  
13 - $("#new_note").live("ajax:before", function(){  
14 - $("#submit_note").attr("disabled", "disabled");  
15 - })  
16 -  
17 - $("#new_note").live("ajax:complete", function(){  
18 - $("#submit_note").removeAttr("disabled");  
19 - })  
20 -  
21 $(function(){ 11 $(function(){
22 - $("#note_note").live("click", function(){  
23 - $(this).css("height", "100px");  
24 - $('.attach_holder').show();  
25 - });  
26 -  
27 NoteList.init("#{tid}", "#{tt}", "#{project_notes_path(@project)}"); 12 NoteList.init("#{tid}", "#{tt}", "#{project_notes_path(@project)}");
28 }); 13 });