Răsfoiți Sursa

Fix: page edit detection handler

Sotaro KARASAWA 10 ani în urmă
părinte
comite
e8f15a250f
3 a modificat fișierele cu 44 adăugiri și 33 ștergeri
  1. 20 10
      lib/routes/page.js
  2. 15 17
      lib/views/page.html
  3. 9 6
      resource/css/crowi.scss

+ 20 - 10
lib/routes/page.js

@@ -135,7 +135,6 @@ module.exports = function(crowi, app) {
 
 
           return Bookmark.findByUser(userData, {limit: 10, populatePage: true, requestUser: req.user});
           return Bookmark.findByUser(userData, {limit: 10, populatePage: true, requestUser: req.user});
         }).then(function(bookmarkList) {
         }).then(function(bookmarkList) {
-          debug(bookmarkList);
           renderVars.bookmarkList = bookmarkList;
           renderVars.bookmarkList = bookmarkList;
 
 
           return Page.findListByCreator(userData, {limit: 10});
           return Page.findListByCreator(userData, {limit: 10});
@@ -219,7 +218,6 @@ module.exports = function(crowi, app) {
     var currentRevision = pageForm.currentRevision;
     var currentRevision = pageForm.currentRevision;
     var grant = pageForm.grant;
     var grant = pageForm.grant;
     var path = pageForm.path;
     var path = pageForm.path;
-    var extra = pageForm.extra;
 
 
     // TODO: make it pluggable
     // TODO: make it pluggable
     var notify = pageForm.notify || {};
     var notify = pageForm.notify || {};
@@ -227,6 +225,7 @@ module.exports = function(crowi, app) {
     debug('notify: ', notify);
     debug('notify: ', notify);
 
 
     var redirectPath = encodeURI(path);
     var redirectPath = encodeURI(path);
+    var pageData = {};
 
 
     // set to render
     // set to render
     res.locals.pageForm = pageForm;
     res.locals.pageForm = pageForm;
@@ -238,25 +237,32 @@ module.exports = function(crowi, app) {
 
 
     var ignoreNotFound = true;
     var ignoreNotFound = true;
     Page.findPage(path, req.user, null, ignoreNotFound)
     Page.findPage(path, req.user, null, ignoreNotFound)
-    .then(function(pageData) {
+    .then(function(data) {
+      pageData = data;
+
       if (!req.form.isValid) {
       if (!req.form.isValid) {
-        return renderPage(pageData, req, res);
+        debug('Form data not valid');
+        throw new Error('Form data not valid.');
       }
       }
 
 
-      if (pageData && !pageData.isUpdatable(currentRevision)) {
+      if (data && !data.isUpdatable(currentRevision)) {
+        debug('Conflict occured');
         req.form.errors.push('すでに他の人がこのページを編集していたため保存できませんでした。ページを再読み込み後、自分の編集箇所のみ再度編集してください。');
         req.form.errors.push('すでに他の人がこのページを編集していたため保存できませんでした。ページを再読み込み後、自分の編集箇所のみ再度編集してください。');
-        return renderPage(pageData, req, res);
+        throw new Error('Conflict.');
       }
       }
 
 
-      if (pageData) {
+      if (data) {
         // update existing page
         // update existing page
-        var newRevision = Revision.prepareRevision(pageData, body, req.user);
-        return Page.pushRevision(pageData, newRevision, req.user);
+        var newRevision = Revision.prepareRevision(data, body, req.user);
+        return Page.pushRevision(data, newRevision, req.user);
       } else {
       } else {
         // new page
         // new page
         return Page.create(path, body, req.user, {grant: grant});
         return Page.create(path, body, req.user, {grant: grant});
       }
       }
     }).then(function(data) {
     }).then(function(data) {
+      if (!data) {
+        throw new Error('Data not found');
+      }
       crowi.getIo().sockets.emit('page edited', {page: data, user: req.user});
       crowi.getIo().sockets.emit('page edited', {page: data, user: req.user});
 
 
       if (grant != data.grant) {
       if (grant != data.grant) {
@@ -267,7 +273,11 @@ module.exports = function(crowi, app) {
         return res.redirect(redirectPath);
         return res.redirect(redirectPath);
       }
       }
     }).catch(function(err) {
     }).catch(function(err) {
-      debug('Create or edit page error', err);
+      debug('Page create or edit error.', err);
+      if (pageData && !req.form.isValid) {
+        return renderPage(pageData, req, res);
+      }
+
       return res.redirect(redirectPath);
       return res.redirect(redirectPath);
     });
     });
   };
   };

+ 15 - 17
lib/views/page.html

@@ -151,6 +151,21 @@
 
 
   </div>
   </div>
   {% endif %}
   {% endif %}
+
+<div id="notifPageEdited" class="fk-notif fk-notif-danger"><i class="fa fa-exclamation-triangle"></i> <span class="edited-user"></span>さんがこのページを編集しました。 <a href="javascript:location.reload();"><i class="fa fa-angle-double-right"></i> 最新版を読み込む</a></div>
+<script>
+  $(function() {
+    var me = {{ user|json|safe }};
+    var socket = io();
+    socket.on('page edited', function (data) {
+      if (data.user._id != me._id
+        && data.page.path == {{ page.path|json|safe }}) {
+        $('#notifPageEdited').show();
+        $('#notifPageEdited .edited-user').html(data.user.name);
+      }
+    });
+  });
+</script>
 </div>
 </div>
 
 
 {% block content_main_after %}
 {% block content_main_after %}
@@ -184,23 +199,6 @@
 {% endblock %}
 {% endblock %}
 
 
 {% block footer %}
 {% block footer %}
-<div id="notifPageEdited" class="fk-hide fk-notif fk-notif-danger"><i class="fa fa-exclamation-triangle"></i> <span class="edited-user"></span>さんがこのページを編集しました。 <a href="javascript:location.reload();"><i class="fa fa-angle-double-right"></i> 最新版を読み込む</a></div>
-<div id="notifPageEditing" class="fk-hide fk-notif fk-notif-warning"><i class="fa fa-exclamation-triangle"></i> 他の人がこのページの編集を開始しました。</div>
-
-
-<script>
-  $(function() {
-    var me = {{ user|json|safe }};
-    var socket = io();
-    socket.on('page edited', function (data) {
-      if (data.user._id != me._id
-        && data.page.path == {{ page.path|json|safe }}) {
-        $('#notifPageEdited').removeClass('fk-hide').css({bottom: 0});
-        $('#notifPageEdited .edited-user').html(data.user.name);
-      }
-    });
-  });
-</script>
 {% endblock %}
 {% endblock %}
 
 
 {% block body_end %}
 {% block body_end %}

+ 9 - 6
resource/css/crowi.scss

@@ -257,16 +257,15 @@ input.searching {
   }
   }
 }
 }
 
 
-.fk-hide {
-  display: none;
-}
-
 // notification
 // notification
 .fk-notif {
 .fk-notif {
+  display: none;
+
+  bottom: 0;
+  left: 0;
   width: 100%;
   width: 100%;
   position: fixed;
   position: fixed;
-  bottom: -80px;
-  z-index: 8;
+  z-index: 1029;
   padding: 10px;
   padding: 10px;
   box-shadow: -1px 0 3px 0px #666;
   box-shadow: -1px 0 3px 0px #666;
   font-weight: bold;
   font-weight: bold;
@@ -290,6 +289,10 @@ input.searching {
     color: #8a6d3b;
     color: #8a6d3b;
   }
   }
 }
 }
+.on-edit .fk-notif {
+  bottom: 50px;
+  z-index: 1061;
+}
 
 
 // external-services
 // external-services
 .crowi {
 .crowi {