2014-09-21 02:29:33 +02:00
/ *
2015-03-12 07:58:49 +01:00
* options / user - css . js - allow user to enter custom css entries and ( march 2015 ) replaces the old stylesheet system
2014-09-21 02:29:33 +02:00
*
* Copyright ( c ) 2014 Marcin Łabanowski < marcin @ 6 irc . net >
2015-03-12 07:58:49 +01:00
* Copyright ( c ) 2015 Fredrick Brennan < admin @ 8 chan . co >
2014-09-21 02:29:33 +02:00
*
* Usage :
* $config [ 'additional_javascript' ] [ ] = 'js/jquery.min.js' ;
* $config [ 'additional_javascript' ] [ ] = 'js/options.js' ;
* $config [ 'additional_javascript' ] [ ] = 'js/options/user-css.js' ;
* /
+ function ( ) {
2015-03-12 07:58:49 +01:00
var tab = Options . add _tab ( "user-css" , "css3" , _ ( "Theme" ) ) ;
2014-09-21 02:29:33 +02:00
var textarea = $ ( "<textarea></textarea>" ) . css ( {
2015-03-12 07:58:49 +01:00
"height" : "80%" ,
2015-02-14 01:01:23 +01:00
"width" : "100%" ,
"font-size" : "9pt" ,
"font-family" : "monospace" ,
2014-09-21 02:29:33 +02:00
} ) . appendTo ( tab . content ) ;
2015-02-14 01:07:38 +01:00
var submit = $ ( "<input type='button' value='" + _ ( "Save custom CSS" ) + "'>" ) . css ( {
2015-02-14 01:01:23 +01:00
"width" : "100%" ,
2014-09-21 02:29:33 +02:00
} ) . click ( function ( ) {
localStorage . user _css = textarea . val ( ) ;
apply _css ( ) ;
} ) . appendTo ( tab . content ) ;
2015-03-12 08:44:33 +01:00
var main = function ( ) {
if ( typeof styles === "undefined" ) return ;
2015-03-12 07:58:49 +01:00
var stylechooser = $ ( "<select id='stylechooser'></select>" ) . appendTo ( tab . content ) ;
2015-03-12 08:35:08 +01:00
// Handle empty localStorage
if ( ! localStorage . stylesheets _all _boards ) localStorage . stylesheets _all _boards = "false" ;
if ( ! localStorage . board _stylesheets ) localStorage . board _stylesheets = '{}' ;
2015-03-12 07:58:49 +01:00
var fix _choice = function ( ) {
2015-03-18 10:00:21 +01:00
var chosen = false ;
2015-03-12 07:58:49 +01:00
stylechooser . empty ( ) ;
$ . each ( styles , function ( k , v ) {
var ps ;
k === "Custom" ? k2 = _ ( "Board-specific CSS" ) : k2 = k ;
2015-03-18 10:00:21 +01:00
var option = $ ( "<option value='" + k + "' " + ( k === "Custom" ? "class='default'" : "" ) + ">" + k2 + "</option>" ) . appendTo ( stylechooser ) ;
2015-03-12 07:58:49 +01:00
if ( localStorage . stylesheets _all _boards === "false" ) {
var bs = JSON . parse ( localStorage . board _stylesheets ) ;
if ( bs [ board _name ] ) ps = bs [ board _name ] ;
}
2015-03-18 10:00:21 +01:00
if ( ( k === localStorage . stylesheet && localStorage . stylesheets _all _boards === "true" ) || ( localStorage . stylesheets _all _boards === "false" && ( ps && k === ps ) ) ) { option . prop ( 'selected' , 'selected' ) ; chosen = true }
2015-03-12 07:58:49 +01:00
} )
2015-03-18 10:00:21 +01:00
if ( ! chosen ) stylechooser . find ( '.default' ) . prop ( 'selected' , 'selected' ) ;
2015-03-12 07:58:49 +01:00
} ;
fix _choice ( ) ;
var allboards = $ ( "<label><input type='checkbox' id='css-all-boards'> " + _ ( "Style should affect all boards, not just current board" ) + " (/" + board _name + "/)</label>" ) . appendTo ( tab . content ) . find ( 'input' ) ;
if ( localStorage . stylesheets _all _boards === "true" ) allboards . prop ( 'checked' , 'checked' ) ;
allboards . on ( 'change' , function ( e ) {
if ( $ ( this ) . is ( ':checked' ) ) {
localStorage . stylesheets _all _boards = "true" ;
} else {
localStorage . stylesheets _all _boards = "false" ;
}
fix _choice ( ) ;
apply _css ( ) ;
} ) ;
stylechooser . on ( 'change' , function ( e ) {
var ps = $ ( this ) . val ( ) ;
if ( localStorage && localStorage . stylesheets _all _boards === "false" ) {
var bs = JSON . parse ( localStorage . board _stylesheets ) ;
if ( ! bs ) bs = { } ;
bs [ board _name ] = ps ;
localStorage . board _stylesheets = JSON . stringify ( bs ) ;
} else {
localStorage . stylesheet = ps ;
}
apply _css ( ) ;
} ) ;
update _textarea ( ) ;
2015-03-12 08:44:33 +01:00
}
2015-03-12 07:58:49 +01:00
2014-09-21 02:29:33 +02:00
var apply _css = function ( ) {
2015-03-12 07:58:49 +01:00
var to _apply ;
if ( localStorage && localStorage . stylesheets _all _boards === "false" ) {
if ( localStorage && localStorage . board _stylesheets ) {
var bs = JSON . parse ( localStorage . board _stylesheets ) ;
if ( bs && bs [ board _name ] ) {
to _apply = styles [ bs [ board _name ] ] ;
}
}
} else if ( localStorage && localStorage . stylesheet ) {
to _apply = styles [ localStorage . stylesheet ] ;
}
2014-09-21 02:29:33 +02:00
$ ( '.user-css' ) . remove ( ) ;
2015-03-12 07:58:49 +01:00
var ls = $ ( 'link[rel="stylesheet"]:not(#stylesheet)' ) . last ( ) ;
ls . after ( $ ( "<style></style>" )
. addClass ( "user-css" )
. text ( localStorage . user _css )
) ;
if ( to _apply ) {
$ ( '.user-chosen-stylesheet,#stylesheet' ) . remove ( ) ;
ls . after ( $ ( "<link rel='stylesheet'/>" )
. attr ( "class" , "user-chosen-stylesheet" )
. attr ( "id" , "stylesheet" )
2015-03-12 08:35:08 +01:00
. attr ( "href" , ( configRoot !== '/' ? configRoot : '' ) + to _apply )
2015-03-12 07:58:49 +01:00
) ;
}
return to _apply ;
2014-09-21 02:29:33 +02:00
} ;
var update _textarea = function ( ) {
if ( ! localStorage . user _css ) {
2015-02-26 04:51:45 +01:00
textarea . text ( "/* " + _ ( "Enter here your own CSS rules..." ) + " */\n" +
"/* " + _ ( "If you want to make a redistributable style, be sure to\nhave a Yotsuba B theme selected." ) + " */\n" +
2015-03-12 07:58:49 +01:00
"/* " + _ ( "These will be applied on top of whatever theme you choose below." ) + " */\n" +
2015-02-26 04:51:45 +01:00
"/* " + _ ( "You can include CSS files from remote servers, for example:" ) + " */\n" +
2014-11-02 00:05:56 +01:00
'/* @import "http://example.com/style.css"; */' ) ;
2014-09-21 02:29:33 +02:00
}
2015-03-12 07:58:49 +01:00
textarea . text ( localStorage . user _css ) ;
apply _css ( ) ;
2014-09-21 02:29:33 +02:00
} ;
2015-03-12 08:44:33 +01:00
main ( ) ;
2014-09-21 02:29:33 +02:00
} ( ) ;