-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracting sync-mode and editors modules
- Loading branch information
1 parent
69ee140
commit c913a12
Showing
7 changed files
with
262 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
app/assets/javascripts/mumuki_laboratory/application/editors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* This module allows to read and write the current editor's contents | ||
* regardless if it is an standard editor or a custom editor | ||
*/ | ||
mumuki.editors = { | ||
/** | ||
* @type {() => void} | ||
*/ | ||
_contentSyncer: null, | ||
|
||
/** | ||
* Updates the current editor's content | ||
* | ||
* @param {string} content | ||
*/ | ||
setContent(content) { | ||
const $customEditor = $('#mu-custom-editor-value'); | ||
if ($customEditor.length) { | ||
$customEditor.val(content); | ||
} else { | ||
mumuki.editor.setContent(content); | ||
} | ||
}, | ||
|
||
|
||
/** | ||
* @returns {EditorProperty[]} | ||
*/ | ||
getContents() { | ||
return mumuki.CustomEditor.hasSources ? | ||
mumuki.CustomEditor.getContents() : | ||
this.getStandardEditorContents(); | ||
}, | ||
|
||
/** | ||
* Syncs and returns the content objects of the standard editor form | ||
* | ||
* This content object may include keys like {@code content}, | ||
* {@code content_extra} and {@code content_test} | ||
* | ||
* @returns {EditorProperty[]} | ||
*/ | ||
getStandardEditorContents() { | ||
this._syncContent(); | ||
return $('.new_solution').serializeArray(); | ||
}, | ||
|
||
/** | ||
* Answers a submission object with a key for each of the current | ||
* editor sources. | ||
* | ||
* This method will use CustomEditor's sources if availble, or | ||
* standard editor's content sources otherwise | ||
* | ||
* @returns {Submission} | ||
*/ | ||
getSubmission() { | ||
let content = {}; | ||
let contents = this.getContents(); | ||
contents.forEach((it) => { | ||
content[it.name] = it.value; | ||
}); | ||
return content; | ||
}, | ||
|
||
/** | ||
* Copies current solution from it native rendering components | ||
* to the appropriate submission form elements. | ||
* | ||
* Both editors and runners with a custom editor that don't register a source should | ||
* register its own syncer function in order to {@link syncContent} work properly. | ||
* | ||
* @see registerContentSyncer | ||
* @see CustomEditor#addSource | ||
*/ | ||
_syncContent() { | ||
if (this._contentSyncer) { | ||
this._contentSyncer(); | ||
} | ||
}, | ||
|
||
/** | ||
* Sets a content syncer, that will be used by {@link _syncContent} | ||
* in ordet to dump solution into the submission form fields. | ||
* | ||
* Each editor should have its own syncer registered - otherwise previous or none may be used | ||
* causing unpredicatble behaviours - or cleared by passing {@code null}. | ||
* | ||
* As a particular case, runners with custom editors that don't add sources using {@link CustomEditor#addSource} | ||
* should set the {@code #mu-custom-editor-value} value within its syncer. | ||
* | ||
* @param {() => void} [syncer] the syncer, or null, if no sync'ing is needed | ||
*/ | ||
registerContentSyncer(syncer = null) { | ||
this._contentSyncer = syncer; | ||
}, | ||
|
||
/** | ||
* @param {CustomEditorSource} source | ||
*/ | ||
addCustomSource(source) { | ||
mumuki.CustomEditor.addSource(source) | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
app/assets/javascripts/mumuki_laboratory/application/sync-mode.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
mumuki.syncMode = (() => { | ||
|
||
/** | ||
* Syncs progress and solutions | ||
* from local storage | ||
*/ | ||
class ClientSyncMode { | ||
syncProgress() { | ||
mumuki.progress.updateWholeProgressBar($anchor => this._getProgressListItemClass($anchor)); | ||
} | ||
|
||
syncEditorContent() { | ||
const lastSubmission = mumuki.SubmissionsStore.getLastSubmission(mumuki.currentExerciseId); | ||
if (lastSubmission) { | ||
/** @todo extract core module */ | ||
const content = mumuki.SubmissionsStore.submissionSolutionContent(lastSubmission.submission); | ||
mumuki.editors.setContent(content); | ||
} | ||
} | ||
|
||
/** | ||
* @param {JQuery} $anchor | ||
*/ | ||
_getProgressListItemClass($anchor) { | ||
const exerciseId = $anchor.data('mu-exercise-id'); | ||
const status = mumuki.SubmissionsStore.getLastSubmissionStatus(exerciseId); | ||
return mumuki.renderers.progressListItemClassForStatus(status, exerciseId == mumuki.currentExerciseId); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Syncs progress and solutions | ||
* from server. | ||
* | ||
* This class does actually nothing | ||
* since that behaviour is actually the default one, son no additional actions | ||
* are nedeed. | ||
*/ | ||
class ServerSyncMode { | ||
syncProgress() { | ||
// nothing | ||
} | ||
|
||
syncEditorContent() { | ||
// nothing | ||
} | ||
} | ||
|
||
|
||
/** Selects the most appropriate sync mode */ | ||
function _selectSyncMode() { | ||
if (mumuki.incognitoMode) { | ||
mumuki.syncMode._current = new ClientSyncMode(); | ||
} else { | ||
mumuki.syncMode._current = new ServerSyncMode(); | ||
} | ||
} | ||
|
||
mumuki.load(() => { | ||
mumuki.syncMode._selectSyncMode(); | ||
mumuki.syncMode._current.syncProgress(); | ||
mumuki.syncMode._current.syncEditorContent(); | ||
}) | ||
|
||
return { | ||
ServerSyncMode, | ||
ClientSyncMode, | ||
|
||
_selectSyncMode, | ||
|
||
/** @type {ClientSyncMode|ServerSyncMode}*/ | ||
_current: null | ||
} | ||
})(); |
Oops, something went wrong.