Quellcode durchsuchen

feat: add namespace check before saving

Gerald vor 9 Jahren
Ursprung
Commit
d0f536b475

+ 3 - 0
src/_locales/cs/messages.yml

@@ -346,3 +346,6 @@ msgSyncing:
 msgSyncInit:
   description: Message shown when sync service is initializing.
   message: ''
+msgNamespaceConflict:
+  description: Message shown when namespace of the new script conflicts with an existent one.
+  message: ''

+ 3 - 0
src/_locales/de/messages.yml

@@ -346,3 +346,6 @@ msgSyncing:
 msgSyncInit:
   description: Message shown when sync service is initializing.
   message: ''
+msgNamespaceConflict:
+  description: Message shown when namespace of the new script conflicts with an existent one.
+  message: ''

+ 3 - 0
src/_locales/en/messages.yml

@@ -348,3 +348,6 @@ msgSyncing:
 msgSyncInit:
   description: Message shown when sync service is initializing.
   message: Initializing...
+msgNamespaceConflict:
+  description: Message shown when namespace of the new script conflicts with an existent one.
+  message: 'Script namespace conflicts! Please modify @name and @namespace.'

+ 3 - 0
src/_locales/pl/messages.yml

@@ -346,3 +346,6 @@ msgSyncing:
 msgSyncInit:
   description: Message shown when sync service is initializing.
   message: ''
+msgNamespaceConflict:
+  description: Message shown when namespace of the new script conflicts with an existent one.
+  message: ''

+ 3 - 0
src/_locales/ru/messages.yml

@@ -348,3 +348,6 @@ msgSyncing:
 msgSyncInit:
   description: Message shown when sync service is initializing.
   message: ''
+msgNamespaceConflict:
+  description: Message shown when namespace of the new script conflicts with an existent one.
+  message: ''

+ 3 - 0
src/_locales/zh/messages.yml

@@ -348,3 +348,6 @@ msgSyncing:
 msgSyncInit:
   description: Message shown when sync service is initializing.
   message: 正在初始化...
+msgNamespaceConflict:
+  description: Message shown when namespace of the new script conflicts with an existent one.
+  message: 脚本命名空间冲突,请修改@name和@namespace!

+ 17 - 14
src/background/db.js

@@ -63,20 +63,16 @@ VMDB.prototype.getScript = function (id, tx) {
 
 VMDB.prototype.queryScript = function (id, meta, tx) {
   var _this = this;
-  return (
-    id ? _this.getScript(id, tx)
+  return id
+    ? _this.getScript(id, tx)
     : new Promise(function (resolve, reject) {
       var uri = scriptUtils.getNameURI({meta: meta});
-      if (uri !== '::')
-        (tx || _this.db.transaction('scripts')).objectStore('scripts')
-        .index('uri').get(uri).onsuccess = function (e) {
-          resolve(e.target.result);
-        };
-      else resolve();
-    })
-  ).then(function (script) {
-    return script || scriptUtils.newScript();
-  });
+      tx = tx || _this.db.transaction('scripts');
+      tx.objectStore('scripts')
+      .index('uri').get(uri).onsuccess = function (e) {
+        resolve(e.target.result);
+      };
+    });
 };
 
 VMDB.prototype.getScriptData = function (id) {
@@ -296,10 +292,14 @@ VMDB.prototype.saveScript = function (script, tx) {
   tx = tx || this.db.transaction('scripts', 'readwrite');
   var o = tx.objectStore('scripts');
   return new Promise(function (resolve, reject) {
-    o.put(script).onsuccess = function (e) {
+    var res = o.put(script);
+    res.onsuccess = function (e) {
       script.id = e.target.result;
       resolve(script);
     };
+    res.onerror = function () {
+      reject(_.i18n('msgNamespaceConflict'));
+    };
   });
 };
 
@@ -539,7 +539,10 @@ VMDB.prototype.parseScript = function (data) {
       });
     });
   return _this.queryScript(data.id, meta, tx).then(function (script) {
-    if (!script.id) {
+    if (script) {
+      if (!data.id) throw _.i18n('msgNamespaceConflict');
+    } else {
+      script = scriptUtils.newScript();
       res.cmd = 'add';
       res.data.message = _.i18n('msgInstalled');
     }

+ 4 - 0
src/options/app.js

@@ -1,5 +1,9 @@
 zip.workerScriptsPath = '/lib/zip.js/';
 
+_.showMessage = function (options) {
+  new MessageView(options);
+};
+
 var App = Backbone.Router.extend({
   routes: {
     '': 'renderMain',

+ 18 - 0
src/options/style.css

@@ -335,3 +335,21 @@ fieldset.title {
   margin-left: 2px;
   background: red;
 }
+.message {
+  position: absolute;
+  width: 200px;
+  top: 0;
+  left: 50%;
+  margin-left: -100px;
+  padding: 16px;
+  z-index: 10;
+  transform: translateY(-120%);
+  transition: transform .5s;
+  background: white;
+  border-bottom-left-radius: 4px;
+  border-bottom-right-radius: 4px;
+  box-shadow: 0 0 5px rgba(0,0,0,.2);
+}
+.message-show {
+  transform: translateY(0);
+}

+ 1 - 0
src/options/templates/message.html

@@ -0,0 +1 @@
+<div><%= it.data %></div>

+ 5 - 1
src/options/views/edit.js

@@ -69,6 +69,10 @@ var EditView = BaseView.extend({
       }
     }).then(function () {
       _this.updateStatus(false);
+    }, function (err) {
+      _.showMessage({
+        data: err,
+      });
     });
   },
   close: function () {
@@ -76,7 +80,7 @@ var EditView = BaseView.extend({
       scriptList.trigger('edit:close');
   },
   saveClose: function () {
-    this.save().then(this.close.bind(this));
+    this.save().then(this.close);
   },
   hideMeta: function () {
     if (!this.metaView) return;

+ 35 - 0
src/options/views/message.js

@@ -0,0 +1,35 @@
+var MessageView = BaseView.extend({
+  className: 'message',
+  templateUrl: '/options/templates/message.html',
+  transitionTime: 500,
+  initialize: function (options) {
+    var _this = this;
+    _this.options = options;
+    BaseView.prototype.initialize.call(_this);
+    _.bindAll(_this, 'toggle', 'delay', 'remove');
+  },
+  _render: function () {
+    var _this = this;
+    _this.$el
+    .html(_this.templateFn(_this.options))
+    .appendTo(document.body);
+    _this.delay(16)
+    .then(_this.toggle)
+    .then(function () {
+      return _this.delay(_this.options.delay || 2000);
+    })
+    .then(_this.toggle)
+    .then(_this.remove);
+  },
+  delay: function (time) {
+    if (time == null) time = this.transitionTime;
+    return new Promise(function (resolve, reject) {
+      setTimeout(resolve, time);
+    });
+  },
+  toggle: function () {
+    var _this = this;
+    _this.$el.toggleClass('message-show');
+    return _this.delay();
+  },
+});