## 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.from_now = -> new Date() Date.now_utc = -> Math.round( (new Date()).getTime() / 1000.0) Date.prototype.to_utc = -> Math.round(this.getTime() / 1000.0) Date.prototype.remove_time = -> this.setHours(0) this.setMinutes(0) this.setSeconds(0) this.setMilliseconds(0) return this 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 String.prototype.rjust = (length, char)-> ret = this while (ret.length < length) ret = char + ret 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, opts) -> opts ||= {} css_opts = opts.style || {} cls = opts.className || '' id = vm.name template = tmp #options['z-index'] = Overlay.instance.zindex + 10 $('body').append("
") $('#overlay-' + id).css(css_opts) $('#overlay-' + id).addClass(cls) $('#overlay-' + id).css({'margin-left' : -1 * $('#overlay-' + id).width() / 2}) #$('#backdrop-' + id).click => #console.log('backdrop clicked.') #Overlay.remove(id) setTimeout -> $('#overlay-' + id).koBind(vm) if opts.stretch == true $("#overlay-#{id} .modal-body").css({'max-height' : ($(window).height() - 200)}) $('#overlay-' + id).css({'margin-top' : ($(window).height() - 100)/ -2}) $('#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').fadeIn '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() $('#backdrop-' + id).remove() $('#overlay-' + id).remove() if (id == 'confirm') Overlay.removePopovers = -> $('.popover').remove() Overlay.isVisible = (id) -> $('#overlay-' + id).length > 0 Overlay.popover = (el, vm, tmp, opts)-> id = vm.name opts.placement = opts.placement || 'bottom' $po = $("