{"version":3,"file":"DurationPickerMaker.min.js","sources":["https:\/\/moodle.sonsbeekmedia.nl\/caie_39\/mod\/teachingtools\/amd\/src\/duration\/DurationPickerMaker.js"],"sourcesContent":["\/\/ Version 1.0\n\n\nexport default class DurationPickerMaker {\n\n TIME_CHUNK_SELECTION_ATTR_NAME = \"time-chunk-selection-mode\";\n\n _SelectTextInTargetElement(event) {\n let selectedTimeChunkName = this._formattedDuration.GetSelectedTimeChunk(event.target.selectionStart);\n let selectionRange = this._formattedDuration.GetIndexRangeForTimeChunk(selectedTimeChunkName);\n event.target.setAttribute(this.TIME_CHUNK_SELECTION_ATTR_NAME, selectedTimeChunkName);\n event.target.setSelectionRange(selectionRange.startIndex, selectionRange.endIndex);\n };\n\n _HighlightArea(inputBox, range) {\n inputBox.focus();\n inputBox.select();\n inputBox.selectionStart = range.startIndex;\n inputBox.endIndex = range.endIndex;\n }\n\n _SetValueAndNotifyObservers()\n {\n this._targetElement.value = this._formattedDuration.ToFormattedString();\n this._NotifySecondValueObservers(this._formattedDuration.ToTotalSeconds());\n }\n IncrementSeconds()\n {\n \/\/this._formattedDuration.AddSeconds(1);\n this._formattedDuration.SubtractSeconds(1);\n this._SetValueAndNotifyObservers();\n }\n\n _GetCursorPosition()\n {\n let field = this._targetElement;\n if (field.selectionStart || field.selectionStart === '0' || field.selectionStart === 0)\n {\n return field.selectionDirection==='backward' ? field.selectionStart : field.selectionEnd;\n }\n return null;\n }\n\n ResetSeconds() {\n this._formattedDuration.SetTotalSeconds(0);\n this._SetValueAndNotifyObservers();\n }\n\n SetSeconds(seconds) {\n this._formattedDuration.SetTotalSeconds(seconds);\n this._SetValueAndNotifyObservers();\n }\n\n GetSeconds() {\n return this._formattedDuration.ToTotalSeconds();\n }\n\n _ChangeValueDueToUpOrDownArrowKeyPressed(targetElement, direction) {\n const selectedChunkName = targetElement.getAttribute(this.TIME_CHUNK_SELECTION_ATTR_NAME);\n if (direction === \"up\") {\n this._formattedDuration.IncrementValueForTimeChunk(selectedChunkName);\n } else if (direction === 'down') {\n this._formattedDuration.DecrementValueTimeChunk(selectedChunkName);\n }\n this._SetValueAndNotifyObservers();\n this._HighlightArea(targetElement, this._formattedDuration.GetIndexRangeForTimeChunk(selectedChunkName));\n };\n\n _ShiftFocusLeft(inputBox) {\n let chunkName = inputBox.getAttribute(this.TIME_CHUNK_SELECTION_ATTR_NAME);\n this._SetValueAndNotifyObservers();\n if (chunkName === this._formattedDuration.HOURS_CHUNK || chunkName === this._formattedDuration.MINUTES_CHUNK) {\n this._HighlightArea(inputBox, this._formattedDuration.GetIndexRangeForHoursChunk());\n return;\n }\n this._HighlightArea(inputBox, this._formattedDuration.GetIndexRangeForMinutesChunk());\n }\n\n _ShiftFocusRight(targetElement) {\n let chunkName = targetElement.getAttribute(this.TIME_CHUNK_SELECTION_ATTR_NAME);\n this._SetValueAndNotifyObservers();\n if (chunkName === this._formattedDuration.SECONDS_CHUNK || chunkName === this._formattedDuration.MINUTES_CHUNK) {\n this._HighlightArea(targetElement, this._formattedDuration.GetIndexRangeForSecondsChunk());\/\/ select hours area as it was selectee\n return;\n }\n this._HighlightArea(targetElement, this._formattedDuration.GetIndexRangeForMinutesChunk())\n }\n\n _SetFormattedStringFromTargetIfValid(event) {\n let currentValue = event.target.value;\n if (this._formattedDuration.IsFormattedStringValid(currentValue)) {\n this._formattedDuration.FromFormattedString(currentValue);\n }\n };\n\n _SetValueFromFormattedStringButDontLooseSelection(){\n let previousCursorPos = this._GetCursorPosition();\n if( previousCursorPos !== null)\n {\n let selectedChunkName = this._formattedDuration.GetSelectedTimeChunk(previousCursorPos);\n this._SetValueAndNotifyObservers();\n this._HighlightArea(this._targetElement, this._formattedDuration.GetIndexRangeForTimeChunk(selectedChunkName));\n }\n else\n {\n \/\/ \"could not set value, previousCursorPos is null\"\n }\n }\n\n _HandleKeyDown(event) {\n if (event.key === 'ArrowDown' || event.key === 'ArrowUp' || event.key === 'ArrowLeft' || event.key === 'ArrowRight') {\n switch (event.key) {\n \/\/ use up and down arrow keys to increase value;\n case 'ArrowDown':\n this._ChangeValueDueToUpOrDownArrowKeyPressed(event.target, 'down');\n break;\n case 'ArrowUp':\n this._ChangeValueDueToUpOrDownArrowKeyPressed(event.target, 'up');\n break;\n \/\/ use left and right arrow keys to shift focus;\n case 'ArrowLeft':\n this._ShiftFocusLeft(event.target);\n break;\n case 'ArrowRight':\n this._ShiftFocusRight(event.target);\n break;\n }\n event.preventDefault();\n }\n \/\/ The following keys will be accepted when the input field is selected\n const acceptedKeys = ['Backspace', 'ArrowDown', 'ArrowUp', 'Tab'];\n if (isNaN(event.key) && !acceptedKeys.includes(event.key)) {\n event.preventDefault();\n }\n };\n\n constructor(formattedDuration) {\n this._formattedDuration = formattedDuration;\n this._secondValueObservers = [];\n this._targetElement = null;\n }\n\n SetPickerElement(targetElement) {\n \/\/ todo: add validation when target element is null or does not have some properties (e.g. value)\n if (targetElement.getAttribute('data-upgraded') === 'true') {\n return; \/\/ in case some developer calls this or includes it twice\n }\n this._targetElement = targetElement;\n targetElement.setAttribute('data-upgraded', true);\n targetElement.value = this._formattedDuration.ToFormattedString();\n this._ConnectEvents(targetElement);\n this._NotifySecondValueObservers(this._formattedDuration.ToTotalSeconds());\n }\n\n _NotifySecondValueObservers(newValue) {\n this._secondValueObservers.forEach(function (item, index, array) {\n \/\/ assuming that observer has method: setSecondsValue([int])\n item.setSecondsValue(newValue)\n })\n }\n\n AddSecondChangeObserver(secondChangeObservers) {\n this._secondValueObservers.push(secondChangeObservers);\n this._NotifySecondValueObservers(this._formattedDuration.ToTotalSeconds());\n }\n\n _OnKeyUpHandler(event) {\n this._SetFormattedStringFromTargetIfValid(event );\n this._NotifySecondValueObservers(this._formattedDuration.ToTotalSeconds());\n }\n\n _OnKeyDownHandler(event) {\n this._HandleKeyDown(event);\n }\n\n _OnSelectHandler(event) {\n this._SelectTextInTargetElement(event);\n }\n\n _OnMouseUpHandler(event) {\n this._SelectTextInTargetElement(event);\n this._SetValueFromFormattedStringButDontLooseSelection();\n }\n\n _OnChangeHandler(event) {\n }\n\n _OnBlurHandler(event) {\n this._SetFormattedStringFromTargetIfValid(event);\n this._SetValueAndNotifyObservers();\n }\n\n _ConnectEvents(pickerElement) {\n pickerElement.addEventListener('keydown', (event) => this._OnKeyDownHandler(event));\n pickerElement.addEventListener('select', (event) => this._OnSelectHandler(event));\n pickerElement.addEventListener('mouseup', (event) => this._OnMouseUpHandler(event));\n pickerElement.addEventListener('change', (event) => this._OnChangeHandler(event));\n pickerElement.addEventListener('blur', (event) => this._OnBlurHandler(event));\n pickerElement.addEventListener('keyup', (event) => this._OnKeyUpHandler(event));\n pickerElement.addEventListener('drop', (event) => event.preventDefault());\n }\n}\n\n"],"names":["_SelectTextInTargetElement","event","selectedTimeChunkName","this","_formattedDuration","GetSelectedTimeChunk","target","selectionStart","selectionRange","GetIndexRangeForTimeChunk","setAttribute","TIME_CHUNK_SELECTION_ATTR_NAME","setSelectionRange","startIndex","endIndex","_HighlightArea","inputBox","range","focus","select","_SetValueAndNotifyObservers","_targetElement","value","ToFormattedString","_NotifySecondValueObservers","ToTotalSeconds","IncrementSeconds","SubtractSeconds","_GetCursorPosition","field","selectionDirection","selectionEnd","ResetSeconds","SetTotalSeconds","SetSeconds","seconds","GetSeconds","_ChangeValueDueToUpOrDownArrowKeyPressed","targetElement","direction","selectedChunkName","getAttribute","IncrementValueForTimeChunk","DecrementValueTimeChunk","_ShiftFocusLeft","chunkName","HOURS_CHUNK","MINUTES_CHUNK","GetIndexRangeForMinutesChunk","GetIndexRangeForHoursChunk","_ShiftFocusRight","SECONDS_CHUNK","GetIndexRangeForSecondsChunk","_SetFormattedStringFromTargetIfValid","currentValue","IsFormattedStringValid","FromFormattedString","_SetValueFromFormattedStringButDontLooseSelection","previousCursorPos","_HandleKeyDown","key","preventDefault","isNaN","includes","constructor","formattedDuration","_secondValueObservers","SetPickerElement","_ConnectEvents","newValue","forEach","item","index","array","setSecondsValue","AddSecondChangeObserver","secondChangeObservers","push","_OnKeyU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