(function() { var dialog, promise, transferred; _.extend(scrivito, { task_list_dialog: { open: function() { promise = $.Deferred(); transferred = []; dialog = $(scrivito.template.render('task_list_dialog')).appendTo($('#scrivito_editing')); load_task_list(); scrivito.dialog.open_and_adjust_without_transition(dialog); return scrivito.withDialogBehaviour(dialog, promise, {escape: close}); } } }); var fetch_task_list = function(mode) { var base_url = window.location.protocol + '//' + window.location.host + '/__scrivito/'; var path = 'tasks'; // the other option is 'all' mode = mode || 'user'; return $.ajax(base_url + path, { type: 'GET', data: {mode: mode}, dataType: 'json', cache: false }); }; var load_task_list = function(mode) { var objs; render(mode); fetch_task_list(mode).then(function(result, text_status, xhr) { var tasks = result.tasks; _.each(tasks, function(task) { task.obj_data = task.obj; task.obj = scrivito.legacy_obj.create_instance(task.obj); task.last_changed = string_date_to_function(task.last_changed); }); render(mode, tasks); }); }; var string_date_to_function = function(string_date) { return function() { return scrivito.types.deserializeAsDate(string_date); }; }; var render = function(mode, tasks) { mode = mode || 'user'; dialog.html(scrivito.template.render('task_list_dialog/content', { title: scrivito.t('task_list_dialog.title.' + mode), tasks: tasks, is_loaded: !!tasks, mode_button: scrivito.t('task_list_dialog.mode_toggle.' + mode) })); // on click go to page dialog.find('[data-scrivito-obj-id]').on('click', function() { var obj_id = $(this).attr('data-scrivito-obj-id'); var task = _.find(tasks || [], function(task) { return task.obj.id() === obj_id; }); scrivito.open_obj(task.obj); return false; }); scrivito.updateAutoHeightFor(dialog.find('.scrivito_changes_dialog_content')); dialog.find('.scrivito_cancel').on('click', close); // TODO: sortable // TODO: my/all changes // mine/all changes toggle dialog.find('.fiona7_toggle_task_list_type').on('click', function() { if (mode == 'all') { load_task_list('user'); } else { load_task_list('all'); } }); }; var close = function() { scrivito.dialog.close_without_transition(dialog); promise.resolve(transferred); return false; }; }());