Skip to content

Commit

Permalink
More refactoring for the greater good
Browse files Browse the repository at this point in the history
  • Loading branch information
tekkub committed Aug 26, 2013
1 parent 8014909 commit 02aa36b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 55 deletions.
83 changes: 77 additions & 6 deletions coffee/button.coffee
Expand Up @@ -4,10 +4,24 @@ class Button
@li.attr('id', "bookmark-#{bookmark.id}")
@render()


getListItem: ->
return @li


getLink: ->
return @link


render: ->
link = $('<a>')
link.click (e) ->
if link.data('pinned')
@renderLink()
@renderImageDiv()


renderLink: ->
@link = $('<a>')
@link.click (e) ->
if @link.data('pinned')
if e.which == 1 && !e.metaKey && !e.shiftKey
# We have a normal click, pin this tab
chrome.tabs.getCurrent (tab) ->
Expand All @@ -22,7 +36,64 @@ class Button

return false

@li.append link
@li.append @link


renderImageDiv: ->
@img_div = $('<div>')
.attr('class', 'link-image')
.bind('dragenter', @onDragEnter)
.bind('dragleave', @onDragLeave)
.bind('dragover', @onDragOver)
.bind('drop', @onDrop)

@link.append @img_div


onDragEnter: (event) ->
# Update the drop zone class on drag enter/leave
$(event.target).addClass('dragover')
return false


onDragLeave: (event) ->
$(event.target).removeClass('dragover')
return false


onDragOver: (event) ->
# Allow drops of any kind into the zone.
return false


onDrop: (event) ->
dt = event.originalEvent.dataTransfer

target_img = $(event.target)
target_img.removeClass('dragover')

return true if dt.types[0] != "Files"
if dt.files.length != 1
event.stopPropagation()
return false

file = dt.files[0]

if file.type.indexOf("image") == 0
reader = new FileReader()
reader.onload = (e) ->
imgsrc = e.target.result
target_img.css("background", "url(#{imgsrc})")
.css('background-size', "100%")

key = @bookmark.id
chrome.bookmarks.get key, (bookmark) ->
bookmark_data = getBookmarkData(bookmark[0])
bookmark_data['rawimg'] = imgsrc
saveBookmarkData(key, bookmark_data)

reader.readAsDataURL(file)

event.stopPropagation()
return false

getListItem: ->
return @li
53 changes: 4 additions & 49 deletions coffee/script.coffee
Expand Up @@ -134,55 +134,10 @@ chrome.storage.sync.get null, (data) ->

$.each subtree.children, (i,bookmark) ->
butt = new Button bookmark
li = butt.getListItem
link = li.child('a')

key = bookmark.id
img_div = $('<div>')
.attr('class', 'link-image')
.bind 'drop', (ev) ->
dt = ev.originalEvent.dataTransfer

target_img = $(ev.target)
target_img.removeClass('dragover')

return true if dt.types[0] != "Files"
if dt.files.length != 1
ev.stopPropagation()
return false

file = dt.files[0]

if file.type.indexOf("image") == 0
reader = new FileReader()
reader.onload = (e) ->
imgsrc = e.target.result
target_img.css("background", "url(#{imgsrc})")
.css('background-size', "100%")

chrome.bookmarks.get key, (bookmark) ->
bookmark_data = getBookmarkData(bookmark[0])
bookmark_data['rawimg'] = imgsrc
saveBookmarkData(key, bookmark_data)

reader.readAsDataURL(file)

ev.stopPropagation()
return false

.bind 'dragenter', (ev) ->
# Update the drop zone class on drag enter/leave
$(ev.target).addClass('dragover')
return false

.bind 'dragleave', (ev) ->
$(ev.target).removeClass('dragover')
return false

.bind 'dragover', (ev) ->
# Allow drops of any kind into the zone.
return false
link.append img_div
li = butt.getListItem()
link = butt.getLink()

row.append li

setting_div = $('<div>')
.attr('class', 'settings')
Expand Down

0 comments on commit 02aa36b

Please sign in to comment.