Commit 0b981eb9b3fbef8f34b9be37a2484c96189ea417

Authored by Dmitriy Zaporozhets
1 parent 37f3d0f2

coffescript refactoring pt.1

app/assets/javascripts/commit.js.coffee
1 -@Commit =  
2 - init: -> 1 +class Commit
  2 + constructor: ->
3 $('.files .file').each -> 3 $('.files .file').each ->
4 new CommitFile(this) 4 new CommitFile(this)
5 5
  6 +@Commit = Commit
app/assets/javascripts/dashboard.js.coffee
1 -window.dashboardPage = ->  
2 - Pager.init 20, true  
3 - initSidebarTab()  
4 - $(".event_filter_link").bind "click", (event) ->  
5 - event.preventDefault()  
6 - toggleFilter $(this)  
7 - reloadActivities()  
8 -  
9 -reloadActivities = ->  
10 - $(".content_list").html ''  
11 - Pager.init 20, true  
12 -  
13 -toggleFilter = (sender) ->  
14 - sender.parent().toggleClass "inactive"  
15 - event_filters = $.cookie("event_filter")  
16 - filter = sender.attr("id").split("_")[0]  
17 - if event_filters  
18 - event_filters = event_filters.split(",")  
19 - else  
20 - event_filters = new Array()  
21 -  
22 - index = event_filters.indexOf(filter)  
23 - if index is -1  
24 - event_filters.push filter  
25 - else  
26 - event_filters.splice index, 1  
27 -  
28 - $.cookie "event_filter", event_filters.join(",")  
29 -  
30 -initSidebarTab = ->  
31 - key = "dashboard_sidebar_filter"  
32 -  
33 - # store selection in cookie  
34 - $('.dash-sidebar-tabs a').on 'click', (e) ->  
35 - $.cookie(key, $(e.target).attr('id'))  
36 -  
37 - # show tab from cookie  
38 - sidebar_filter = $.cookie(key)  
39 - $("#" + sidebar_filter).tab('show') if sidebar_filter 1 +class Dashboard
  2 + constructor: ->
  3 + Pager.init 20, true
  4 + @initSidebarTab()
  5 +
  6 + $(".event_filter_link").bind "click", (event) =>
  7 + event.preventDefault()
  8 + @toggleFilter($(event.currentTarget))
  9 + @reloadActivities()
  10 +
  11 + reloadActivities: ->
  12 + $(".content_list").html ''
  13 + Pager.init 20, true
  14 +
  15 + toggleFilter: (sender) ->
  16 + sender.parent().toggleClass "inactive"
  17 + event_filters = $.cookie("event_filter")
  18 + filter = sender.attr("id").split("_")[0]
  19 + if event_filters
  20 + event_filters = event_filters.split(",")
  21 + else
  22 + event_filters = new Array()
  23 +
  24 + index = event_filters.indexOf(filter)
  25 + if index is -1
  26 + event_filters.push filter
  27 + else
  28 + event_filters.splice index, 1
  29 +
  30 + $.cookie "event_filter", event_filters.join(",")
  31 +
  32 + initSidebarTab: ->
  33 + key = "dashboard_sidebar_filter"
  34 +
  35 + # store selection in cookie
  36 + $('.dash-sidebar-tabs a').on 'click', (e) ->
  37 + $.cookie(key, $(e.target).attr('id'))
  38 +
  39 + # show tab from cookie
  40 + sidebar_filter = $.cookie(key)
  41 + $("#" + sidebar_filter).tab('show') if sidebar_filter
  42 +
  43 +
  44 +@Dashboard = Dashboard
app/assets/javascripts/dispatcher.js.coffee
@@ -4,16 +4,27 @@ $ -> @@ -4,16 +4,27 @@ $ ->
4 class Dispatcher 4 class Dispatcher
5 constructor: () -> 5 constructor: () ->
6 page = $('body').attr('data-page') 6 page = $('body').attr('data-page')
  7 + project_id = $('body').attr('data-project-id')
7 8
8 console.log(page) 9 console.log(page)
  10 +
  11 + path = page.split(':')
9 12
10 switch page 13 switch page
11 - when 'issues:index' then Issues.init()  
12 - when 'dashboard:show' then dashboardPage()  
13 - when 'commit:show' then Commit.init() 14 + when 'issues:index'
  15 + Issues.init()
  16 + when 'dashboard:show'
  17 + new Dashboard()
  18 + when 'commit:show'
  19 + new Commit()
14 when 'groups:show', 'teams:show', 'projects:show' 20 when 'groups:show', 'teams:show', 'projects:show'
15 Pager.init(20, true) 21 Pager.init(20, true)
16 when 'projects:new', 'projects:edit' 22 when 'projects:new', 'projects:edit'
17 - new Projects()  
18 - when 'admin:teams:show', 'admin:groups:show', 'admin:logs:show', 'admin:users:new'  
19 - Admin.init() 23 + new Project()
  24 + when 'walls:show'
  25 + new Wall(project_id)
  26 +
  27 + switch path.first()
  28 + when 'admin' then Admin.init()
  29 + when 'wikis' then new Wikis()
  30 +
app/assets/javascripts/project.js.coffee 0 → 100644
@@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
  1 +class Project
  2 + constructor: ->
  3 + $('.new_project, .edit_project').on 'ajax:before', ->
  4 + $('.project_new_holder, .project_edit_holder').hide()
  5 + $('.save-project-loader').show()
  6 +
  7 + $('form #project_default_branch').chosen()
  8 + disableButtonIfEmptyField '#project_name', '.project-submit'
  9 +
  10 + $('#project_issues_enabled').change ->
  11 + if ($(this).is(':checked') == true)
  12 + $('#project_issues_tracker').removeAttr('disabled')
  13 + else
  14 + $('#project_issues_tracker').attr('disabled', 'disabled')
  15 +
  16 + $('#project_issues_tracker').change()
  17 +
  18 + $('#project_issues_tracker').change ->
  19 + if ($(this).val() == gon.default_issues_tracker || $(this).is(':disabled'))
  20 + $('#project_issues_tracker_id').attr('disabled', 'disabled')
  21 + else
  22 + $('#project_issues_tracker_id').removeAttr('disabled')
  23 +
  24 +@Project = Project
  25 +
  26 +$ ->
  27 + # Git clone panel switcher
  28 + scope = $ '.project_clone_holder'
  29 + if scope.length > 0
  30 + $('a, button', scope).click ->
  31 + $('a, button', scope).removeClass 'active'
  32 + $(@).addClass 'active'
  33 + $('#project_clone', scope).val $(@).data 'clone'
  34 +
  35 + # Ref switcher
  36 + $('.project-refs-select').on 'change', ->
  37 + $(@).parents('form').submit()
app/assets/javascripts/projects.js.coffee
@@ -1,35 +0,0 @@ @@ -1,35 +0,0 @@
1 -window.Projects = ->  
2 - $('.new_project, .edit_project').on 'ajax:before', ->  
3 - $('.project_new_holder, .project_edit_holder').hide()  
4 - $('.save-project-loader').show()  
5 -  
6 - $('form #project_default_branch').chosen()  
7 - disableButtonIfEmptyField '#project_name', '.project-submit'  
8 -  
9 -$ ->  
10 - # Git clone panel switcher  
11 - scope = $ '.project_clone_holder'  
12 - if scope.length > 0  
13 - $('a, button', scope).click ->  
14 - $('a, button', scope).removeClass 'active'  
15 - $(@).addClass 'active'  
16 - $('#project_clone', scope).val $(@).data 'clone'  
17 -  
18 - # Ref switcher  
19 - $('.project-refs-select').on 'change', ->  
20 - $(@).parents('form').submit()  
21 -  
22 - $('#project_issues_enabled').change ->  
23 - if ($(this).is(':checked') == true)  
24 - $('#project_issues_tracker').removeAttr('disabled')  
25 - else  
26 - $('#project_issues_tracker').attr('disabled', 'disabled')  
27 -  
28 - $('#project_issues_tracker').change()  
29 -  
30 - $('#project_issues_tracker').change ->  
31 - if ($(this).val() == gon.default_issues_tracker || $(this).is(':disabled'))  
32 - $('#project_issues_tracker_id').attr('disabled', 'disabled')  
33 - else  
34 - $('#project_issues_tracker_id').removeAttr('disabled')  
35 -  
app/assets/javascripts/wall.js.coffee
1 -@Wall =  
2 - note_ids: []  
3 - project_id: null  
4 -  
5 - init: (project_id) ->  
6 - Wall.project_id = project_id  
7 - Wall.getContent()  
8 - Wall.initRefresh()  
9 - Wall.initForm() 1 +class Wall
  2 + constructor: (project_id) ->
  3 + @project_id = project_id
  4 + @note_ids = []
  5 + @getContent()
  6 + @initRefresh()
  7 + @initForm()
10 8
11 # 9 #
12 # Gets an initial set of notes. 10 # Gets an initial set of notes.
13 # 11 #
14 getContent: -> 12 getContent: ->
15 - Api.notes Wall.project_id, (notes) ->  
16 - $.each notes, (i, note) -> 13 + Api.notes @project_id, (notes) =>
  14 + $.each notes, (i, note) =>
17 # render note if it not present in loaded list 15 # render note if it not present in loaded list
18 # or skip if rendered 16 # or skip if rendered
19 - if $.inArray(note.id, Wall.note_ids) == -1  
20 - Wall.note_ids.push(note.id)  
21 - Wall.renderNote(note)  
22 - Wall.scrollDown() 17 + if $.inArray(note.id, @note_ids) == -1
  18 + @note_ids.push(note.id)
  19 + @renderNote(note)
  20 + @scrollDown()
23 $("abbr.timeago").timeago() 21 $("abbr.timeago").timeago()
24 22
25 initRefresh: -> 23 initRefresh: ->
26 - setInterval("Wall.refresh()", 10000) 24 + setInterval =>
  25 + @refresh()
  26 + , 10000
27 27
28 refresh: -> 28 refresh: ->
29 - Wall.getContent() 29 + @getContent()
30 30
31 scrollDown: -> 31 scrollDown: ->
32 notes = $('ul.notes') 32 notes = $('ul.notes')
@@ -36,8 +36,8 @@ @@ -36,8 +36,8 @@
36 form = $('.wall-note-form') 36 form = $('.wall-note-form')
37 form.find("#target_type").val('wall') 37 form.find("#target_type").val('wall')
38 38
39 - form.on 'ajax:success', ->  
40 - Wall.refresh() 39 + form.on 'ajax:success', =>
  40 + @refresh()
41 form.find(".js-note-text").val("").trigger("input") 41 form.find(".js-note-text").val("").trigger("input")
42 42
43 form.on 'ajax:complete', -> 43 form.on 'ajax:complete', ->
@@ -58,7 +58,7 @@ @@ -58,7 +58,7 @@
58 form.show() 58 form.show()
59 59
60 renderNote: (note) -> 60 renderNote: (note) ->
61 - template = Wall.noteTemplate() 61 + template = @noteTemplate()
62 template = template.replace('{{author_name}}', note.author.name) 62 template = template.replace('{{author_name}}', note.author.name)
63 template = template.replace('{{created_at}}', note.created_at) 63 template = template.replace('{{created_at}}', note.created_at)
64 template = template.replace('{{text}}', linkify(sanitize(note.body))) 64 template = template.replace('{{text}}', linkify(sanitize(note.body)))
@@ -81,3 +81,5 @@ @@ -81,3 +81,5 @@
81 </span> 81 </span>
82 <abbr class="timeago" title="{{created_at}}">{{created_at}}</abbr> 82 <abbr class="timeago" title="{{created_at}}">{{created_at}}</abbr>
83 </li>' 83 </li>'
  84 +
  85 +@Wall = Wall
app/assets/javascripts/wikis.js.coffee 0 → 100644
@@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
  1 +class Wikis
  2 + constructor: ->
  3 + modal = $('#modal-new-wiki').modal({modal: true, show:false})
  4 +
  5 + $('.add-new-wiki').bind "click", ->
  6 + modal.show()
  7 +
  8 + $('.build-new-wiki').bind "click", ->
  9 + field = $('#new_wiki_path')
  10 + slug = field.val()
  11 + path = field.attr('data-wikis-path')
  12 +
  13 + if(slug.length > 0)
  14 + location.href = path + "/" + slug
  15 +
  16 + $('.modal-header .close').bind "click", ->
  17 + modal.hide()
  18 +
  19 +@Wikis = Wikis
app/views/graph/show.html.haml
@@ -7,11 +7,10 @@ @@ -7,11 +7,10 @@
7 7
8 :javascript 8 :javascript
9 var branch_graph; 9 var branch_graph;
10 - $(function(){  
11 - branch_graph = new BranchGraph($("#holder"), {  
12 - url: '#{project_graph_path(@project, @ref, q: @q, format: :json)}',  
13 - commit_url: '#{project_commit_path(@project, 'ae45ca32').gsub("ae45ca32", "%s")}',  
14 - ref: '#{@ref}',  
15 - commit_id: '#{@commit.id}'  
16 - }); 10 +
  11 + branch_graph = new BranchGraph($("#holder"), {
  12 + url: '#{project_graph_path(@project, @ref, q: @q, format: :json)}',
  13 + commit_url: '#{project_commit_path(@project, 'ae45ca32').gsub("ae45ca32", "%s")}',
  14 + ref: '#{@ref}',
  15 + commit_id: '#{@commit.id}'
17 }); 16 });
app/views/layouts/project_resource.html.haml
1 !!! 5 1 !!! 5
2 %html{ lang: "en"} 2 %html{ lang: "en"}
3 = render "layouts/head", title: @project.name_with_namespace 3 = render "layouts/head", title: @project.name_with_namespace
4 - %body{class: "#{app_theme} project", :'data-page' => body_data_page} 4 + %body{class: "#{app_theme} project", :'data-page' => body_data_page, :'data-project-id' => @project.id }
5 = render "layouts/head_panel", title: project_title(@project) 5 = render "layouts/head_panel", title: project_title(@project)
6 = render "layouts/flash" 6 = render "layouts/flash"
7 - if can?(current_user, :download_code, @project) 7 - if can?(current_user, :download_code, @project)
app/views/merge_requests/_show.html.haml
@@ -26,14 +26,12 @@ @@ -26,14 +26,12 @@
26 26
27 :javascript 27 :javascript
28 var merge_request; 28 var merge_request;
29 - $(function(){  
30 - merge_request = new MergeRequest({  
31 - url_to_automerge_check: "#{automerge_check_project_merge_request_path(@project, @merge_request)}",  
32 - check_enable: #{@merge_request.unchecked? ? "true" : "false"},  
33 - url_to_ci_check: "#{ci_status_project_merge_request_path(@project, @merge_request)}",  
34 - ci_enable: #{@project.gitlab_ci? ? "true" : "false"},  
35 - current_status: "#{@merge_request.merge_status_name}",  
36 - action: "#{controller.action_name}"  
37 - });  
38 - });  
39 29
  30 + merge_request = new MergeRequest({
  31 + url_to_automerge_check: "#{automerge_check_project_merge_request_path(@project, @merge_request)}",
  32 + check_enable: #{@merge_request.unchecked? ? "true" : "false"},
  33 + url_to_ci_check: "#{ci_status_project_merge_request_path(@project, @merge_request)}",
  34 + ci_enable: #{@project.gitlab_ci? ? "true" : "false"},
  35 + current_status: "#{@merge_request.merge_status_name}",
  36 + action: "#{controller.action_name}"
  37 + });
app/views/walls/show.html.haml
@@ -21,8 +21,3 @@ @@ -21,8 +21,3 @@
21 21
22 .hint.pull-right CTRL + Enter to send message 22 .hint.pull-right CTRL + Enter to send message
23 .clearfix 23 .clearfix
24 -  
25 -:javascript  
26 - $(function(){  
27 - Wall.init(#{@project.id});  
28 - });  
app/views/wikis/_new.html.haml
@@ -5,27 +5,8 @@ @@ -5,27 +5,8 @@
5 .modal-body 5 .modal-body
6 = label_tag :new_wiki_path do 6 = label_tag :new_wiki_path do
7 %span Page slug 7 %span Page slug
8 - = text_field_tag :new_wiki_path, nil, placeholder: 'how-to-setup', class: 'input-xlarge', required: true 8 + = text_field_tag :new_wiki_path, nil, placeholder: 'how-to-setup', class: 'input-xlarge', required: true, :'data-wikis-path' => project_wikis_path(@project)
9 %p.hint 9 %p.hint
10 Please dont use spaces and slashes 10 Please dont use spaces and slashes
11 .modal-footer 11 .modal-footer
12 = link_to 'Build', '#', class: 'build-new-wiki btn btn-create' 12 = link_to 'Build', '#', class: 'build-new-wiki btn btn-create'
13 -  
14 -:javascript  
15 - $(function(){  
16 - var modal = $('#modal-new-wiki').modal({modal: true, show:false});  
17 - $('.add-new-wiki').bind("click", function(){  
18 - modal.show();  
19 - });  
20 - $('.build-new-wiki').bind("click", function(){  
21 - var slug = $('#new_wiki_path').val();  
22 -  
23 - if(slug.length > 0) {  
24 - location.href = "#{project_wikis_path(@project)}/" + slug;  
25 - }  
26 - });  
27 - $('.modal-header .close').bind("click", function(){  
28 - modal.hide();  
29 - })  
30 - })  
31 -