GitHub PullRequestReviewCommentEvent at 2014-03-19 00:48:32
Repository: gaia
Actor: davidflanagan
Payload: {"comment":{"url":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/comments/10733221","id":10733221,"diff_hunk":"@@ -0,0 +1,290 @@\n+define(function(require, exports, module) {\n+'use strict';\n+\n+/**\n+ * Dependencies\n+ */\n+\n+var debug = require('debug')('controller:preview-gallery');\n+var bindAll = require('lib/bind-all');\n+\n+/**\n+ * Exports\n+ */\n+\n+exports = module.exports = function(app) {\n+  return new PreviewGalleryController(app);\n+};\n+module.exports.PreviewGalleryController = PreviewGalleryController;\n+\n+function PreviewGalleryController(app) {\n+  debug('initializing');\n+  bindAll(this);\n+  this.app = app;\n+  this.storage = this.app.storage;\n+  this.viewfinder = app.views.viewfinder;\n+  this.previewGallery = app.views.previewGallery;\n+  this.controls = app.views.controls;\n+  this.bindEvents();\n+  this.configure();\n+  debug('initialized');\n+}\n+\n+PreviewGalleryController.prototype.bindEvents = function() {\n+  this.previewGallery.on('click:gallery', this.onGalleryButtonClick);\n+  this.previewGallery.on('click:share', this.shareCurrentItem);\n+  this.previewGallery.on('click:delete', this.deleteCurrentItem);\n+  this.previewGallery.on('click:back', this.closePreview);\n+  this.previewGallery.on('itemChange', this.onItemChange);\n+\n+  this.app.on('preview', this.previewItem);\n+  this.app.on('newmedia', this.onNewMedia);\n+  this.app.on('blur', this.onBlur);\n+\n+  this.storage.on('itemdeleted', this.onItemDeleted);\n+\n+  debug('events bound');\n+};\n+\n+PreviewGalleryController.prototype.configure = function() {\n+  this.currentItemIndex = 0;\n+  this.items = [];\n+};\n+\n+/**\n+ * Open the gallery app when the\n+ * gallery button is pressed.\n+ *\n+ * @private\n+ */\n+PreviewGalleryController.prototype.onGalleryButtonClick = function(event) {\n+  // Can't launch the gallery if the lockscreen is locked.\n+  // The button shouldn't even be visible in this case, but\n+  // let's be really sure here.\n+  if (this.app.inSecureMode) { return; }\n+  \n+  var MozActivity = window.MozActivity;\n+\n+  // Launch the gallery with an activity\n+  this.mozActivity = new MozActivity({\n+    name: 'browse',\n+    data: { type: 'photos' }\n+  });\n+};\n+\n+PreviewGalleryController.prototype.shareCurrentItem = function() {\n+  if (this.app.inSecureMode) { return; }\n+\n+  var index = this.currentItemIndex;\n+  var item = this.items[index];\n+  var type = item.isVideo ? 'video/*' : 'image/*';\n+  var filename = item.filepath.substring(\n+    item.filepath.lastIndexOf('/') + 1);\n+  var activity = new window.MozActivity({\n+    name: 'share',\n+    data: {\n+      type: type,\n+      number: 1,\n+      blobs: [item.blob],\n+      filenames: [filename],\n+      filepaths: [item.filepath] /* temporary hack for bluetooth app */\n+    }\n+  });\n+  activity.onerror = function(e) {\n+    console.warn('Share activity error:', activity.error.name);\n+  };\n+};\n+\n+/**\n+ * Delete the current item \n+ * when the delete button is pressed.\n+ * @private\n+ */\n+PreviewGalleryController.prototype.deleteCurrentItem = function() {\n+  // The button should be gone, but hard exit from this function\n+  // just in case.\n+  if (this.app.inSecureMode) { return; }\n+\n+  var index = this.currentItemIndex;\n+  var item = this.items[index];\n+  var msg, storage, filepath;\n+\n+  if (item.isVideo) {\n+    msg = navigator.mozL10n.get('delete-video?');\n+    storage = this.storage.video;\n+    filepath = item.filepath;\n+  }\n+  else {\n+    msg = navigator.mozL10n.get('delete-photo?');\n+    storage = this.storage.image;\n+    filepath = item.filepath;\n+  }\n+\n+  if (confirm(msg)) {\n+    this.deleteItem(filepath);\n+    // Actually delete the file\n+    storage.delete(filepath).onerror = function(e) {\n+      console.warn('Failed to delete', filepath,\n+                   'from DeviceStorage:', e.target.error);\n+    };\n+\n+    // If this is a video file, delete its poster image as well\n+    if (item.isVideo) {\n+      var poster = filepath.replace('.3gp', '.jpg');\n+      var pictureStorage = this.storage.image;\n+\n+      pictureStorage.delete(poster).onerror = function(e) {\n+        console.warn('Failed to delete poster image', poster,\n+                     'for video', filepath, 'from DeviceStorage:',\n+                     e.target.error);\n+      };\n+    }\n+  }\n+};\n+\n+/**\n+ * Delete the item with corresponding filepath\n+ * and update the thumbnail icon \n+ * on the viewfinder if needed.\n+ *\n+ * @param  {String} filepath\n+ */\n+PreviewGalleryController.prototype.deleteItem = function(filepath) {\n+  var deleteIdx = -1;\n+  var deletedItem = null;\n+  var deletedFileName;\n+  // Check whether filepath is a video poster image or not. If filepath\n+  // contains 'VID' and ends with '.jpg', consider it a video poster\n+  // image and get the video filepath by changing '.jpg' to '.3gp'\n+  if (filepath.indexOf('VID') != -1 &&\n+      filepath.lastIndexOf('.jpg') === filepath.length - 4) {\n+    deletedFileName = filepath.replace('.jpg', '.3gp');\n+  } else {\n+    deletedFileName = filepath;\n+  }\n+\n+  // find the item in items\n+  for (var n = 0; n < this.items.length; n++) {\n+    if (this.items[n].filepath === deletedFileName) {\n+      deletedItem = this.items[n];\n+      deleteIdx = n;\n+      break;\n+    }\n+  }\n+\n+  // Exit when item not found\n+  if (n === this.items.length) { return; }\n+\n+  // Remove the item from the array of items\n+  this.items.splice(deleteIdx, 1);\n+\n+  // If there are no more items, go back to the camera\n+  if (this.items.length === 0) {\n+    this.controls.removeThumbnail();\n+    this.closePreview();\n+  }\n+  else {\n+    if (deleteIdx == this.items.length) {\n+      this.currentItemIndex = this.items.length - 1;\n+    }\n+    else if (deleteIdx === 0) {\n+      // Update thumbnail icon to the previous image \n+      // when delete the latest image\n+      var newItem = this.items[this.currentItemIndex];\n+      this.controls.setThumbnail(newItem.thumbnail);\n+    }\n+\n+    var isPreviewOpened = this.previewGallery.isPreviewOpened();\n+    if (isPreviewOpened) {\n+      this.previewItem();\n+    }\n+  }\n+};\n+\n+PreviewGalleryController.prototype.closePreview = function() {\n+  var camera = this.app.camera;\n+  if(camera.mozCamera) {\n+    camera.resumePreview();\n+  }\n+\n+  this.currentItemIndex = 0;\n+  this.previewGallery.close();\n+  this.app.emit('previewgallery:closed');\n+};\n+\n+/**\n+ * To Do: Image Swipe Transition\n+ */\n+PreviewGalleryController.prototype.onItemChange = function(e) {","path":"apps/camera/js/controllers/preview-gallery.js","position":218,"original_position":218,"commit_id":"15bd71aad1331e7204e7c07db64ff2eae2ae4662","original_commit_id":"15bd71aad1331e7204e7c07db64ff2eae2ae4662","user":{"login":"davidflanagan","id":516914,"avatar_url":"https://avatars.githubusercontent.com/u/516914?","gravatar_id":"a7cc58f54d25826ac0568490237b959d","url":"https://api.github.com/users/davidflanagan","html_url":"https://github.com/davidflanagan","followers_url":"https://api.github.com/users/davidflanagan/followers","following_url":"https://api.github.com/users/davidflanagan/following{/other_user}","gists_url":"https://api.github.com/users/davidflanagan/gists{/gist_id}","starred_url":"https://api.github.com/users/davidflanagan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidflanagan/subscriptions","organizations_url":"https://api.github.com/users/davidflanagan/orgs","repos_url":"https://api.github.com/users/davidflanagan/repos","events_url":"https://api.github.com/users/davidflanagan/events{/privacy}","received_events_url":"https://api.github.com/users/davidflanagan/received_events","type":"User","site_admin":false},"body":"It is strange to me that this is called onItemChange when the argument is a swipe event from the gesture detector.  No item has changed yet.","created_at":"2014-03-19T00:48:32Z","updated_at":"2014-03-19T00:48:32Z","html_url":"https://github.com/mozilla-b2g/gaia/pull/17233#discussion_r10733221","pull_request_url":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/17233","_links":{"self":{"href":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/comments/10733221"},"html":{"href":"https://github.com/mozilla-b2g/gaia/pull/17233#discussion_r10733221"},"pull_request":{"href":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/17233"}}}}