Move image-view to a package

This removes the ugliness of having project.coffee require
image-edit-session at the end since the image-edit-session can
now register itself in an @activate callback and as a deferred
deserializer in the package.cson file.
This commit is contained in:
Kevin Sawicki
2013-06-07 10:15:32 -07:00
parent 3029137cb8
commit fca1c13d03
8 changed files with 24 additions and 28 deletions

View File

@@ -1,65 +0,0 @@
fsUtils = require 'fs-utils'
_ = require 'underscore'
# Public: Manages the states between {Editor}s, images, and the project as a whole.
#
# Essentially, the graphical version of a {EditSession}.
module.exports=
class ImageEditSession
registerDeserializer(this)
# Files with these extensions will be opened as images
@imageExtensions: ['.gif', '.jpeg', '.jpg', '.png']
### Internal ###
Project = require 'project'
Project.registerOpener (path) =>
new ImageEditSession(path) if _.include(@imageExtensions, fsUtils.extension(path))
@deserialize: (state) ->
if fsUtils.exists(state.path)
project.open(state.path)
else
console.warn "Could not build edit session for path '#{state.path}' because that file no longer exists"
constructor: (@path) ->
serialize: ->
deserializer: 'ImageEditSession'
path: @path
getViewClass: ->
require 'image-view'
### Public ###
# Retrieves the filename of the open file.
#
# This is `'untitled'` if the file is new and not saved to the disk.
#
# Returns a {String}.
getTitle: ->
if path = @getPath()
fsUtils.base(path)
else
'untitled'
# Retrieves the URI of the current image.
#
# Returns a {String}.
getUri: -> @path
# Retrieves the path of the current image.
#
# Returns a {String}.
getPath: -> @path
# Compares two `ImageEditSession`s to determine equality.
#
# Equality is based on the condition that the two URIs are the same.
#
# Returns a {Boolean}.
isEqual: (other) ->
other instanceof ImageEditSession and @getUri() is other.getUri()

View File

@@ -1,103 +0,0 @@
ScrollView = require 'scroll-view'
_ = require 'underscore'
$ = require 'jquery'
# Public: Renders images in the {Editor}.
module.exports =
class ImageView extends ScrollView
### Internal ###
@content: ->
@div class: 'image-view', tabindex: -1, =>
@div class: 'image-container', =>
@img outlet: 'image'
initialize: (imageEditSession) ->
super
requireStylesheet 'image-view'
@image.load =>
@originalHeight = @image.height()
@originalWidth = @image.width()
@loaded = true
@centerImage()
@setPath(imageEditSession?.getPath())
@subscribe $(window), 'resize', _.debounce((=> @centerImage()), 300)
@command 'image-view:zoom-in', => @zoomIn()
@command 'image-view:zoom-out', => @zoomOut()
@command 'image-view:reset-zoom', => @resetZoom()
afterAttach: (onDom) ->
return unless onDom
if pane = @getPane()
@active = @is(pane.activeView)
@subscribe pane, 'pane:active-item-changed', (event, item) =>
wasActive = @active
@active = @is(pane.activeView)
@centerImage() if @active and not wasActive
### Public ###
# Places the image in the center of the {Editor}.
centerImage: ->
return unless @loaded and @isVisible()
@image.css
'top': Math.max((@height() - @image.outerHeight()) / 2, 0)
'left': Math.max((@width() - @image.outerWidth()) / 2, 0)
@image.show()
# Indicates the path of the image.
#
# path - A {String} for the new image path.
setPath: (path) ->
if path?
if @image.attr('src') isnt path
@loaded = false
@image.hide().attr('src', path)
else
@image.hide()
# Retrieve's the {Editor}'s pane.
#
# Returns a {Pane}.
getPane: ->
@parent('.item-views').parent('.pane').view()
# Zooms the image out.
#
# This is done by a factor of `0.9`.
zoomOut: ->
@adjustSize(0.9)
# Zooms the image in.
#
# This is done by a factor of `1.1`.
zoomIn: ->
@adjustSize(1.1)
# Zooms the image to its normal width and height.
resetZoom: ->
return unless @loaded and @isVisible()
@image.width(@originalWidth)
@image.height(@originalHeight)
@centerImage()
### Internal ###
adjustSize: (factor) ->
return unless @loaded and @isVisible()
newWidth = @image.width() * factor
newHeight = @image.height() * factor
@image.width(newWidth)
@image.height(newHeight)
@centerImage()
setModel: (imageEditSession) ->
@setPath(imageEditSession?.getPath())

View File

@@ -36,10 +36,3 @@
'.editor !important, .editor.mini !important':
'escape': 'editor:consolidate-selections'
'.image-view':
'meta-+': 'image-view:zoom-in'
'meta-=': 'image-view:zoom-in'
'meta--': 'image-view:zoom-out'
'meta-_': 'image-view:zoom-out'
'meta-0': 'image-view:reset-zoom'

View File

@@ -317,5 +317,3 @@ class Project
@on 'buffer-created', (buffer) -> callback(buffer)
_.extend Project.prototype, EventEmitter
require 'image-edit-session'