asciiblaster

- draw irc art in your web browser
git clone git://git.acid.vegas/asciiblaster.git
Log | Files | Refs | Archive | README

tool.js (4255B)

      1 var Tool = Model({
      2   init: function (el) {
      3     this.el = el
      4     this.lex = new Lex (el)
      5     this.name = el.innerHTML
      6   },
      7   bind: function(){
      8     var tool = this
      9     tool.el.addEventListener('mousedown', function(e){
     10       tool.focus()
     11     })
     12     tool.el.addEventListener('contextmenu', function(e){
     13       tool.context(e)
     14     })
     15     if (tool.memorable) {
     16       // console.log(tool.name, localStorage.getItem("ascii.tools." + tool.name) )
     17       tool.use( localStorage.getItem("ascii.tools." + tool.name) == "true" )
     18     }
     19   },
     20   use: function(){},
     21   context: function(e){},
     22   done: function(){},
     23   focus: function(){
     24     // focused && focused.blur()
     25     current_tool && current_tool.blur()
     26     current_tool = this
     27     this.el.classList.add('focused')
     28     this.use()
     29     if (this.name != 'shader' && is_desktop) { cursor_input.focus() }
     30   },
     31   blur: function(){
     32     current_tool = null
     33     this.el.classList.remove('focused')
     34     this.done()
     35   }
     36 })
     37 
     38 var FileTool = Tool.extend({
     39   focus: function(){
     40     if (current_filetool === this) {
     41       this.blur()
     42       return
     43     }
     44     current_filetool && current_filetool.blur()
     45     current_filetool = this
     46     this.el.classList.add('focused')
     47     this.use()
     48     if (this.name != 'shader' && is_desktop) { cursor_input.focus() }
     49   },
     50   blur: function(){
     51     current_filetool = null
     52     this.el.classList.remove('focused')
     53     this.done()
     54   }
     55 })
     56 
     57 var RadioItem = Tool.extend({
     58   init: function(group, el){
     59     this.group = group
     60     this.el = el
     61   },
     62   focus: function(){
     63     this.el.classList.add('focused')
     64   },
     65   blur: function(){
     66     this.el.classList.remove('focused')
     67   },
     68   bind: function(){
     69     var control = this
     70     this.el.addEventListener('mousedown', function(){
     71       control.group.use(control)
     72     })
     73   }
     74 })
     75 
     76 var RadioGroup = Tool.extend({
     77   init: function(el){
     78     this.el = el
     79     this.controls = {}
     80     var names = el.innerHTML.split(' ')
     81     el.innerHTML = ''
     82     var group = this
     83     names.forEach(function(value){
     84       var el = document.createElement('span')
     85       el.classList.add('radio','tool')
     86       var control = new RadioItem(group, el)
     87       if (value.substr(0,1) === '*') {
     88         control.value = value = value.substr(1)
     89         group.use(control)
     90       }
     91       control.value = el.innerHTML = value
     92       group.controls[value] = control
     93       group.el.appendChild(el)
     94     })
     95   },
     96   use: function(control){
     97     if (typeof control === 'string') {
     98       control = this.controls[control]
     99     }
    100     this.selected_control && this.selected_control.blur()
    101     this.value = control.value
    102     this.selected_control = control
    103     control.focus()
    104     control.use()
    105     if (this.memorable){
    106       localStorage.setItem("ascii.tools." + this.name, this.value)
    107     } 
    108   },
    109   bind: function(){
    110     var tool = this
    111     for (var n in this.controls){
    112       this.controls[n].bind()
    113     }
    114     if (tool.memorable) {
    115       var value = localStorage.getItem("ascii.tools." + tool.name)
    116       if (value) tool.use(value)
    117     }
    118   }
    119 })
    120 
    121 var Checkbox = Tool.extend({
    122   init: function (el){
    123     this.__init(el)
    124     var name = this.name.replace(/^[x_] /,"")
    125     var state = localStorage.getItem("ascii.tools." + name) == "true" || this.name[0] == "x"
    126     this.name = name
    127     this.update(state)
    128   },
    129   update: function(state){
    130     if (state) this.el.innerHTML = "x " + this.name
    131     else       this.el.innerHTML = "_ " + this.name
    132     if (this.memorable) { localStorage.setItem("ascii.tools." + this.name, !! state) }
    133   }
    134 })
    135 
    136 var BlurredCheckbox = Checkbox.extend({
    137   focus: function(){
    138     this.use()
    139   },
    140   blur: function(){
    141     this.el.classList.remove('focused')
    142     this.done()
    143   }
    144 })
    145 
    146 var BlurredTool = Tool.extend({
    147   focus: function(){
    148     this.use()
    149   },
    150   blur: function(){
    151     this.el.classList.remove('focused')
    152     this.done()
    153   }
    154 })
    155 
    156 var HiddenCheckbox = BlurredCheckbox.extend({
    157   on: "o",
    158   off: ".",
    159   init: function (el){
    160     this.el = el
    161     this.lex = new Lex (el)
    162     this.name = this.el.id
    163     var state = localStorage.getItem("ascii.tools." + name) == "true" || this.el.innerHTML[0] == this.on
    164     this.update(state)
    165   },
    166   update: function(state){
    167     this.el.innerHTML = state ? this.on : this.off
    168     if (this.memorable) { localStorage.setItem("ascii.tools." + this.name, !! state) }
    169   }
    170 })