Browse Source

Add events for script update

Gerald 10 years ago
parent
commit
92ee20f443
4 changed files with 66 additions and 45 deletions
  1. 28 32
      src/background.js
  2. 10 3
      src/options/model.js
  3. 6 2
      src/options/templates/script.html
  4. 22 8
      src/options/view.js

+ 28 - 32
src/background.js

@@ -483,20 +483,9 @@ function fetchRequire(url) {
 	});
 }
 
-/**
- * broadcast script change
- * {
- *   id: required if script is not given
- *   script: optional
- *   message: optional
- *   code: optional
- *     0 for updated, 1 for installed,
- *     others for message: 3 for updating
- * }
- */
-function updateItem(res) {
+function updateItem(data) {
 	if (port) try {
-		port.postMessage(res);
+		port.postMessage(data);
 	} catch(e) {
 		port = null;
 		console.log(e);
@@ -543,21 +532,23 @@ function parseScript(data, src, callback) {
 		if (callback) callback(ret);
 	}
 	var ret = {
-		code: 0,
-		message: 'message' in data ? data.message : _.i18n('msgUpdated'),
+		cmd: 'update',
+		data: {
+			message: 'message' in data ? data.message : _.i18n('msgUpdated'),
+		},
 	};
 	if (data.status && data.status != 200 || data.code == '') {
 		// net error
-		ret.code = -1;
-		ret.message = _.i18n('msgErrorFetchingScript');
+		ret.cmd = 'error';
+		ret.data.message = _.i18n('msgErrorFetchingScript');
 		finish();
 	} else {
 		// store script
 		var meta = parseMeta(data.code);
 		queryScript(data.id, meta, function(script) {
 			if (!script.id) {
-				ret.code=1;
-				ret.message=_.i18n('msgInstalled');
+				ret.cmd = 'add';
+				ret.data.message = _.i18n('msgInstalled');
 			}
 			// add additional data for import and user edit
 			if (data.more)
@@ -572,8 +563,8 @@ function parseScript(data, src, callback) {
 			if (data.url && !/^(file|data):/.test(data.url))
 				script.custom.lastInstallURL = data.url;
 			saveScript(script).onsuccess = function(e) {
-				ret.id = script.id = e.target.result;
-				ret.script = getMeta(script);
+				script.id = e.target.result;
+				_.assign(ret.data, getMeta(script));
 				finish();
 				if (!meta.grant.length && !_.options.get('ignoreGrant'))
 					notify({
@@ -632,9 +623,8 @@ function updateMeta(meta, src, callback) {
 			if (i in script) script[i] = meta[i];
 		o.put(script).onsuccess = function (e) {
 			updateItem({
-				code: 0,
-				id: script.id,
-				script: getMeta(script),
+				cmd: 'update',
+				data: getMeta(script),
 			});
 		};
 	};
@@ -645,7 +635,7 @@ var _update = {};
 function realCheckUpdate(script) {
   function update() {
     if(downloadURL) {
-      ret.message = _.i18n('msgUpdating');
+      ret.data.message = _.i18n('msgUpdating');
       fetchURL(downloadURL, function(){
         parseScript({
 					id: script.id,
@@ -653,7 +643,7 @@ function realCheckUpdate(script) {
           code: this.responseText,
         });
       });
-    } else ret.message = '<span class=new>' + _.i18n('msgNewVersion') + '</span>';
+    } else ret.data.message = '<span class=new>' + _.i18n('msgNewVersion') + '</span>';
     updateItem(ret);
 		finish();
   }
@@ -662,7 +652,13 @@ function realCheckUpdate(script) {
 	}
 	if (_update[script.id]) return;
 	_update[script.id] = 1;
-  var ret = {id: script.id, code: 3};
+  var ret = {
+		cmd: 'update',
+		data: {
+			id: script.id,
+			updating: true,
+		},
+	};
 	var downloadURL =
 		script.custom.downloadURL ||
 		script.meta.downloadURL ||
@@ -672,18 +668,18 @@ function realCheckUpdate(script) {
 		script.meta.updateURL ||
 		downloadURL;
   if(updateURL) {
-    ret.message = _.i18n('msgCheckingForUpdate');
+    ret.data.message = _.i18n('msgCheckingForUpdate');
 		updateItem(ret);
     fetchURL(updateURL, function() {
-      ret.message = _.i18n('msgErrorFetchingUpdateInfo');
+      ret.data.message = _.i18n('msgErrorFetchingUpdateInfo');
       if (this.status == 200)
 				try {
 					var meta = parseMeta(this.responseText);
 					if(compareVersion(script.meta.version, meta.version) < 0)
 						return update();
-					ret.message = _.i18n('msgNoUpdate');
+					ret.data.message = _.i18n('msgNoUpdate');
 				} catch(e) {}
-			ret.code = 2;
+			ret.data.updating = false;
       updateItem(ret);
 			finish();
     }, null, {
@@ -703,7 +699,7 @@ function checkUpdate(id, src, callback) {
 }
 
 function checkUpdateAll(e, src, callback) {
-	setOption('lastUpdate', Date.now());
+	_.options.set('lastUpdate', Date.now());
 	var o = db.transaction('scripts').objectStore('scripts');
 	o.index('update').openCursor(1).onsuccess = function (e) {
 		var r = e.target.result;

+ 10 - 3
src/options/model.js

@@ -1,7 +1,4 @@
 var Meta = Backbone.Model.extend({
-  initialize: function () {
-
-  },
   parse: function (script) {
     this.meta = script.meta;
     return script.custom;
@@ -52,3 +49,13 @@ var ScriptList = Backbone.Collection.extend({
 });
 
 var scriptList = new ScriptList();
+
+var port = chrome.runtime.connect({name: 'Options'});
+port.onMessage.addListener(function (res) {
+  if (res.cmd === 'add')
+    scriptList.push(res.data);
+  else if (res.data) {
+    var model = scriptList.get(res.data.id);
+    if (model) model.set(res.data);
+  }
+});

+ 6 - 2
src/options/templates/script.html

@@ -1,4 +1,4 @@
-<img class=script-icon src="/images/icon48.png" data-src="<%- it.meta.icon %>">
+<img class=script-icon src="<%- it._icon %>">
 <div class="script-version pull-right">
   <%- it.meta.version ? 'v' + it.meta.version : '' %>
 </div>
@@ -24,6 +24,10 @@
     it.enabled ? 'buttonDisable' : 'buttonEnable'
   %>"></button>
   <button data-id=remove data-i18n="buttonRemove"></button>
-  <%= it.canUpdate ? '<button data-id=update data-i18n="buttonUpdate"></button>' : '' %>
+  <%= it.canUpdate ?
+  '<button data-id=update data-i18n="buttonUpdate"' + (
+    it.updating ? ' disabled' : ''
+  ) + '></button>'
+  : '' %>
   <span data-id=message><%= it.message %></span>
 </div>

+ 22 - 8
src/options/view.js

@@ -44,6 +44,7 @@ var BaseView = Backbone.View.extend({
   },
 });
 
+var DEFAULT_ICON = '/images/icon48.png';
 var ScriptView = BaseView.extend({
   className: 'script',
   templateUrl: 'templates/script.html',
@@ -54,9 +55,21 @@ var ScriptView = BaseView.extend({
     'click [data-id=update]': 'onUpdate',
   },
   initialize: function () {
-    BaseView.prototype.initialize.call(this);
-    this.listenTo(this.model, 'change', this.render);
-    this.listenTo(this.model, 'remove', this.onRemoved);
+    var _this = this;
+    _this.model.set('_icon', DEFAULT_ICON);
+    BaseView.prototype.initialize.call(_this);
+    _this.listenTo(_this.model, 'change', _this.render);
+    _this.listenTo(_this.model, 'remove', _this.onRemoved);
+  },
+  loadIcon: function () {
+    var _this = this;
+    var icon = _this.model.get('meta').icon;
+    if (icon && icon !== _this.model.get('_icon'))
+      _this.loadImage(icon).then(function () {
+        _this.model.set('_icon', icon);
+      }, function () {
+        _this.model.set('_icon', DEFAULT_ICON);
+      });
   },
   render: function () {
     var _this = this;
@@ -68,11 +81,8 @@ var ScriptView = BaseView.extend({
     it.author = _this.getAuthor(it.meta.author);
     _this.$el.html(_this.templateFn(it));
     if (!it.enabled) _this.$el.addClass('disabled');
-    _this.$('img[data-src]').each(function (i, img) {
-      if (img.dataset.src) _this.loadImage(img.dataset.src).then(function () {
-        img.src = img.dataset.src;
-      });
-    });
+    else _this.$el.removeClass('disabled');
+    _this.loadIcon();
     return _this;
   },
   getAuthor: function (text) {
@@ -138,6 +148,7 @@ var MainTab = BaseView.extend({
   templateUrl: 'templates/tab-installed.html',
   events: {
     'click #bNew': 'newScript',
+    'click #bUpdate': 'updateAll',
   },
   initialize: function () {
     var _this = this;
@@ -191,6 +202,9 @@ var MainTab = BaseView.extend({
       scriptList.trigger('edit:open', new Script(script));
     });
   },
+  updateAll: function () {
+    _.sendMessage({cmd: 'CheckUpdateAll'});
+  },
 });
 
 var ExportList = BaseView.extend({