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 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 26 $(".account-box").mouseenter(showMenu);
30 27 $(".account-box").mouseleave(resetMenu);
31 28  
... ... @@ -45,6 +42,9 @@ $(document).ready(function(){
45 42 }
46 43 });
47 44  
  45 + /**
  46 + * Focus search field by pressing 's' key
  47 + */
48 48 $(document).keypress(function(e) {
49 49 if( $(e.target).is(":input") ) return;
50 50 switch(e.which) {
... ... @@ -52,27 +52,12 @@ $(document).ready(function(){
52 52 e.preventDefault();
53 53 }
54 54 });
55   -
56 55 });
57 56  
58 57 function focusSearch() {
59 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 61 function updatePage(data){
77 62 $.ajax({type: "GET", url: location.href, data: data, dataType: "script"});
78 63 }
... ... @@ -84,5 +69,3 @@ function showMenu() {
84 69 function resetMenu() {
85 70 $(this).removeClass("hover");
86 71 }
87   -
88   -
... ...
app/assets/javascripts/note.js
... ... @@ -13,46 +13,54 @@ init:
13 13 this.notes_path = path + ".js";
14 14 this.target_id = tid;
15 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 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 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 60 prepend:
52 61 function(id, html) {
53 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 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 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 147 initLoadMore:
87 148 function() {
88 149 $(document).endlessScroll({
... ...
app/assets/stylesheets/notes.scss
... ... @@ -2,13 +2,18 @@
2 2 * Notes
3 3 *
4 4 */
5   -#notes-list {
  5 +#notes-list,
  6 +#new_notes_list {
6 7 display:block;
7 8 list-style:none;
8 9 margin:0px;
9 10 padding:0px;
10 11 }
11 12  
  13 +#new_notes_list li:last-child{
  14 + border-bottom:1px solid #aaa;
  15 +}
  16 +
12 17 .issue_notes {
13 18 .note_content {
14 19 float:left;
... ...
app/controllers/notes_controller.rb
... ... @@ -16,7 +16,7 @@ class NotesController < ApplicationController
16 16 when "snippet"
17 17 then project.snippets.find(params[:target_id]).notes
18 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 20 when "issue"
21 21 then project.issues.find(params[:target_id]).notes.inc_author.order("created_at DESC").limit(20)
22 22 when "merge_request"
... ...
app/views/notes/_load.js.haml
1 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 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 6 - elsif params[:first_id]
12 7 :plain
... ... @@ -14,6 +9,7 @@
14 9  
15 10 - else
16 11 :plain
  12 + NoteList.setContent(#{@notes.last.id}, #{@notes.first.id}, "#{escape_javascript(render(:partial => 'notes/notes_list'))}");
17 13  
18 14 - else
19 15 - if params[:first_id]
... ...
app/views/notes/_notes.html.haml
... ... @@ -2,27 +2,12 @@
2 2 = render "notes/form"
3 3 .clear
4 4 %hr
  5 +%ul#new_notes_list
5 6 %ul#notes-list
6   -.loading
  7 +.status
7 8  
8 9  
9 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 11 $(function(){
22   - $("#note_note").live("click", function(){
23   - $(this).css("height", "100px");
24   - $('.attach_holder').show();
25   - });
26   -
27 12 NoteList.init("#{tid}", "#{tt}", "#{project_notes_path(@project)}");
28 13 });
... ...