BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
8377c2fb
Commit
8377c2fb
authored
Sep 08, 2016
by
Bryce Johnson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor Todos to ES6.
parent
45ae34f8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
162 additions
and
177 deletions
+162
-177
dispatcher.js
app/assets/javascripts/dispatcher.js
+1
-1
todos.js
app/assets/javascripts/todos.js
+0
-176
todos.js.es6
app/assets/javascripts/todos.js.es6
+161
-0
No files found.
app/assets/javascripts/dispatcher.js
View file @
8377c2fb
...
...
@@ -40,7 +40,7 @@
new
Milestone
();
break
;
case
'dashboard:todos:index'
:
new
Todos
();
new
gl
.
Todos
();
break
;
case
'projects:milestones:new'
:
case
'projects:milestones:edit'
:
...
...
app/assets/javascripts/todos.js
deleted
100644 → 0
View file @
45ae34f8
(
function
()
{
var
bind
=
function
(
fn
,
me
){
return
function
(){
return
fn
.
apply
(
me
,
arguments
);
};
};
this
.
Todos
=
(
function
()
{
function
Todos
(
opts
)
{
var
ref
;
if
(
opts
==
null
)
{
opts
=
{};
}
this
.
allDoneClicked
=
bind
(
this
.
allDoneClicked
,
this
);
this
.
doneClicked
=
bind
(
this
.
doneClicked
,
this
);
this
.
el
=
(
ref
=
opts
.
el
)
!=
null
?
ref
:
$
(
'.js-todos-options'
);
this
.
perPage
=
this
.
el
.
data
(
'perPage'
);
this
.
clearListeners
();
this
.
initBtnListeners
();
this
.
initFilters
();
}
Todos
.
prototype
.
clearListeners
=
function
()
{
$
(
'.done-todo'
).
off
(
'click'
);
$
(
'.js-todos-mark-all'
).
off
(
'click'
);
return
$
(
'.todo'
).
off
(
'click'
);
};
Todos
.
prototype
.
initBtnListeners
=
function
()
{
$
(
'.done-todo'
).
on
(
'click'
,
this
.
doneClicked
);
$
(
'.js-todos-mark-all'
).
on
(
'click'
,
this
.
allDoneClicked
);
return
$
(
'.todo'
).
on
(
'click'
,
this
.
goToTodoUrl
);
};
Todos
.
prototype
.
initFilters
=
function
()
{
new
UsersSelect
();
this
.
initFilterDropdown
(
$
(
'.js-project-search'
),
'project_id'
,
[
'text'
]);
this
.
initFilterDropdown
(
$
(
'.js-type-search'
),
'type'
);
this
.
initFilterDropdown
(
$
(
'.js-action-search'
),
'action_id'
);
$
(
'form.filter-form'
).
on
(
'submit'
,
function
(
event
)
{
event
.
preventDefault
();
Turbolinks
.
visit
(
this
.
action
+
'&'
+
$
(
this
).
serialize
());
});
};
Todos
.
prototype
.
initFilterDropdown
=
function
(
$dropdown
,
fieldName
,
searchFields
)
{
$dropdown
.
glDropdown
({
selectable
:
true
,
filterable
:
searchFields
?
true
:
false
,
fieldName
:
fieldName
,
search
:
{
fields
:
searchFields
},
data
:
$dropdown
.
data
(
'data'
),
clicked
:
function
()
{
return
$dropdown
.
closest
(
'form.filter-form'
).
submit
();
}
})
};
Todos
.
prototype
.
doneClicked
=
function
(
e
)
{
var
$this
;
e
.
preventDefault
();
e
.
stopImmediatePropagation
();
$this
=
$
(
e
.
currentTarget
);
$this
.
disable
();
return
$
.
ajax
({
type
:
'POST'
,
url
:
$this
.
attr
(
'href'
),
dataType
:
'json'
,
data
:
{
'_method'
:
'delete'
},
success
:
(
function
(
_this
)
{
return
function
(
data
)
{
_this
.
redirectIfNeeded
(
data
.
count
);
_this
.
clearDone
(
$this
.
closest
(
'li'
));
return
_this
.
updateBadges
(
data
);
};
})(
this
)
});
};
Todos
.
prototype
.
allDoneClicked
=
function
(
e
)
{
var
$this
;
e
.
preventDefault
();
e
.
stopImmediatePropagation
();
$this
=
$
(
e
.
currentTarget
);
$this
.
disable
();
return
$
.
ajax
({
type
:
'POST'
,
url
:
$this
.
attr
(
'href'
),
dataType
:
'json'
,
data
:
{
'_method'
:
'delete'
},
success
:
(
function
(
_this
)
{
return
function
(
data
)
{
$this
.
remove
();
$
(
'.prepend-top-default'
).
html
(
'<div class="nothing-here-block">You
\'
re all done!</div>'
);
return
_this
.
updateBadges
(
data
);
};
})(
this
)
});
};
Todos
.
prototype
.
clearDone
=
function
(
$row
)
{
var
$ul
;
$ul
=
$row
.
closest
(
'ul'
);
$row
.
remove
();
if
(
!
$ul
.
find
(
'li'
).
length
)
{
return
$ul
.
parents
(
'.panel'
).
remove
();
}
};
Todos
.
prototype
.
updateBadges
=
function
(
data
)
{
$
(
'.todos-pending .badge, .todos-pending-count'
).
text
(
data
.
count
);
return
$
(
'.todos-done .badge'
).
text
(
data
.
done_count
);
};
Todos
.
prototype
.
getTotalPages
=
function
()
{
return
this
.
el
.
data
(
'totalPages'
);
};
Todos
.
prototype
.
getCurrentPage
=
function
()
{
return
this
.
el
.
data
(
'currentPage'
);
};
Todos
.
prototype
.
getTodosPerPage
=
function
()
{
return
this
.
el
.
data
(
'perPage'
);
};
Todos
.
prototype
.
redirectIfNeeded
=
function
(
total
)
{
var
currPage
,
currPages
,
newPages
,
pageParams
,
url
;
currPages
=
this
.
getTotalPages
();
currPage
=
this
.
getCurrentPage
();
// Refresh if no remaining Todos
if
(
!
total
)
{
location
.
reload
();
return
;
}
// Do nothing if no pagination
if
(
!
currPages
)
{
return
;
}
newPages
=
Math
.
ceil
(
total
/
this
.
getTodosPerPage
());
// Includes query strings
url
=
location
.
href
;
// If new total of pages is different than we have now
if
(
newPages
!==
currPages
)
{
// Redirect to previous page if there's one available
if
(
currPages
>
1
&&
currPage
===
currPages
)
{
pageParams
=
{
page
:
currPages
-
1
};
url
=
gl
.
utils
.
mergeUrlParams
(
pageParams
,
url
);
}
return
Turbolinks
.
visit
(
url
);
}
};
Todos
.
prototype
.
goToTodoUrl
=
function
(
e
)
{
var
todoLink
;
todoLink
=
$
(
this
).
data
(
'url'
);
if
(
!
todoLink
)
{
return
;
}
// Allow Meta-Click or Mouse3-click to open in a new tab
if
(
e
.
metaKey
||
e
.
which
===
2
)
{
e
.
preventDefault
();
return
window
.
open
(
todoLink
,
'_blank'
);
}
else
{
return
Turbolinks
.
visit
(
todoLink
);
}
};
return
Todos
;
})();
}).
call
(
this
);
app/assets/javascripts/todos.js.es6
0 → 100644
View file @
8377c2fb
(global => {
class Todos {
constructor(opts = {}) {
this.allDoneClicked = this.allDoneClicked.bind(this);
this.doneClicked = this.doneClicked.bind(this);
this.el = opts.el || $('.js-todos-options');
this.perPage = this.el.data('perPage');
this.clearListeners();
this.initBtnListeners();
this.initFilters();
}
clearListeners() {
$('.done-todo').off('click');
$('.js-todos-mark-all').off('click');
return $('.todo').off('click');
}
initBtnListeners() {
$('.done-todo').on('click', this.doneClicked);
$('.js-todos-mark-all').on('click', this.allDoneClicked);
return $('.todo').on('click', this.goToTodoUrl);
}
initFilters() {
new UsersSelect();
this.initFilterDropdown($('.js-project-search'), 'project_id', ['text']);
this.initFilterDropdown($('.js-type-search'), 'type');
this.initFilterDropdown($('.js-action-search'), 'action_id');
$('form.filter-form').on('submit', function (event) {
event.preventDefault();
Turbolinks.visit(this.action + '&' + $(this).serialize());
});
}
initFilterDropdown($dropdown, fieldName, searchFields) {
$dropdown.glDropdown({
fieldName,
selectable: true,
filterable: searchFields ? true : false,
search: { fields: searchFields },
data: $dropdown.data('data'),
clicked: function() {
return $dropdown.closest('form.filter-form').submit();
}
})
}
doneClicked(e) {
e.preventDefault();
e.stopImmediatePropagation();
const $target = $(e.currentTarget);
$target.disable();
return $.ajax({
type: 'POST',
url: $target.attr('href'),
dataType: 'json',
data: {
'_method': 'delete'
},
success: data => {
this.redirectIfNeeded(data.count);
this.clearDone($target.closest('li'));
return this.updateBadges(data);
}
});
}
allDoneClicked(e) {
e.preventDefault();
e.stopImmediatePropagation();
$target = $(e.currentTarget);
$target.disable();
return $.ajax({
type: 'POST',
url: $target.attr('href'),
dataType: 'json',
data: {
'_method': 'delete'
},
success: data => {
$target.remove();
$('.prepend-top-default').html('<div class="nothing-here-block">You\'re all done!</div>');
return this.updateBadges(data);
}
});
}
clearDone($row) {
const $ul = $row.closest('ul');
$row.remove();
if (!$ul.find('li').length) {
return $ul.parents('.panel').remove();
}
}
updateBadges(data) {
$('.todos-pending .badge, .todos-pending-count').text(data.count);
return $('.todos-done .badge').text(data.done_count);
}
getTotalPages() {
return this.el.data('totalPages');
}
getCurrentPage() {
return this.el.data('currentPage');
}
getTodosPerPage() {
return this.el.data('perPage');
}
redirectIfNeeded(total) {
let currPages = this.getTotalPages();
currPage = this.getCurrentPage();
// Refresh if no remaining Todos
if (!total) {
window.location.reload();
return;
}
// Do nothing if no pagination
if (!currPages) {
return;
}
const newPages = Math.ceil(total / this.getTodosPerPage());
let url = location.href;
if (newPages !== currPages) {
// Redirect to previous page if there's one available
if (currPages > 1 && currPage === currPages) {
const pageParams = {
page: currPages - 1
};
url = gl.utils.mergeUrlParams(pageParams, url);
}
return Turbolinks.visit(url);
}
}
goToTodoUrl(e) {
const todoLink = $(this).data('url');
if (!todoLink) {
return;
}
// Allow Meta-Click or Mouse3-click to open in a new tab
if (e.metaKey || e.which === 2) {
e.preventDefault();
return window.open(todoLink, '_blank');
} else {
return Turbolinks.visit(todoLink);
}
}
}
global.Todos = Todos;
})(window.gl || (window.gl = {}));
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment