Created
October 14, 2011 11:07
-
-
Save aerith/1286823 to your computer and use it in GitHub Desktop.
This file contains 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
(function (ns, w, d, $) { | |
var cube = ns.cube = {}; | |
cube.support = $.extend({ | |
json : !!(w.JSON && w.JSON.stringify), | |
logger: !!(w.console && w.console.log) | |
}, $.support); | |
cube.browser = $.extend({ | |
msie7: $.browser.msie && $.browser.version < 8, | |
msie8: $.browser.msie && $.browser.version < 9, | |
chrome: /chrome/.test(w.navigator.userAgent.toLowerCase()) | |
}, $.browser); | |
cube.browser.webkit = cube.browser.safari && cube.browser.chrome; | |
cube.ui = {}; | |
cube.ui.style = {}; | |
cube.ui.style.SourceSwitcher = function (options) { | |
options = options || {}; | |
this.config = $.extend({ | |
title: 'cube-ui-style-source-switcher', | |
parentSelector: '#cube-ui-style-source-switcher', | |
childSelector: '> a', | |
sourcePath: '/css', | |
cookieName: 'cube-ui-style-source-switcher', | |
cookieDomain: null, | |
cookiePath: null, | |
cookieExpires: 30, | |
cookieSecure: null | |
}, options || {}); | |
if ('apply' in this.initialize) { | |
this.initialize.apply(this, [options]); | |
} | |
}; | |
cube.ui.style.SourceSwitcher.prototype = { | |
initialize: function () { | |
var self = this; | |
var parent = $(this.config.parentSelector); | |
var child_selector = this.config.childSelector || 'a'; // ← かわいい | |
var style_source = this.getFromCookie(); | |
if (style_source) { | |
this.getStyleElement().setAttribute('href', style_source); | |
} | |
parent | |
.find(child_selector) | |
.click(function (event) { return self.sourceSwitchHandler(this, event) }) | |
.end(); | |
}, | |
getFromCookie: function () { | |
return $.cookie(this.config.cookieName, { | |
expires: this.config.cookieExpires, | |
domain: this.config.cookieDomain, | |
path: this.config.cookiePath, | |
secure: this.config.cookieSecure | |
}); | |
}, | |
setToCookie: function (value) { | |
return $.cookie(this.config.cookieName, value, { | |
expires: this.config.cookieExpires, | |
domain: this.config.cookieDomain, | |
path: this.config.cookiePath, | |
secure: this.config.cookieSecure | |
}); | |
}, | |
getStyleElement: function () { | |
var self = this; | |
return $('link[rel="stylesheet"]') | |
.filter(function () { return this.title === self.config.title }).get(0); | |
}, | |
sourceSwitchHandler: function (element, event) { | |
event.stopPropagation(); | |
event.preventDefault(); | |
var style_trigger = $(element); | |
var style_source = style_trigger.attr('id') ? this.config.sourcePath + '/' + style_trigger.attr('href') : style_trigger.attr('href'); | |
this.setToCookie(style_source); | |
this.getStyleElement().setAttribute('href', style_source); | |
return false; | |
} | |
}; | |
cube.ui.style.SourceSwitcher.setup = function (options) { | |
new cube.ui.style.SourceSwitcher(options); | |
}; | |
cube.ui.image = {}; | |
cube.ui.image.rollOver = { | |
nameDelimiter: '_', | |
onSuffix: '1', | |
offSuffix: '2', | |
image: { | |
on: uiImageRollOverImageOn, | |
off: uiImageRollOverImageOff | |
}, | |
background: { | |
on: uiImageRollOverBackgroundOn, | |
off: uiImageRollOverBackgroundOff | |
} | |
}; | |
function uiImageRollOverImageOn (element) { | |
var image = $(element); | |
image.attr('src', uiImageRollOverOn.apply(this, [image.attr('src')])); | |
} | |
function uiImageRollOverImageOff (element) { | |
var image = $(element); | |
image.attr('src', uiImageRollOverOff.apply(this, [image.attr('src')])); | |
} | |
function uiImageRollOverBackgroundOn (element) { | |
var image = $(element); | |
image.css('background-image', uiImageRollOverOn.apply(this, [image.css('background-image')])); | |
} | |
function uiImageRollOverBackgroundOff (element) { | |
var image = $(element); | |
image.attr('background-image', uiImageRollOverOff.apply(this, [image.css('background-image')])); | |
} | |
function uiImageRollOverOn (value) { | |
return value.replace(/^(.+)\/([^\/]+)\.([^\.]+)$/, function ($$, $1, $2, $3) { | |
var pieces = $2.split(cube.ui.image.rollOver.nameDelimiter); | |
pieces.pop(); | |
pieces.push(cube.ui.image.rollOver.onSuffix); | |
return $1 + '/' + pieces.join('_') + '.' + $3; // ← 顔だらけ | |
}); | |
} | |
function uiImageRollOverOff (value) { | |
return value.replace(/^(.+)\/([^\/]+)\.([^\.]+)$/, function ($$, $1, $2, $3) { | |
var pieces = $2.split(cube.ui.image.rollOver.nameDelimiter); | |
pieces.pop(); | |
pieces.push(cube.ui.image.rollOver.offSuffix); | |
return $1 + '/' + pieces.join('_') + '.' + $3; // ← 顔だらけ | |
}); | |
} | |
})(this, window, document, jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment