{"version":3,"file":"page-edit-plan.min.js","sources":["../src/page-edit-plan.js"],"sourcesContent":["/* eslint no-var: \"error\" */\n/* eslint no-unused-vars: \"off\" */\n/* eslint linebreak-style: \"off\" */\n/* eslint no-trailing-spaces: \"off\" */\n/* eslint no-empty-function: \"off\" */\n/* eslint-env es6*/\n// Put this file in path/to/plugin/amd/src\n// You can call it anything you like\n\nimport {call} from 'core/ajax';\nimport notification from 'core/notification';\n\n// Commented out: import {resetAllFormDirtyStates} from 'core_form/changechecker'; // Moodle 4.00+ only\n\nimport Vue from './vue/vue';\n\nimport EditorComponents from './studyplan-editor-components';\nVue.use(EditorComponents);\n\nimport TSComponents from './treestudyplan-components';\nVue.use(TSComponents);\n\nimport ModalComponents from './modedit-modal';\nVue.use(ModalComponents);\nimport Debugger from './util/debugger';\nimport {addBrowserButtonEvent} from './util/browserbuttonevents';\n\nimport {loadStrings} from './util/string-helper';\nimport {processStudyplan} from './studyplan-processor';\nimport {download, upload} from './downloader';\nimport {studyplanTiming} from './util/date-helper';\nimport mFormComponents from \"./util/mform-helper\";\nVue.use(mFormComponents);\n\nimport PortalVue from './portal-vue/portal-vue.esm';\nVue.use(PortalVue);\nimport BootstrapVue from './bootstrap-vue/bootstrap-vue';\nVue.use(BootstrapVue);\n\n\nconst debug = new Debugger(\"treestudyplan\");\n\nlet resetAllFormDirtyStates = () => { };\nimport('core_form/changechecker').then((ns) => {\n debug.info(ns);\n if (ns.resetAllFormDirtyStates) {\n resetAllFormDirtyStates = ns.resetAllFormDirtyStates;\n }\n return;\n}).catch(()=>{});\n\nlet strings = loadStrings({\n studyplan: {\n 'studyplan_select_placeholder': 'studyplan_select_placeholder',\n 'advanced_import_from_file': 'advanced_import_from_file',\n 'advanced_create_from_template': 'advanced_create_from_template',\n 'studyplan_add': \"studyplan_add\",\n },\n});\n\n/**\n * Initialize the Page\n * @param {int} contextid The context we should attempt to work in (1:1 related to the category)\n * @param {int} categoryid The category we shoud attempt to work in (1:1 related to the context)\n * @param {object} options Options to be passed to this script\n */\nexport function init(contextid, categoryid, options) {\n // Make sure the id's are numeric and integer\n if (undefined === contextid || !Number.isInteger(Number(contextid)) || contextid < 1) {\n contextid = 1;\n } else {\n contextid = Number(contextid); // Ensure a numeric value instead of string\n }\n if (undefined === categoryid || !Number.isInteger(Number(categoryid))) {\n categoryid = 0;\n } else {\n categoryid = Number(categoryid); // Ensure a numeric value instead of string\n }\n\n debug.info(\"options\", options);\n if (options !== null && typeof options === 'object' && !Array.isArray(options)) {\n if (!options.defaultAggregation) {\n options.defaultAggregation = \"core\";\n }\n \n } else {\n options = {defaultAggregation: \"core\"};\n }\n\n // Setup the initial Vue app for this page\n let app = new Vue({\n el: '#root',\n data: {\n create: {\n studyplan: {\n name: '',\n shortname: '',\n description: '',\n idnumber: '',\n slots: 4,\n startdate: '2020-08-01',\n enddate: '',\n context: contextid,\n aggregation: options.defaultAggregation,\n 'aggregation_config': '',\n }\n },\n\n activestudyplan: null,\n activepage: null,\n loadingstudyplan: false,\n studyplans: [],\n templatecount: 0,\n\n text: strings.studyplan,\n usedcontexts: [],\n },\n created() {\n this.$root.$on('studyplanRemoved', (studyplan)=>{\n\n if (app.activestudyplan == studyplan) {\n app.activestudyplan = null;\n }\n\n // Remove studyplan from index list\n let index = null;\n for (let idx in app.studyplans) {\n if (app.studyplans[idx].id == studyplan.id) {\n index = idx;\n break;\n }\n }\n if (index) {\n app.studyplans.splice(index, 1);\n }\n\n });\n },\n mounted() {\n this.initialize();\n addBrowserButtonEvent(this.backPressed);\n },\n beforeunmount() {\n\n },\n computed: {\n dropdownTitle() {\n if (this.activestudyplan && this.activestudyplan.name) {\n return this.activestudyplan.name;\n } else {\n return this.text.studyplan_select_placeholder;\n }\n },\n contextid() {\n return contextid;\n },\n\n },\n methods: {\n backPressed() {\n debug.log(\"Back button pressed\");\n if (app.activestudyplan) {\n debug.log(\"Closing studyplan\");\n this.closeStudyplan();\n }\n },\n initialize() {\n call([{\n methodname: 'local_treestudyplan_list_studyplans',\n args: {'context_id': contextid}\n }])[0].then(function(response) {\n const timingval = {future: 0, present: 1, past: 2};\n response.sort((a, b) => {\n const timinga = studyplanTiming(a);\n const timingb = studyplanTiming(b);\n \n let t = timingval[timinga] - timingval[timingb];\n if (t == 0) {\n // Sort by start date if timing is equal\n t = new Date(b.startdate).getTime() - new Date(a.startdate).getTime();\n \n if (t == 0) {\n // Sort by name if timing is equal\n t = a.name.localeCompare(b.name);\n }\n }\n return t;\n });\n app.studyplans = response;\n \n // Load studyplan from hash if applicable\n const hash = location.hash.replace('#', '');\n if (hash) {\n const id = hash;\n for (const p of app.studyplans) {\n if (p.id == id) {\n app.selectStudyplan(id);\n break;\n }\n }\n }\n return;\n }).catch(notification.exception);\n\n call([{\n methodname: 'local_treestudyplan_list_available_categories',\n args: {operation: 'edit', 'refcontext_id': contextid}\n }])[0].then(function(response) {\n app.usedcontexts = response;\n return;\n }).catch(notification.exception);\n\n this.refreshTemplateCount();\n },\n refreshTemplateCount() {\n call([{\n methodname: 'local_treestudyplan_count_templates',\n args: { }\n }])[0].then(function(response) {\n app.templatecount = response;\n return;\n }).catch(notification.exception);\n },\n closeStudyplan() {\n app.activestudyplan = null;\n window.location.hash = '';\n },\n movedStudyplan(plan, from, to) {\n // Reload the page in the new context (needed, since a number of links are not reactive in the page)\n const params = new URLSearchParams(location.search);\n params.delete('categoryid');\n params.set(\"contextid\", to);\n setTimeout(() => {\n // Reload page in a timeout to give other form javasccript the change to remove the beforeunload handler.\n window.location.search = params.toString();\n }, 50);\n },\n onStudyPlanCreated(newstudyplan) {\n if (newstudyplan.context_id != contextid) {\n // Study plan has changed context id - reload page into new context id and show the plan\n const params = new URLSearchParams(location.search);\n params.delete('categoryid');\n params.set(\"contextid\", newstudyplan.context_id);\n // Make sure the form is no longer dirty before reloading the page - avoid beforeunload handler triggering.\n resetAllFormDirtyStates();\n window.location = window.location.pathname + \"?\" + params.toString() + \"#\" + newstudyplan.id;\n } else {\n app.studyplans.push(newstudyplan);\n app.selectStudyplan(newstudyplan.id);\n }\n },\n switchContext(ctxid) {\n const params = new URLSearchParams(location.search);\n params.delete('categoryid');\n params.set('contextid', ctxid);\n setTimeout(() => {\n // Reload page in a timeout to give other form javasccript the change to remove the beforeunload handler.\n window.location.href = window.location.pathname + \"?\" + params.toString();\n }, 50);\n },\n selectStudyplan(studyplanid) {\n // Fetch studyplan\n app.loadingstudyplan = true;\n app.activestudyplan = null;\n call([{\n methodname: 'local_treestudyplan_get_studyplan_map',\n args: {id: studyplanid}\n }])[0].then(function(response) {\n app.activestudyplan = processStudyplan(response);\n debug.info('studyplan processed');\n app.loadingstudyplan = false;\n window.location.hash = app.activestudyplan.id;\n return;\n }).catch(function(error) {\n notification.exception(error);\n app.loadingstudyplan = false;\n });\n },\n importStudyplan() {\n const self = this;\n upload((filename, content)=>{\n call([{\n methodname: 'local_treestudyplan_import_plan',\n args: {\n content: content,\n format: \"application/json\",\n 'context_id': contextid,\n },\n }])[0].then(function(response) {\n if (response.success) {\n self.initialize();\n } else {\n debug.error(\"Import failed: \", response.msg);\n }\n return;\n }).catch(notification.exception);\n }, \"application/json\");\n },\n },\n });\n}\n\n"],"names":["contextid","categoryid","options","undefined","Number","isInteger","debug","info","Array","isArray","defaultAggregation","app","Vue","el","data","create","studyplan","name","shortname","description","idnumber","slots","startdate","enddate","context","aggregation","activestudyplan","activepage","loadingstudyplan","studyplans","templatecount","text","strings","usedcontexts","created","$root","$on","index","idx","id","splice","mounted","initialize","this","backPressed","beforeunmount","computed","dropdownTitle","studyplan_select_placeholder","methods","log","closeStudyplan","methodname","args","then","response","timingval","future","present","past","sort","a","b","timinga","timingb","t","Date","getTime","localeCompare","hash","location","replace","p","selectStudyplan","catch","notification","exception","operation","refreshTemplateCount","window","movedStudyplan","plan","from","to","params","URLSearchParams","search","delete","set","setTimeout","toString","onStudyPlanCreated","newstudyplan","context_id","resetAllFormDirtyStates","pathname","push","switchContext","ctxid","href","studyplanid","error","importStudyplan","self","filename","content","format","success","msg","use","EditorCompon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