-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from DigiKlausur/dev
Dev
- Loading branch information
Showing
44 changed files
with
1,801 additions
and
454 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
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
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
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
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
147 changes: 147 additions & 0 deletions
147
e2xgrader/nbextensions/src/common/extra_cells/extended_cell/attachment.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,147 @@ | ||
define([], function() { | ||
|
||
class Observable { | ||
|
||
constructor() { | ||
this.observers = []; | ||
} | ||
|
||
registerObserver(observer) { | ||
this.observers.push(observer); | ||
} | ||
|
||
notifyAll(event) { | ||
this.observers.forEach(observer => observer.notify(event)); | ||
} | ||
|
||
} | ||
|
||
class AttachmentModel extends Observable { | ||
|
||
constructor(cell) { | ||
super(); | ||
this.ids = {}; | ||
this.cell = cell; | ||
this.typePattern = /data:([^;]*)/; | ||
this.imagePattern = /!\[[^\(]+\]\(attachment:[^)]+\)/g; | ||
this.infoPattern = /\n### You uploaded \d+ attachments.\n\n/g; | ||
this.attachments = {}; | ||
this.load(); | ||
} | ||
|
||
load() { | ||
let that = this; | ||
this.id = 0; | ||
Object.assign(this.attachments, this.cell.attachments); | ||
Object.keys(this.attachments).forEach(function(key) { | ||
that.id += 1; | ||
that.ids[key] = that.id; | ||
}); | ||
} | ||
|
||
save() { | ||
if (this.attachments !== undefined) { | ||
this.cell.attachments = this.attachments; | ||
console.log('Saved attachments.'); | ||
|
||
} else { | ||
console.log('Attachments not defined.'); | ||
} | ||
this.postSaveHook(); | ||
} | ||
|
||
postSaveHook() { | ||
// Invoked after attachments are saved | ||
|
||
} | ||
|
||
hasAttachment(key) { | ||
return key in this.attachments; | ||
} | ||
|
||
getAttachment(key) { | ||
return { | ||
id: this.ids[key], | ||
name: key, | ||
type: Object.keys(this.attachments[key])[0], | ||
data: Object.values(this.attachments[key])[0] | ||
} | ||
} | ||
|
||
setAttachment(key, dataUrl) { | ||
let type = this.typePattern.exec(dataUrl)[1]; | ||
let data = dataUrl.replace('data:' + type + ';base64,', ''); | ||
this.id += 1; | ||
this.attachments[key] = {}; | ||
this.attachments[key][type] = data; | ||
this.ids[key] = this.id; | ||
this.notifyAll({ | ||
type: 'add', | ||
key: key, | ||
id: this.ids[key] | ||
}); | ||
this.save(); | ||
} | ||
|
||
removeAttachment(key) { | ||
let id = this.ids[key]; | ||
delete this.attachments[key]; | ||
delete this.ids[key]; | ||
this.notifyAll({ | ||
type: 'delete', | ||
key: key, | ||
id: id | ||
}); | ||
this.save(); | ||
} | ||
|
||
getAttachments() { | ||
let that = this; | ||
let attachments = []; | ||
Object.keys(this.attachments).forEach(function(key) { | ||
attachments.push(that.getAttachment(key)); | ||
}); | ||
return attachments; | ||
} | ||
} | ||
|
||
class AttachmentCellModel extends AttachmentModel { | ||
|
||
postSaveHook() { | ||
let cleaned = this.cell.get_text().replace(this.imagePattern, ''); | ||
cleaned = cleaned.replace(this.infoPattern, ''); | ||
let n_attachments = Object.keys(this.attachments).length; | ||
cleaned += '\n### You uploaded ' + n_attachments + ' attachments.\n\n' | ||
this.cell.set_text(cleaned); | ||
this.cell.unrender_force(); | ||
this.cell.render(); | ||
} | ||
|
||
getName(name, type) { | ||
let current_name = name + '.' + type; | ||
let counter = 0; | ||
while (current_name in this.attachments) { | ||
counter += 1; | ||
current_name = name + '_' + counter + '.' + type; | ||
} | ||
return current_name; | ||
} | ||
|
||
} | ||
|
||
class DiagramCellModel extends AttachmentModel { | ||
|
||
postSaveHook() { | ||
this.cell.unrender_force(); | ||
this.cell.render(); | ||
} | ||
|
||
} | ||
|
||
return { | ||
AttachmentModel: AttachmentModel, | ||
AttachmentCellModel: AttachmentCellModel, | ||
DiagramCellModel: DiagramCellModel | ||
} | ||
|
||
}); |
Oops, something went wrong.