Created
August 27, 2013 01:35
-
-
Save pkrnjevic/6348746 to your computer and use it in GitHub Desktop.
Sample angular service using CoffeeScript class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ThumbService | |
constructor: (@$log, @$q, @$resource, @mainService) -> | |
console.log "ThumbService: constructor called" | |
@url = @mainService.url(true)+"rest/thumbs/:value/:count" | |
@thumbs_url = @$resource @url | |
@sf = [] | |
@lines = [] | |
@thumbs = [] | |
@thumb_size = 150 | |
@max_thumbs = 200 | |
formatValue: (value) -> | |
if value instanceof Date | |
y = value.getFullYear() | |
m = value.getMonth()+1 | |
m = `m > 9 ? m : "0"+m` | |
d = value.getDate() | |
d = `d > 9 ? d : "0"+d` | |
v = y + '-' + m + '-' + d + " 23:59:59" | |
else | |
v = value | |
get: (value,@max_thumbs,@thumb_size) -> | |
defer = @$q.defer() | |
value = @formatValue(value) | |
@$log.log "should read thumbs from server here..." | |
@thumbs_url.get {value,count:@max_thumbs}, (results) => | |
@thumbs = results.thumbs | |
@$log.log 'thumbService.get success', results | |
# watch(@thumbs,debugger) | |
defer.resolve results.thumbs | |
, (results) -> | |
@$log.error 'thumbService.get error', results | |
defer.reject results | |
defer.promise | |
rescale: (@thumb_size,@line_width) -> | |
console.log "init sf" | |
@sf = [] | |
len = @thumbs.length | |
j = 0 | |
while j < len | |
i = j | |
w = @_width(i++) | |
while i < len | |
ww = w + @_width(i) | |
break if ww > line_width | |
w = ww | |
i++ | |
f = line_width / w | |
# console.log "f,line_width,w:", f, line_width, w | |
@lines.push (i - j) | |
while j < i | |
@sf[j] = f | |
j++ | |
_size: (index) -> | |
thumb = @thumbs[index] | |
# size is the value sent to thumbnailer. | |
# In theory, size == min(width,height). | |
# If Width or Height == 0, return size. | |
# console.log "thumb.Portrait: ", thumb.Portrait | |
[w, h] = if thumb.Portrait | |
[thumb.Height, thumb.Width] | |
else | |
[thumb.Width, thumb.Height] | |
# console.log('w * size / h:',w * size / h,w,size,h) | |
[w,h] = if h>0 && w>0 | |
[w * @thumb_size / h, @thumb_size] | |
else | |
[@thumb_size, @thumb_size] | |
_width: (index) -> | |
[w,h] = @_size(index) | |
w | |
scaledSize: (index) -> | |
[w, h] = @_size(index) | |
sf = @sf[index] | |
[w * sf, h * sf] | |
dropFirstLine: -> | |
n = @lines.shift() | |
for i in [1..n] by 1 | |
@sf.shift() | |
@thumbs.shift() | |
angular.module("app") | |
.service('thumbService', ['$log', '$q', '$resource', 'mainService', ThumbService]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment