2014-07-08 07:02:26 -04:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
], (App) ->
|
2014-07-30 10:05:13 -04:00
|
|
|
|
2016-08-30 11:10:04 -04:00
|
|
|
App.controller "ProjectPageController", ($scope, $modal, $q, $window, queuedHttp, event_tracking, $timeout) ->
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.projects = window.data.projects
|
|
|
|
$scope.tags = window.data.tags
|
2016-01-21 15:42:50 -05:00
|
|
|
$scope.notifications = window.data.notifications
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.allSelected = false
|
|
|
|
$scope.selectedProjects = []
|
|
|
|
$scope.filter = "all"
|
2014-07-17 12:34:09 -04:00
|
|
|
$scope.predicate = "lastUpdated"
|
2014-07-18 09:30:24 -04:00
|
|
|
$scope.reverse = true
|
2016-09-06 09:59:18 -04:00
|
|
|
$scope.searchText =
|
|
|
|
value : ""
|
2014-07-08 07:02:26 -04:00
|
|
|
|
2017-01-25 05:31:02 -05:00
|
|
|
$timeout () ->
|
|
|
|
recalculateProjectListHeight()
|
|
|
|
, 10
|
2015-11-02 13:44:12 -05:00
|
|
|
|
2014-07-24 09:00:01 -04:00
|
|
|
recalculateProjectListHeight = () ->
|
2015-11-02 13:44:12 -05:00
|
|
|
topOffset = $(".project-list-card")?.offset()?.top
|
2014-07-24 09:00:01 -04:00
|
|
|
bottomOffset = $("footer").outerHeight() + 25
|
|
|
|
sideBarHeight = $("aside").height() - 56
|
|
|
|
# When footer is visible and page doesn't need to scroll we just make it
|
|
|
|
# span between header and footer
|
|
|
|
height = $window.innerHeight - topOffset - bottomOffset
|
|
|
|
|
|
|
|
# When page is small enough that this pushes the project list smaller than
|
|
|
|
# the side bar, then the window going to have to scroll to take into account the
|
|
|
|
# footer. So we now start to track to the bottom of the window, with a 25px padding
|
|
|
|
# since the footer is hidden below the fold. Don't ever get bigger than the sidebar
|
|
|
|
# though since that's what triggered this happening in the first place.
|
|
|
|
if height < sideBarHeight
|
|
|
|
height = Math.min(sideBarHeight, $window.innerHeight - topOffset - 25)
|
|
|
|
$scope.projectListHeight = height
|
2014-07-28 05:28:48 -04:00
|
|
|
|
2015-11-02 13:44:12 -05:00
|
|
|
|
2014-07-08 12:47:20 -04:00
|
|
|
angular.element($window).bind "resize", () ->
|
2014-07-24 09:00:01 -04:00
|
|
|
recalculateProjectListHeight()
|
2014-07-08 12:47:20 -04:00
|
|
|
$scope.$apply()
|
2014-07-24 09:00:01 -04:00
|
|
|
|
2014-07-08 07:02:26 -04:00
|
|
|
# Allow tags to be accessed on projects as well
|
|
|
|
projectsById = {}
|
|
|
|
for project in $scope.projects
|
|
|
|
projectsById[project.id] = project
|
|
|
|
|
|
|
|
for tag in $scope.tags
|
|
|
|
for project_id in tag.project_ids or []
|
|
|
|
project = projectsById[project_id]
|
|
|
|
if project?
|
|
|
|
project.tags ||= []
|
|
|
|
project.tags.push tag
|
|
|
|
|
2014-07-17 12:34:09 -04:00
|
|
|
$scope.changePredicate = (newPredicate)->
|
|
|
|
if $scope.predicate == newPredicate
|
|
|
|
$scope.reverse = !$scope.reverse
|
|
|
|
$scope.predicate = newPredicate
|
2014-07-08 07:02:26 -04:00
|
|
|
|
2014-07-18 05:43:32 -04:00
|
|
|
$scope.getSortIconClass = (column)->
|
|
|
|
if column == $scope.predicate and $scope.reverse
|
2014-07-18 07:20:12 -04:00
|
|
|
return "fa-caret-down"
|
2014-07-18 05:43:32 -04:00
|
|
|
else if column == $scope.predicate and !$scope.reverse
|
2014-07-18 07:20:12 -04:00
|
|
|
return "fa-caret-up"
|
2014-07-18 05:43:32 -04:00
|
|
|
else
|
2014-07-18 07:20:12 -04:00
|
|
|
return ""
|
2014-07-18 05:43:32 -04:00
|
|
|
|
2014-07-21 06:33:10 -04:00
|
|
|
$scope.searchProjects = ->
|
|
|
|
event_tracking.send 'project-list-page-interaction', 'project-search', 'keydown'
|
|
|
|
$scope.updateVisibleProjects()
|
|
|
|
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.clearSearchText = () ->
|
2016-09-06 09:59:18 -04:00
|
|
|
$scope.searchText.value = ""
|
2014-07-21 09:27:56 -04:00
|
|
|
$scope.filter = "all"
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.$emit "search:clear"
|
2014-07-21 09:27:56 -04:00
|
|
|
$scope.updateVisibleProjects()
|
2014-07-08 07:02:26 -04:00
|
|
|
|
|
|
|
$scope.setFilter = (filter) ->
|
|
|
|
$scope.filter = filter
|
|
|
|
$scope.updateVisibleProjects()
|
|
|
|
|
|
|
|
$scope.updateSelectedProjects = () ->
|
|
|
|
$scope.selectedProjects = $scope.projects.filter (project) -> project.selected
|
|
|
|
|
|
|
|
$scope.getSelectedProjects = () ->
|
|
|
|
$scope.selectedProjects
|
|
|
|
|
|
|
|
$scope.getSelectedProjectIds = () ->
|
|
|
|
$scope.selectedProjects.map (project) -> project.id
|
|
|
|
|
|
|
|
$scope.getFirstSelectedProject = () ->
|
|
|
|
$scope.selectedProjects[0]
|
|
|
|
|
|
|
|
$scope.updateVisibleProjects = () ->
|
|
|
|
$scope.visibleProjects = []
|
|
|
|
selectedTag = $scope.getSelectedTag()
|
|
|
|
for project in $scope.projects
|
|
|
|
visible = true
|
|
|
|
# Only show if it matches any search text
|
2016-09-06 09:59:18 -04:00
|
|
|
if $scope.searchText.value? and $scope.searchText.value != ""
|
2016-11-15 06:41:14 -05:00
|
|
|
if project.name.toLowerCase().indexOf($scope.searchText.value.toLowerCase()) == -1
|
2014-07-08 07:02:26 -04:00
|
|
|
visible = false
|
|
|
|
# Only show if it matches the selected tag
|
|
|
|
if $scope.filter == "tag" and selectedTag? and project.id not in selectedTag.project_ids
|
|
|
|
visible = false
|
|
|
|
|
|
|
|
# Hide projects we own if we only want to see shared projects
|
|
|
|
if $scope.filter == "shared" and project.accessLevel == "owner"
|
|
|
|
visible = false
|
|
|
|
|
|
|
|
# Hide projects we don't own if we only want to see owned projects
|
|
|
|
if $scope.filter == "owned" and project.accessLevel != "owner"
|
|
|
|
visible = false
|
|
|
|
|
|
|
|
if $scope.filter == "archived"
|
|
|
|
# Only show archived projects
|
|
|
|
if !project.archived
|
|
|
|
visible = false
|
|
|
|
else
|
|
|
|
# Only show non-archived projects
|
|
|
|
if project.archived
|
|
|
|
visible = false
|
|
|
|
|
|
|
|
if visible
|
|
|
|
$scope.visibleProjects.push project
|
|
|
|
else
|
|
|
|
# We don't want hidden selections
|
|
|
|
project.selected = false
|
|
|
|
$scope.updateSelectedProjects()
|
|
|
|
|
|
|
|
$scope.getSelectedTag = () ->
|
|
|
|
for tag in $scope.tags
|
|
|
|
return tag if tag.selected
|
|
|
|
return null
|
|
|
|
|
|
|
|
$scope._removeProjectIdsFromTagArray = (tag, remove_project_ids) ->
|
|
|
|
# Remove project_id from tag.project_ids
|
|
|
|
remaining_project_ids = []
|
|
|
|
removed_project_ids = []
|
|
|
|
for project_id in tag.project_ids
|
|
|
|
if project_id not in remove_project_ids
|
|
|
|
remaining_project_ids.push project_id
|
|
|
|
else
|
|
|
|
removed_project_ids.push project_id
|
|
|
|
tag.project_ids = remaining_project_ids
|
|
|
|
return removed_project_ids
|
|
|
|
|
|
|
|
$scope._removeProjectFromList = (project) ->
|
|
|
|
index = $scope.projects.indexOf(project)
|
|
|
|
if index > -1
|
|
|
|
$scope.projects.splice(index, 1)
|
|
|
|
|
|
|
|
$scope.removeSelectedProjectsFromTag = (tag) ->
|
|
|
|
tag.showWhenEmpty = true
|
|
|
|
|
|
|
|
selected_project_ids = $scope.getSelectedProjectIds()
|
|
|
|
selected_projects = $scope.getSelectedProjects()
|
|
|
|
|
|
|
|
removed_project_ids = $scope._removeProjectIdsFromTagArray(tag, selected_project_ids)
|
|
|
|
|
|
|
|
# Remove tag from project.tags
|
|
|
|
remaining_tags = []
|
|
|
|
for project in selected_projects
|
|
|
|
project.tags ||= []
|
|
|
|
index = project.tags.indexOf tag
|
|
|
|
if index > -1
|
|
|
|
project.tags.splice(index, 1)
|
|
|
|
|
|
|
|
for project_id in removed_project_ids
|
2016-01-29 07:25:31 -05:00
|
|
|
queuedHttp({
|
|
|
|
method: "DELETE"
|
|
|
|
url: "/tag/#{tag._id}/project/#{project_id}"
|
|
|
|
headers:
|
|
|
|
"X-CSRF-Token": window.csrfToken
|
|
|
|
})
|
2014-07-08 07:02:26 -04:00
|
|
|
|
|
|
|
# If we're filtering by this tag then we need to remove
|
|
|
|
# the projects from view
|
|
|
|
$scope.updateVisibleProjects()
|
|
|
|
|
|
|
|
$scope.addSelectedProjectsToTag = (tag) ->
|
|
|
|
selected_projects = $scope.getSelectedProjects()
|
2014-07-21 06:33:10 -04:00
|
|
|
event_tracking.send 'project-list-page-interaction', 'project action', 'addSelectedProjectsToTag'
|
2014-07-08 07:02:26 -04:00
|
|
|
|
|
|
|
# Add project_ids into tag.project_ids
|
|
|
|
added_project_ids = []
|
|
|
|
for project_id in $scope.getSelectedProjectIds()
|
|
|
|
unless project_id in tag.project_ids
|
|
|
|
tag.project_ids.push project_id
|
|
|
|
added_project_ids.push project_id
|
|
|
|
|
|
|
|
# Add tag into each project.tags
|
|
|
|
for project in selected_projects
|
|
|
|
project.tags ||= []
|
|
|
|
unless tag in project.tags
|
|
|
|
project.tags.push tag
|
|
|
|
|
|
|
|
for project_id in added_project_ids
|
2016-01-29 08:29:25 -05:00
|
|
|
queuedHttp.post "/tag/#{tag._id}/project/#{project_id}", {
|
2014-07-08 07:02:26 -04:00
|
|
|
_csrf: window.csrfToken
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.createTag = (name) ->
|
2014-10-09 03:39:15 -04:00
|
|
|
return tag
|
2014-07-08 07:02:26 -04:00
|
|
|
|
|
|
|
$scope.openNewTagModal = (e) ->
|
|
|
|
modalInstance = $modal.open(
|
|
|
|
templateUrl: "newTagModalTemplate"
|
|
|
|
controller: "NewTagModalController"
|
|
|
|
)
|
|
|
|
|
|
|
|
modalInstance.result.then(
|
2016-01-29 10:11:27 -05:00
|
|
|
(tag) ->
|
|
|
|
$scope.tags.push tag
|
2014-10-09 03:39:15 -04:00
|
|
|
$scope.addSelectedProjectsToTag(tag)
|
2014-07-08 07:02:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
$scope.createProject = (name, template = "none") ->
|
|
|
|
deferred = $q.defer()
|
|
|
|
|
|
|
|
queuedHttp
|
|
|
|
.post("/project/new", {
|
|
|
|
_csrf: window.csrfToken
|
|
|
|
projectName: name
|
|
|
|
template: template
|
|
|
|
})
|
|
|
|
.success((data, status, headers, config) ->
|
|
|
|
$scope.projects.push {
|
|
|
|
name: name
|
|
|
|
_id: data.project_id
|
|
|
|
accessLevel: "owner"
|
|
|
|
# TODO: Check access level if correct after adding it in
|
|
|
|
# to the rest of the app
|
|
|
|
}
|
|
|
|
$scope.updateVisibleProjects()
|
|
|
|
deferred.resolve(data.project_id)
|
|
|
|
)
|
|
|
|
.error((data, status, headers, config) ->
|
|
|
|
deferred.reject()
|
|
|
|
)
|
|
|
|
|
|
|
|
return deferred.promise
|
|
|
|
|
|
|
|
$scope.openCreateProjectModal = (template = "none") ->
|
2014-07-21 06:33:10 -04:00
|
|
|
event_tracking.send 'project-list-page-interaction', 'new-project', template
|
2014-07-08 07:02:26 -04:00
|
|
|
modalInstance = $modal.open(
|
|
|
|
templateUrl: "newProjectModalTemplate"
|
|
|
|
controller: "NewProjectModalController"
|
|
|
|
resolve:
|
|
|
|
template: () -> template
|
|
|
|
scope: $scope
|
|
|
|
)
|
|
|
|
|
|
|
|
modalInstance.result.then (project_id) ->
|
|
|
|
window.location = "/project/#{project_id}"
|
|
|
|
|
2016-02-23 10:00:04 -05:00
|
|
|
MAX_PROJECT_NAME_LENGTH = 150
|
2014-07-08 07:02:26 -04:00
|
|
|
$scope.renameProject = (project, newName) ->
|
2016-02-23 10:00:04 -05:00
|
|
|
if !newName? or newName.length == 0 or newName.length > MAX_PROJECT_NAME_LENGTH
|
|
|
|
return
|
|
|
|
project.name = newName
|
2014-07-08 07:02:26 -04:00
|
|
|
queuedHttp.post "/project/#{project.id}/rename", {
|
2014-10-07 06:06:37 -04:00
|
|
|
newProjectName: project.name
|
2014-07-08 07:02:26 -04:00
|
|
|
_csrf: window.csrfToken
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.openRenameProjectModal = () ->
|
|
|
|
project = $scope.getFirstSelectedProject()
|
|
|
|
return if !project? or project.accessLevel != "owner"
|
2014-07-21 06:33:10 -04:00
|
|
|
event_tracking.send 'project-list-page-interaction', 'project action', 'Rename'
|
2014-07-08 07:02:26 -04:00
|
|
|
modalInstance = $modal.open(
|
|
|
|
templateUrl: "renameProjectModalTemplate"
|
|
|
|
controller: "RenameProjectModalController"
|
|
|
|
resolve:
|
|
|
|
projectName: () -> project.name
|
|
|
|
)
|
|
|
|
|
|
|
|
modalInstance.result.then(
|
|
|
|
(newName) ->
|
|
|
|
$scope.renameProject(project, newName)
|
|
|
|
)
|
|
|
|
|
|
|
|
$scope.cloneProject = (project, cloneName) ->
|
|
|
|
deferred = $q.defer()
|
2014-07-21 06:33:10 -04:00
|
|
|
event_tracking.send 'project-list-page-interaction', 'project action', 'Clone'
|
2014-07-08 07:02:26 -04:00
|
|
|
queuedHttp
|
|
|
|
.post("/project/#{project.id}/clone", {
|
|
|
|
_csrf: window.csrfToken
|
|
|
|
projectName: cloneName
|
|
|
|
})
|
|
|
|
.success((data, status, headers, config) ->
|
|
|
|
$scope.projects.push {
|
|
|
|
name: cloneName
|
|
|
|
id: data.project_id
|
|
|
|
accessLevel: "owner"
|
|
|
|
# TODO: Check access level if correct after adding it in
|
|
|
|
# to the rest of the app
|
|
|
|
}
|
|
|
|
$scope.updateVisibleProjects()
|
|
|
|
deferred.resolve(data.project_id)
|
|
|
|
)
|
|
|
|
.error((data, status, headers, config) ->
|
|
|
|
deferred.reject()
|
|
|
|
)
|
|
|
|
|
|
|
|
return deferred.promise
|
|
|
|
|
|
|
|
$scope.openCloneProjectModal = () ->
|
|
|
|
project = $scope.getFirstSelectedProject()
|
|
|
|
return if !project?
|
|
|
|
|
|
|
|
modalInstance = $modal.open(
|
|
|
|
templateUrl: "cloneProjectModalTemplate"
|
|
|
|
controller: "CloneProjectModalController"
|
|
|
|
resolve:
|
|
|
|
project: () -> project
|
|
|
|
scope: $scope
|
|
|
|
)
|
|
|
|
|
|
|
|
$scope.openArchiveProjectsModal = () ->
|
|
|
|
modalInstance = $modal.open(
|
|
|
|
templateUrl: "deleteProjectsModalTemplate"
|
|
|
|
controller: "DeleteProjectsModalController"
|
|
|
|
resolve:
|
|
|
|
projects: () -> $scope.getSelectedProjects()
|
|
|
|
)
|
2014-07-21 06:33:10 -04:00
|
|
|
event_tracking.send 'project-list-page-interaction', 'project action', 'Delete'
|
2014-07-08 07:02:26 -04:00
|
|
|
modalInstance.result.then () ->
|
|
|
|
$scope.archiveOrLeaveSelectedProjects()
|
|
|
|
|
|
|
|
$scope.archiveOrLeaveSelectedProjects = () ->
|
|
|
|
selected_projects = $scope.getSelectedProjects()
|
|
|
|
selected_project_ids = $scope.getSelectedProjectIds()
|
|
|
|
|
|
|
|
# Remove project from any tags
|
|
|
|
for tag in $scope.tags
|
|
|
|
$scope._removeProjectIdsFromTagArray(tag, selected_project_ids)
|
|
|
|
|
|
|
|
for project in selected_projects
|
2016-01-29 10:11:27 -05:00
|
|
|
project.tags = []
|
2014-07-08 07:02:26 -04:00
|
|
|
if project.accessLevel == "owner"
|
|
|
|
project.archived = true
|
|
|
|
queuedHttp {
|
|
|
|
method: "DELETE"
|
|
|
|
url: "/project/#{project.id}"
|
|
|
|
headers:
|
|
|
|
"X-CSRF-Token": window.csrfToken
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$scope._removeProjectFromList project
|
|
|
|
|
|
|
|
queuedHttp {
|
|
|
|
method: "POST"
|
|
|
|
url: "/project/#{project.id}/leave"
|
|
|
|
headers:
|
|
|
|
"X-CSRF-Token": window.csrfToken
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.updateVisibleProjects()
|
|
|
|
|
|
|
|
|
|
|
|
$scope.openDeleteProjectsModal = () ->
|
|
|
|
modalInstance = $modal.open(
|
|
|
|
templateUrl: "deleteProjectsModalTemplate"
|
|
|
|
controller: "DeleteProjectsModalController"
|
|
|
|
resolve:
|
|
|
|
projects: () -> $scope.getSelectedProjects()
|
|
|
|
)
|
|
|
|
|
|
|
|
modalInstance.result.then () ->
|
|
|
|
$scope.deleteSelectedProjects()
|
|
|
|
|
|
|
|
$scope.deleteSelectedProjects = () ->
|
|
|
|
selected_projects = $scope.getSelectedProjects()
|
|
|
|
selected_project_ids = $scope.getSelectedProjectIds()
|
|
|
|
|
|
|
|
# Remove projects from array
|
|
|
|
for project in selected_projects
|
|
|
|
$scope._removeProjectFromList project
|
|
|
|
|
|
|
|
# Remove project from any tags
|
|
|
|
for tag in $scope.tags
|
|
|
|
$scope._removeProjectIdsFromTagArray(tag, selected_project_ids)
|
|
|
|
|
|
|
|
for project_id in selected_project_ids
|
|
|
|
queuedHttp {
|
|
|
|
method: "DELETE"
|
|
|
|
url: "/project/#{project_id}?forever=true"
|
|
|
|
headers:
|
|
|
|
"X-CSRF-Token": window.csrfToken
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.updateVisibleProjects()
|
|
|
|
|
|
|
|
$scope.restoreSelectedProjects = () ->
|
|
|
|
selected_projects = $scope.getSelectedProjects()
|
|
|
|
selected_project_ids = $scope.getSelectedProjectIds()
|
|
|
|
|
|
|
|
for project in selected_projects
|
|
|
|
project.archived = false
|
|
|
|
|
|
|
|
for project_id in selected_project_ids
|
|
|
|
queuedHttp {
|
|
|
|
method: "POST"
|
|
|
|
url: "/project/#{project_id}/restore"
|
|
|
|
headers:
|
|
|
|
"X-CSRF-Token": window.csrfToken
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.updateVisibleProjects()
|
|
|
|
|
|
|
|
$scope.openUploadProjectModal = () ->
|
|
|
|
modalInstance = $modal.open(
|
|
|
|
templateUrl: "uploadProjectModalTemplate"
|
|
|
|
controller: "UploadProjectModalController"
|
|
|
|
)
|
|
|
|
|
|
|
|
$scope.downloadSelectedProjects = () ->
|
|
|
|
selected_project_ids = $scope.getSelectedProjectIds()
|
2014-07-21 06:33:10 -04:00
|
|
|
event_tracking.send 'project-list-page-interaction', 'project action', 'Download Zip'
|
2014-07-08 07:02:26 -04:00
|
|
|
if selected_project_ids.length > 1
|
|
|
|
path = "/project/download/zip?project_ids=#{selected_project_ids.join(',')}"
|
|
|
|
else
|
|
|
|
path = "/project/#{selected_project_ids[0]}/download/zip"
|
|
|
|
|
|
|
|
window.location = path
|
2014-07-18 09:06:37 -04:00
|
|
|
|
|
|
|
$scope.updateVisibleProjects()
|
2014-07-08 07:02:26 -04:00
|
|
|
|
|
|
|
App.controller "ProjectListItemController", ($scope) ->
|
|
|
|
$scope.ownerName = () ->
|
|
|
|
if $scope.project.accessLevel == "owner"
|
|
|
|
return "You"
|
|
|
|
else if $scope.project.owner?
|
|
|
|
return "#{$scope.project.owner.first_name} #{$scope.project.owner.last_name}"
|
|
|
|
else
|
|
|
|
return "?"
|
|
|
|
|
|
|
|
$scope.$watch "project.selected", (value) ->
|
|
|
|
if value?
|
2014-09-01 13:05:51 -04:00
|
|
|
$scope.updateSelectedProjects()
|