Skip to content

Instantly share code, notes, and snippets.

@faustman
Forked from steida/gist:861916
Created September 13, 2011 09:46
Show Gist options
  • Save faustman/1213496 to your computer and use it in GitHub Desktop.
Save faustman/1213496 to your computer and use it in GitHub Desktop.
###*
Copyright (c) 2011 daniel.steigerwald.cz MIT-style license
###
###*
@fileoverview Bootstrap for the Steida JS Library
todo:
add warning about obsolete browsers
###
goog.provide 'steida'
goog.require 'goog.events'
goog.require 'goog.style'
goog.require 'goog.ui.IdGenerator'
goog.require 'goog.userAgent'
###*
Same as goog.inherits, except is uses __super__ instead of superClass_,
because then super keyword can be used, instead of closure goog.base.
super 'foo', is way better then: goog.base this 'foo'
Coffeescript extends keyword cant be used now, because it produces
helpers functions which closure compiler doesnt like.
Also, Coffeescript static props inheritance is a bad thing.
@param {Function} childCtor Child class.
@param {Function} parentCtor Parent class.
###
steida.inherits = (childCtor, parentCtor) ->
/** @constructor */
`var tempCtor = function() {}`
tempCtor.prototype = parentCtor.prototype
childCtor.__super__ = parentCtor.prototype
childCtor.prototype = new tempCtor
childCtor.prototype.constructor = childCtor
###*
Golden ratio
@const
@type {number}
###
steida.GOLDEN_RATIO = (1 + Math.sqrt(5)) / 2
###
Ajax App boilerplate. Can be used as define (compiler option).
@const
@type {boolean}
###
steida.isAjaxApp = true
###*
Time tester
###
steida.tt = ->
if arguments.callee.start
document.title = (goog.now() - arguments.callee.start) + ''
delete arguments.callee.start
else
arguments.callee.start = goog.now()
return
###*
Enable HTML5 elements for IE
@param {Document=} opt_doc A document for whitch HTML5 should be enabled
###
steida.enableHTML5ForIE = (opt_doc = document) ->
'
abbr article aside audio canvas details figcaption figure
footer header hgroup mark menu meter nav output progress
section summary time video
'
.replace /\w+/g, (n) -> opt_doc.createElement n; return
return
###*
Periodically check total listener count
###
steida.checkLeaks = (count) ->
setInterval (->
document.title = goog.events.getTotalListenerCount() + ''; return)
100
###*
Prevent Clickjacking
http://ajaxian.com/archives/busting-framebusters-clickjacking-is-still-a-big-issue
###
steida.preventClickjacking = ->
top = self
while top != top.top
top = top.top
if top != self
alert 'For security reasons, framing is not allowed; click OK to remove the frames.'
top.location.replace self.location.href
return
###
Monkey patching for Google Closure
http://en.wikipedia.org/wiki/Monkey_patch
###
###*
getNextUniqueId fixed for more google instances at page
###
goog.ui.IdGenerator::getNextUniqueId = ((guid) ->
window[guid] ?= 0
# '.' instead of ':', to prevent clash with closure library
-> '.' + (window[guid]++).toString 36
) '38753850-f23c-11df-98cf-0800200c9a66'
###*
setOpacity fixed for IE
###
goog.style.setOpacity = (el, alpha) ->
style = el.style
if 'opacity' of style
style.opacity = alpha
else if 'MozOpacity' of style
style.MozOpacity = alpha
else if 'filter' of style
style.zoom = 1 if !el.currentStyle || !el.currentStyle.hasLayout
opacity = if alpha == 1 then '' else "alpha(opacity=#{alpha * 100})"
filter = style.filter || (el.currentStyle && el.currentStyle.filter) || ''
reAlpha = /alpha\(opacity=([\d.]+)\)/i
style.filter =
if reAlpha.test(filter)
filter.replace reAlpha, opacity
else
filter + opacity
###*
Run Ajax App boilerplate
###
if steida.isAjaxApp
steida.preventClickjacking()
steida.enableHTML5ForIE()
# IE only (FF not, https://developer.mozilla.org/En/Using_Firefox_1.5_caching)
if goog.userAgent.IE
goog.events.listenOnce window, 'unload', -> goog.events.removeAll(); return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment