github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/mist/assets/muted/muted.js (about)

     1  // Copyright (c) 2013-2014, Jeffrey Wilcke. All rights reserved.
     2  //
     3  // This library is free software; you can redistribute it and/or
     4  // modify it under the terms of the GNU General Public
     5  // License as published by the Free Software Foundation; either
     6  // version 2.1 of the License, or (at your option) any later version.
     7  //
     8  // This library is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    11  // General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this library; if not, write to the Free Software
    15  // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    16  // MA 02110-1301  USA
    17  
    18  // Helper function for generating pseudo callbacks and sending data to the QML part of the application
    19  function postData(data, cb) {
    20  	data._seed = Math.floor(Math.random() * 1000000)
    21  	if(cb) {
    22  		Muted._callbacks[data._seed] = cb;
    23  	}
    24  
    25  	if(data.args === undefined) {
    26  		data.args = [];
    27  	}
    28  
    29  	navigator.qt.postMessage(JSON.stringify(data));
    30  }
    31  
    32  window.Muted = {
    33  	prototype: Object(),
    34  }
    35  
    36  window.Muted._callbacks = {}
    37  window.Muted._onCallbacks = {}
    38  
    39  function debug(/**/) {
    40  	console.log("hello world")
    41  
    42  	var args = arguments;
    43  	var msg = ""
    44  	for(var i = 0; i < args.length; i++){
    45  		if(typeof args[i] == "object") {
    46  			msg += " " + JSON.stringify(args[i])
    47  		} else {
    48  			msg += args[i]
    49  		}
    50  	}
    51  
    52  	document.querySelector("#debugger").innerHTML += "<div class='line'><div class='col1'></div><div class='col2'>"+msg+"</div></div>";
    53  }
    54  console.log = function() {
    55  	var args = []
    56  	for(var i = 0; i < arguments.length; i++) {
    57  		args.push(arguments[i]);
    58  	}
    59  	postData({call:"log", args:args})
    60  }
    61  
    62  navigator.qt.onmessage = function(ev) {
    63  	var data = JSON.parse(ev.data)
    64  
    65  	if(data._event !== undefined) {
    66  		Muted.trigger(data._event, data.data);
    67  	} else {
    68  		if(data._seed) {
    69  			var cb = Muted._callbacks[data._seed];
    70  			if(cb) {
    71  				// Call the callback
    72  				cb(data.data);
    73  				// Remove the "trigger" callback
    74  				delete Muted._callbacks[ev._seed];
    75  			}
    76  		}
    77  	}
    78  }