<commit_before>async = require('async')

{defaults, required, to_json} = require('misc')

{salvus_client} = require('salvus_client')

{alert_message} = require('alerts')


misc_page = require('misc_page')

templates = $(".salvus-tasks-templates")
task_template = templates.find(".salvus-task")
edit_title_template = templates.find(".salvus-tasks-title-edit")

class exports.Tasks
    constructor: (opts) ->
        opts = defaults opts,
            project_page   : required
        @project_page  = opts.project_page
        @project_id    = opts.project_page.project.project_id
        @task_list_id  = opts.project_page.project.task_list_id
        @element       = templates.find(".salvus-tasks").clone().show()
        @elt_task_list = @element.find(".salvus-tasks-list")
        @init_create_task()
        @last_edited = 0
        @init_showing_done()
        @update_task_list()

    onshow: () =>
        @update_task_list (err, need_to_update) =>
            if err
                alert_message(type:"error", message:"error updating task list -- #{to_json(err)}")
            else
                if need_to_update
                    @render_task_list()

        # to do
        ###
        task = task_template.clone().find(".salvus-task-title").text("sample task")
        @element.append(task)
        ###

    update_task_list: (cb) =>  # cb(error, need_to_update)  -- if need_to_update is true that means the task list changed since last time updated
        # check to see if the task list has changed on the server side, and if so download and re-render it.
        # TODO: this will go away when the hub has a message queue and can send events when task list changes.

        if @_update_lock
            cb('already updating'); return

        @_update_lock = true
        need_to_update = undefined
        async.series([
            (cb) =>
                # create new task list if it doesn't exit
                if @task_list_id
                    cb(); return
                # create a new task list
                salvus_client.create_task_list
                    owners : [@project_id]
                    cb     : (err, task_list_id) =>
                        if err
                            cb(err)
                        else if not task_list_id
                            cb("BUG: no err but task_list_id not true")
                        else
                            # tell project about our new task list
                            @project_page.project.task_list_id = task_list_id
                            @task_list_id = task_list_id
                            salvus_client.set_project_task_list
                                project_id : @project_id
                                task_list_id : task_list_id
                                cb           : cb
            (cb) =>
                # check to see if the backend task list is newer than what is in the browser
                if @last_edited == 0
                    need_to_update = true
                    cb()
                    return
                salvus_client.get_task_list_last_edited
                    task_list_id : @task_list_id
                    project_id   : @project_id
                    cb           : (err, last_edited) =>
                        if err
                            cb(err)
                        else
                            need_to_update = (last_edited > @last_edited)
                            cb()
            (cb) =>
                # update if necessary
                if not need_to_update
                    cb(); return
                salvus_client.get_task_list
                    task_list_id : @task_list_id
                    project_id   : @project_id
                    include_deleted : false
                    cb           : (err, task_list) =>
                        if err
                            cb(err)
                        else
                            @task_list = task_list
                            cb()
        ], (err) =>
            @_update_lock = false
            cb?(err, need_to_update)
        )


    create_task: (opts) =>
        opts = defaults opts,
            title : required
            position : 0
            cb    : required
        salvus_client.create_task
            task_list_id : @task_list_id
            project_id   : @project_id
            title        : opts.title
            position     : opts.position
            cb           : opts.cb

    render_task_list: () =>
        @elt_task_list.empty()
        for task in @task_list.tasks
            @render_task(task)
        if not @current_task and @task_list.tasks.length > 0
            @set_current_task(@task_list.tasks[0])

    render_task: (task) =>
        if not @showing_done and task.done
            return # nothing to do
        t = task_template.clone()
        title = t.find(".salvus-task-title")
        title.text(task.title)
        # TODO -- instead of append, should put in correct position, according to task.position
        @elt_task_list.append(t)
        task.element = t
        t.click () => @set_current_task(task)
        title.click () => @edit_title(task)
        t.find(".salvus-task-viewer-not-done").click () => @mark_task_done(task, true)
        t.find(".salvus-task-viewer-done").click () => @mark_task_done(task, false)
        if task.done
            t.find(".salvus-task-viewer-done").show()
            t.find(".salvus-task-viewer-not-done").hide()
        if @current_task? and task.task_id == @current_task.task_id
            @set_current_task(task)

    set_current_task: (task) =>
        if @current_task?
            @current_task.element.removeClass("salvus-current-task")
        @current_task = task
        task.element.addClass("salvus-current-task")

    edit_title: (task) =>
        e = task.element
        elt_title = e.find(".salvus-task-title")
        elt = edit_title_template.clone()
        elt_title.after(elt)
        elt_title.hide()
        elt.val(task.title)
        elt.focus()
        elt.focusout () =>
            save_title()
        elt.keydown (evt) =>
            if evt.which is 13
                save_title()
                return false<commit_msg>tasks: working on making draggable/re-orderable<commit_after>async = require('async')

{defaults, required, to_json} = require('misc')

{salvus_client} = require('salvus_client')

{alert_message} = require('alerts')


misc_page = require('misc_page')

templates = $(".salvus-tasks-templates")
task_template = templates.find(".salvus-task")
edit_title_template = templates.find(".salvus-tasks-title-edit")

class exports.Tasks
    constructor: (opts) ->
        opts = defaults opts,
            project_page   : required
        @project_page  = opts.project_page
        @project_id    = opts.project_page.project.project_id
        @task_list_id  = opts.project_page.project.task_list_id
        @element       = templates.find(".salvus-tasks").clone().show()
        @elt_task_list = @element.find(".salvus-tasks-list")
        @init_create_task()
        @last_edited = 0
        @init_showing_done()
        @update_task_list()

    onshow: () =>
        @update_task_list (err, need_to_update) =>
            if err
                alert_message(type:"error", message:"error updating task list -- #{to_json(err)}")
            else
                if need_to_update
                    @render_task_list()

        # to do
        ###
        task = task_template.clone().find(".salvus-task-title").text("sample task")
        @element.append(task)
        ###

    update_task_list: (cb) =>  # cb(error, need_to_update)  -- if need_to_update is true that means the task list changed since last time updated
        # check to see if the task list has changed on the server side, and if so download and re-render it.
        # TODO: this will go away when the hub has a message queue and can send events when task list changes.

        if @_update_lock
            cb('already updating'); return

        @_update_lock = true
        need_to_update = undefined
        async.series([
            (cb) =>
                # create new task list if it doesn't exit
                if @task_list_id
                    cb(); return
                # create a new task list
                salvus_client.create_task_list
                    owners : [@project_id]
                    cb     : (err, task_list_id) =>
                        if err
                            cb(err)
                        else if not task_list_id
                            cb("BUG: no err but task_list_id not true")
                        else
                            # tell project about our new task list
                            @project_page.project.task_list_id = task_list_id
                            @task_list_id = task_list_id
                            salvus_client.set_project_task_list
                                project_id : @project_id
                                task_list_id : task_list_id
                                cb           : cb
            (cb) =>
                # check to see if the backend task list is newer than what is in the browser
                if @last_edited == 0
                    need_to_update = true
                    cb()
                    return
                salvus_client.get_task_list_last_edited
                    task_list_id : @task_list_id
                    project_id   : @project_id
                    cb           : (err, last_edited) =>
                        if err
                            cb(err)
                        else
                            need_to_update = (last_edited > @last_edited)
                            cb()
            (cb) =>
                # update if necessary
                if not need_to_update
                    cb(); return
                salvus_client.get_task_list
                    task_list_id : @task_list_id
                    project_id   : @project_id
                    include_deleted : false
                    cb           : (err, task_list) =>
                        if err
                            cb(err)
                        else
                            @task_list = task_list
                            cb()
        ], (err) =>
            @_update_lock = false
            cb?(err, need_to_update)
        )


    create_task: (opts) =>
        opts = defaults opts,
            title : required
            position : 0
            cb    : required
        salvus_client.create_task
            task_list_id : @task_list_id
            project_id   : @project_id
            title        : opts.title
            position     : opts.position
            cb           : opts.cb

    render_task_list: () =>
        @elt_task_list.empty()
        for task in @task_list.tasks
            @render_task(task)
        if not @current_task and @task_list.tasks.length > 0
            @set_current_task(@task_list.tasks[0])

    render_task: (task) =>
        if not @showing_done and task.done
            return # nothing to do
        t = task_template.clone()
        title = t.find(".salvus-task-title")
        title.text(task.title)
        # TODO -- instead of append, should put in correct position, according to task.position
        @elt_task_list.append(t)
        task.element = t
        t.click () => @set_current_task(task)
        title.click () => @edit_title(task)
        t.find(".salvus-task-viewer-not-done").click () => @mark_task_done(task, true)
        t.find(".salvus-task-viewer-done").click () => @mark_task_done(task, false)
        if task.done
            t.find(".salvus-task-viewer-done").show()
            t.find(".salvus-task-viewer-not-done").hide()
        if @current_task? and task.task_id == @current_task.task_id
            @set_current_task(task)
        t.draggable(handle:t.find(".fa-reorder"))

    set_current_task: (task) =>
        if @current_task?
            @current_task.element.removeClass("salvus-current-task")
        @current_task = task
        task.element.addClass("salvus-current-task")

    edit_title: (task) =>
        e = task.element
        elt_title = e.find(".salvus-task-title")
        elt = edit_title_template.clone()
        elt_title.after(elt)
        elt_title.hide()
        elt.val(task.title)
        elt.focus()
        elt.focusout () =>
            save_title()
        elt.keydown (evt) =>
            if evt.which is 13
                save_title()
                return false
{'fraction_non_alphanumeric': 0.09151679134171574, 'fraction_numerical': 0.0011141174598121915, 'mean_word_length': 1.404707233065442, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5737079', 'n_tokens_mistral': 3532, 'n_tokens_neox': 3397, 'n_words': 760}