Created
September 24, 2014 07:23
-
-
Save ironboy/7c4f93137b841c01bf9e 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
// ------------------------------------------------------- | |
// LESS-reg-js 1.0 | |
// (c) Nodebite 2014, Thomas Frank | |
// MIT license, free to use everywhere. | |
// | |
// Register a JavaScript as a LESS function. | |
// (Yes! This is valid LESS!) | |
// | |
// ------------------------------------------------------- | |
// | |
// Usage: | |
// ------------------------------------------------------- | |
// | |
// Register a function | |
// | |
// .reg-js(`reg('functionName',function([arg1..arg5]){ | |
// | |
// A normal JavaScript function | |
// that MUST return an JSON-eqsque object such as | |
// { | |
// h1:{"padding":0,"margin":"3px"}, | |
// h2:{"padding-top":"5px","overflow":"hidden"} | |
// } etc. | |
// | |
// })`); | |
// | |
// Note: To return several rules for the same selector | |
// use "h2", "h2_", "h2__" etc as keys/names... | |
// | |
// ------------------------------------------------------- | |
// | |
// Call a function: | |
// | |
// .run-js("functionName",[arg1..arg5]); | |
// | |
// Note: To call a JS-function within another JS-function: | |
// this.functionName([arg1..arg5]); | |
// | |
// ------------------------------------------------------- | |
// | |
// register a javascript function | |
.reg-js(@def){} | |
.reg-js(`(function(){ | |
this.reg = function(methodname,method){ | |
this.api = this.api || {}; | |
this.api[methodname] = method; | |
return " "; | |
}; | |
return " "; | |
})()`); | |
// run a javascript function | |
.run-js(@methodname,@a1:"",@a2:"",@a3:"",@a4:"",@a5:""){ | |
@temp: ~`(function(){ | |
var css = this.api.jsonToCss( | |
this.api[@{methodname}](@{a1},@{a2},@{a3},@{a4},@{a5}) | |
); | |
var i = css.indexOf("{"); | |
var j = css.indexOf(":"); | |
this.tres1 = css.substring(0,i); | |
this.tres2 = css.substring(i+4,j); | |
this.tres3 = css.substring(j+2,css.lastIndexOf(";")); | |
return ' '; | |
})()`; | |
// break up the funciton result in 3 vars | |
// to get around LESS limitiation on variable output | |
@r1: ~`(function(){ return this.tres1 || ' '; })()`; | |
@r2: ~`(function(){ return this.tres2 || ' '; })()`; | |
@r3: ~`(function(){ return this.tres3 || ' '; })()`; | |
@{r1} {@{r2}:@r3} | |
} | |
// helper function to convert | |
// a json-esque structure to css | |
.reg-js(`reg('jsonToCss',function(json){ | |
var css = ""; | |
for(var i in json){ | |
css += i.split("_").join("") + ' {\n'; | |
for(var j in json[i]){ | |
css += " " + j + ": " + json[i][j] + ";\n"; | |
} | |
css += "}\n\n"; | |
} | |
css = css || " "; | |
return css; | |
})`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment