## EXTENSIONS Array.prototype.indexAt = (val) -> for i in [0...this.length] if this[i] == val return i return -1 Array.prototype.includes = (val) -> return this.indexAt(val) != -1 Array.prototype.itemAt = (val)-> return this.slice(val)[0] Array.prototype.pushOnce = (item) -> if (!this.includes(item)) this.push(item) Array.prototype.remove = (item) -> idx = this.indexOf(item) this.splice(idx, 1) if idx > -1 Date.from_utc = (utc) -> new Date(utc * 1000) Date.prototype.to_utc = -> Math.round(this.getTime() / 1000.0) String.prototype.endsWith = (suffix) -> this.indexOf(suffix, this.length - suffix.length) != -1 String.prototype.includes = (str) -> this.indexOf(str) != -1 String.prototype.truncate = (val)-> ret = this.substring(0, val) ret = ret + "..." if this.length > val return ret History.getRelativeUrl = -> url = History.getState().url "/#{url.replace(History.getRootUrl(), '')}" ## SELECTOPTS class @SelectOpts constructor : -> @options = [] add : (val, str)=> @options.push {val : val.toString(), str : str} return this find : (val)=> for obj in @options return obj.str if (obj.val == val.toString()) return "" ## PAGETIMER class @PageTimer constructor: (func, time) -> @callback = func @frequency = time * 1000 @t_id = -1 start : => @t_id = setInterval(@callback, @frequency) stop : => clearInterval(@t_id) @t_id = -1 isRunning : => @t_id != -1 setFrequency : (time) => @stop() @frequency = time * 1000 @start() getFrequency : => return @frequency / 1000 increasePollTime : => @setFrequency( @getFrequency() + (if @getFrequency() % 5 == 0 then 9 else 1) ) ## NOTIFIER class @Notifier constructor: -> @popup = null @tid = null @nids = [] hasSupport : -> if (window.webkitNotifications) return true else return false hasPermission : -> return (window.webkitNotifications.checkPermission() == 0) requestPermission : (cb) -> window.webkitNotifications.requestPermission -> cb(window.webkitNotifications.checkPermission() == 0) if (cb) notify : (icon, title, body, opts) -> if (@hasSupport() && @hasPermission() && !@isActive()) opts = {} if !opts? stay = opts["stay"] delay = opts["delay"] nid = opts["nid"] if (nid?) if (@nids.includes(nid)) return false else @nids.pushOnce(nid) @popup = window.webkitNotifications.createNotification(icon, title, body) if (!stay? || !stay) @popup.ondisplay = -> setTimeout('notifier.Hide()', 5000) if (delay?) @tid = setTimeout('notifier.popup.show()', delay * 1000) else @popup.show() return true return false hide : -> if (@popup != null) @popup.cancel() @popup = null if (@tid != null) clearTimeout(@tid) @tid = null isActive : -> if (@popup != null) return true else return false ## OVERLAY class @Overlay constructor : -> @zindex = 100 @notifyTimer = null $(document).click -> Overlay.removePopovers() Overlay.instance = new Overlay() Overlay.closeDialog = -> @remove('dialog') Overlay.add = (vm, tmp, options, cls) -> id = vm.name template = tmp cls = cls || '' options = {} if !options? #options['z-index'] = Overlay.instance.zindex + 10 $('body').append("
") #$('#overlay-' + id).css(options) #$('#overlay-' + id).addClass(cls) #$('#overlay-' + id).css({'margin-left' : -1 * $('#overlay-' + id).width() / 2}) #$('.overlay .content').css({'max-height' : ($(window).height() - 100)}) #$('#backdrop-' + id).click => #console.log('backdrop clicked.') #Overlay.remove(id) setTimeout -> $('#overlay-' + id).koBind(vm) $('#overlay-' + id).modal('show') $('#overlay-' + id).on 'hidden', -> $('#overlay-' + id).koClean() $('#overlay-' + id).remove() , 100 #Overlay.instance.zindex = Overlay.instance.zindex + 10 Overlay.dialog = (msg, opts) -> vm = name : 'dialog' message : ko.observable(msg) yes : opts.yes no : opts.no cancel : Overlay.remove('dialog') Overlay.add(vm, 'view-dialog', { width : 300 }) Overlay.notify = (msg, cls, tm) -> cls = cls || '' tm = tm || 3000 Overlay.clearNotifications() $('body').prepend(" ") if (cls) $('#notify').addClass(cls) $('#notify').slideDown 'slow', -> Overlay.instance.notifyTimeout = setTimeout -> $('#notify').fadeOut('slow') , tm Overlay.clearNotifications = -> clearTimeout(Overlay.instance.notifyTimeout) $('#notify').remove() Overlay.confirm = (msg, opts) -> vm = yes : -> opts.yes() if opts.yes? Overlay.remove('confirm') no : -> opts.no() if opts.no? Overlay.remove('confirm') $('body').prepend(" ") $('#overlay-confirm').koBind(vm) $('#overlay-confirm').slideDown 'fast' Overlay.remove = (id) -> $('#overlay-' + id).modal('hide') $('#popover-' + id).koClean().remove() Overlay.removePopovers = -> $('.popover').remove() Overlay.popover = (el, tmp, vm, opts)-> id = vm.name opts.placement = opts.placement || 'bottom' $po = $("