rename study chapters
parent
191cc33a17
commit
c40d51ca4d
|
@ -12,6 +12,9 @@ final class ChapterRepo(coll: Coll) {
|
|||
|
||||
def byId(id: Chapter.ID): Fu[Option[Chapter]] = coll.byId[Chapter](id)
|
||||
|
||||
def byIdAndStudy(id: Chapter.ID, studyId: Study.ID): Fu[Option[Chapter]] =
|
||||
coll.byId[Chapter](id).map { _.filter(_.studyId == studyId) }
|
||||
|
||||
def orderedMetadataByStudy(studyId: Study.ID): Fu[List[Chapter.Metadata]] =
|
||||
coll.find(
|
||||
$doc("studyId" -> studyId),
|
||||
|
|
|
@ -132,6 +132,13 @@ private[study] final class SocketHandler(
|
|||
chapterId <- o str "d"
|
||||
} api.setChapter(byUserId, studyId, chapterId)
|
||||
|
||||
case ("renameChapter", o) if owner => for {
|
||||
byUserId <- member.userId
|
||||
d <- o obj "d"
|
||||
id <- d str "id"
|
||||
name <- d str "name"
|
||||
} api.renameChapter(byUserId, studyId, id, name)
|
||||
|
||||
}
|
||||
|
||||
private def reading[A](o: JsValue)(f: A => Unit)(implicit reader: Reads[A]): Unit =
|
||||
|
|
|
@ -137,14 +137,22 @@ final class StudyApi(
|
|||
|
||||
def setChapter(byUserId: User.ID, studyId: Study.ID, chapterId: Chapter.ID) = sequenceStudy(studyId) { study =>
|
||||
(study.position.chapterId != chapterId) ?? {
|
||||
chapterRepo.byId(chapterId) flatMap {
|
||||
_.filter(_.studyId == study.id) ?? { chapter =>
|
||||
chapterRepo.byIdAndStudy(chapterId, studyId) flatMap {
|
||||
_ ?? { chapter =>
|
||||
studyRepo.update(study withChapter chapter) >>- reloadAll(study)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def renameChapter(byUserId: User.ID, studyId: Study.ID, chapterId: Chapter.ID, name: String) = sequenceStudy(studyId) { study =>
|
||||
chapterRepo.byIdAndStudy(chapterId, studyId) flatMap {
|
||||
_ ?? { chapter =>
|
||||
chapterRepo.update(chapter.copy(name = name)) >>- reloadChapters(study)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private def reloadUid(study: Study, uid: Uid) =
|
||||
sendTo(study.id, Socket.ReloadUid(uid))
|
||||
|
||||
|
|
|
@ -128,3 +128,6 @@
|
|||
color: #3893E8;
|
||||
opacity: 1;
|
||||
}
|
||||
.study_box .chapters input {
|
||||
width: 100%;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
var m = require('mithril');
|
||||
var classSet = require('chessground').util.classSet;
|
||||
var partial = require('chessground').util.partial;
|
||||
|
||||
function onEnter(action) {
|
||||
return function(el, isUpdate) {
|
||||
if (!isUpdate) $(el).keypress(function(e) {
|
||||
if (e.which == 10 || e.which == 13) action($(this).val());
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ctrl: function(chapters, send) {
|
||||
|
@ -20,6 +29,13 @@ module.exports = {
|
|||
send("addChapter", {
|
||||
name: name
|
||||
});
|
||||
},
|
||||
rename: function(id, name) {
|
||||
send("renameChapter", {
|
||||
id: id,
|
||||
name: name
|
||||
});
|
||||
vm.confing = null;
|
||||
}
|
||||
};
|
||||
},
|
||||
|
@ -31,6 +47,7 @@ module.exports = {
|
|||
if (ownage) return m('span.action.config', {
|
||||
onclick: function(e) {
|
||||
ctrl.chapters.vm.confing = confing ? null : chapter.id;
|
||||
e.stopPropagation();
|
||||
}
|
||||
}, m('i', {
|
||||
'data-icon': '%'
|
||||
|
@ -39,7 +56,10 @@ module.exports = {
|
|||
|
||||
var chapterConfig = function(chapter) {
|
||||
return m('div.config', [
|
||||
"config"
|
||||
m('input', {
|
||||
value: chapter.name,
|
||||
config: onEnter(partial(ctrl.chapters.rename, chapter.id))
|
||||
})
|
||||
]);
|
||||
};
|
||||
|
||||
|
@ -47,12 +67,7 @@ module.exports = {
|
|||
return m('div.create', [
|
||||
m('input', {
|
||||
class: 'list_input',
|
||||
config: function(el, isUpdate) {
|
||||
if (isUpdate) return;
|
||||
$(el).keypress(function(e) {
|
||||
if (e.which == 10 || e.which == 13) ctrl.chapters.add($(this).val());
|
||||
})
|
||||
},
|
||||
config: onEnter(ctrl.chapters.add),
|
||||
placeholder: 'Add a new chapter'
|
||||
})
|
||||
]);
|
||||
|
|
Loading…
Reference in New Issue