diff --git a/frappe/desk/calendar.py b/frappe/desk/calendar.py index d9cd03004a..efe4f3ef20 100644 --- a/frappe/desk/calendar.py +++ b/frappe/desk/calendar.py @@ -24,3 +24,20 @@ def get_event_conditions(doctype, filters=None): frappe.throw(_("Not Permitted"), frappe.PermissionError) return get_filters_cond(doctype, filters, [], with_match_conditions = True) + +@frappe.whitelist() +def get_events(doctype, start, end, field_map, filters=None, fields=None): + field_map = frappe._dict(json.loads(field_map)) + + if filters: + filters = json.loads(filters or '') + + if not fields: + fields = [field_map.start, field_map.end, field_map.title, 'name'] + + filters += [ + [doctype, field_map.start, '<=', end], + [doctype, field_map.end, '>=', start], + ] + + return frappe.get_list(doctype, fields=fields, filters=filters) diff --git a/frappe/desk/doctype/calendar_view/__init__.py b/frappe/desk/doctype/calendar_view/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/frappe/desk/doctype/calendar_view/calendar_view.js b/frappe/desk/doctype/calendar_view/calendar_view.js new file mode 100644 index 0000000000..2087932643 --- /dev/null +++ b/frappe/desk/doctype/calendar_view/calendar_view.js @@ -0,0 +1,29 @@ +// Copyright (c) 2017, Frappe Technologies and contributors +// For license information, please see license.txt + +frappe.ui.form.on('Calendar View', { + onload: function(frm) { + frm.trigger('reference_doctype'); + }, + reference_doctype: function(frm) { + const { reference_doctype } = frm.doc; + if (!reference_doctype) return; + + frappe.model.with_doctype(reference_doctype, () => { + const meta = frappe.get_meta(reference_doctype); + + const subject_options = meta.fields.filter( + df => !frappe.model.no_value_type.includes(df.fieldtype) + ).map(df => df.fieldname); + + const date_options = meta.fields.filter( + df => ['Date', 'Datetime'].includes(df.fieldtype) + ).map(df => df.fieldname) + + frm.set_df_property('subject_field', 'options', subject_options); + frm.set_df_property('start_date_field', 'options', date_options); + frm.set_df_property('end_date_field', 'options', date_options); + frm.refresh(); + }); + } +}); diff --git a/frappe/desk/doctype/calendar_view/calendar_view.json b/frappe/desk/doctype/calendar_view/calendar_view.json new file mode 100644 index 0000000000..bc6f356e56 --- /dev/null +++ b/frappe/desk/doctype/calendar_view/calendar_view.json @@ -0,0 +1,184 @@ +{ + "allow_copy": 0, + "allow_guest_to_view": 0, + "allow_import": 0, + "allow_rename": 0, + "autoname": "Prompt", + "beta": 0, + "creation": "2017-10-23 13:02:10.295824", + "custom": 0, + "docstatus": 0, + "doctype": "DocType", + "document_type": "", + "editable_grid": 1, + "engine": "InnoDB", + "fields": [ + { + "allow_bulk_edit": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "columns": 0, + "fieldname": "reference_doctype", + "fieldtype": "Link", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_global_search": 0, + "in_list_view": 1, + "in_standard_filter": 0, + "label": "Reference DocType", + "length": 0, + "no_copy": 0, + "options": "DocType", + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "remember_last_selected_value": 0, + "report_hide": 0, + "reqd": 1, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, + { + "allow_bulk_edit": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "columns": 0, + "fieldname": "subject_field", + "fieldtype": "Select", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_global_search": 0, + "in_list_view": 1, + "in_standard_filter": 0, + "label": "Subject Field", + "length": 0, + "no_copy": 0, + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "remember_last_selected_value": 0, + "report_hide": 0, + "reqd": 1, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, + { + "allow_bulk_edit": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "columns": 0, + "fieldname": "start_date_field", + "fieldtype": "Select", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_standard_filter": 0, + "label": "Start Date Field", + "length": 0, + "no_copy": 0, + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "remember_last_selected_value": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, + { + "allow_bulk_edit": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "columns": 0, + "fieldname": "end_date_field", + "fieldtype": "Select", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_standard_filter": 0, + "label": "End Date Field", + "length": 0, + "no_copy": 0, + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "remember_last_selected_value": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + } + ], + "has_web_view": 0, + "hide_heading": 0, + "hide_toolbar": 0, + "idx": 0, + "image_view": 0, + "in_create": 0, + "is_submittable": 0, + "issingle": 0, + "istable": 0, + "max_attachments": 0, + "modified": "2017-10-23 13:32:33.994308", + "modified_by": "Administrator", + "module": "Desk", + "name": "Calendar View", + "name_case": "", + "owner": "faris@erpnext.com", + "permissions": [ + { + "amend": 0, + "apply_user_permissions": 0, + "cancel": 0, + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "if_owner": 0, + "import": 0, + "permlevel": 0, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "set_user_permissions": 0, + "share": 1, + "submit": 0, + "write": 1 + } + ], + "quick_entry": 0, + "read_only": 0, + "read_only_onload": 0, + "show_name_in_global_search": 0, + "sort_field": "modified", + "sort_order": "DESC", + "track_changes": 0, + "track_seen": 0 +} \ No newline at end of file diff --git a/frappe/desk/doctype/calendar_view/calendar_view.py b/frappe/desk/doctype/calendar_view/calendar_view.py new file mode 100644 index 0000000000..6bb9f6e90a --- /dev/null +++ b/frappe/desk/doctype/calendar_view/calendar_view.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2017, Frappe Technologies and contributors +# For license information, please see license.txt + +from __future__ import unicode_literals +import frappe +from frappe.model.document import Document + +class CalendarView(Document): + pass diff --git a/frappe/public/js/frappe/list/list_sidebar.js b/frappe/public/js/frappe/list/list_sidebar.js index 744a19f770..68f250da9d 100644 --- a/frappe/public/js/frappe/list/list_sidebar.js +++ b/frappe/public/js/frappe/list/list_sidebar.js @@ -27,6 +27,7 @@ frappe.views.ListSidebar = Class.extend({ this.setup_assigned_to_me(); this.setup_views(); this.setup_kanban_boards(); + this.setup_calendar_view(); this.setup_email_inbox(); let limits = frappe.boot.limits; @@ -271,6 +272,45 @@ frappe.views.ListSidebar = Class.extend({ } }); }, + setup_calendar_view: function() { + const doctype = this.doctype; + + frappe.db.get_list('Calendar View', { + filters: { + reference_doctype: doctype + } + }) + .then(result => { + if (!result) return; + const calendar_views = result; + const $link_calendar = this.sidebar.find('.list-link[data-view="Calendar"]'); + + let default_link = ''; + if (frappe.views.calendar[this.doctype]) { + // has standard calendar view + default_link = `
  • ${ __("Default") }
  • ` + } + const other_links = calendar_views.map( + calendar_view => `
  • + ${ __(calendar_view.name) } +
  • ` + ).join(''); + + const dropdown_html = ` +
    + + +
    + `; + + $link_calendar.html(dropdown_html); + }); + }, setup_email_inbox: function() { // get active email account for the user and add in dropdown if(this.doctype != "Communication") diff --git a/frappe/public/js/frappe/views/calendar/calendar.js b/frappe/public/js/frappe/views/calendar/calendar.js index f43e721cfb..508be3bcf0 100644 --- a/frappe/public/js/frappe/views/calendar/calendar.js +++ b/frappe/public/js/frappe/views/calendar/calendar.js @@ -8,14 +8,10 @@ frappe.views.CalendarView = frappe.views.ListRenderer.extend({ name: 'Calendar', render_view: function() { var me = this; - var options = { - doctype: this.doctype, - parent: this.wrapper, - page: this.list_view.page, - list_view: this.list_view - } - $.extend(options, frappe.views.calendar[this.doctype]); - this.calendar = new frappe.views.Calendar(options); + this.get_calendar_options() + .then(options => { + this.calendar = new frappe.views.Calendar(options); + }); }, set_defaults: function() { this._super(); @@ -27,6 +23,47 @@ frappe.views.CalendarView = frappe.views.ListRenderer.extend({ get_header_html: function() { return null; }, + get_calendar_options: function() { + let calendar_view = this.user_settings.last_calendar_view; + + if (!calendar_view) { + calendar_view = frappe.get_route()[3] || 'Default'; + } + + // save in user_settings + frappe.model.user_settings.save(this.doctype, 'Calendar', { + last_calendar_view: calendar_view + }); + + const options = { + doctype: this.doctype, + parent: this.wrapper, + page: this.list_view.page, + list_view: this.list_view + } + + return new Promise(resolve => { + if (calendar_view === 'Default') { + Object.assign(options, frappe.views.calendar[this.doctype]) + resolve(options); + } else { + + frappe.model.with_doc('Calendar View', calendar_view, (name, r) => { + const doc = frappe.get_doc('Calendar View', calendar_view); + Object.assign(options, { + field_map: { + id: "name", + start: doc.start_date_field, + end: doc.end_date_field, + title: doc.subject_field + } + }); + + resolve(options); + }); + } + }) + }, required_libs: [ 'assets/frappe/js/lib/fullcalendar/fullcalendar.min.css', 'assets/frappe/js/lib/fullcalendar/fullcalendar.min.js', @@ -127,6 +164,7 @@ frappe.views.Calendar = Class.extend({ args: me.get_args(start, end), callback: function(r) { var events = r.message; + console.log(events); events = me.prepare_events(events); callback(events); } @@ -202,7 +240,8 @@ frappe.views.Calendar = Class.extend({ doctype: this.doctype, start: this.get_system_datetime(start), end: this.get_system_datetime(end), - filters: this.list_view.filter_list.get_filters() + filters: this.list_view.filter_list.get_filters(), + field_map: this.field_map }; return args; },