Acme-MUDLike

 view release on metacpan or  search on metacpan

lib/Acme/MUDLike.pm  view on Meta::CPAN

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009 by Scott Walters

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.9 or,
at your option, any later version of Perl 5 you may have available.
By using this software, you signify that you like llamas.

Includes code by John Resig:

 jQuery 1.1.2 - New Wave Javascript

 Copyright (c) 2007 John Resig (jquery.com) 
 Dual licensed under the MIT (MIT-LICENSE.txt)
 and GPL (GPL-LICENSE.txt) licenses.

 $Date: 2007-02-28 12:03:00 -0500 (Wed, 28 Feb 2007) $
 $Rev: 1465 $

Includes code by Awwaiid (Brock Wilcox)

=cut

package Acme::MUDLike::data;

sub chat_js {

return <<'EOF';

var poll_count = 0;

function new_request() {
  var req;
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  } 
  return req;
}

function do_request(url, callback) {
  var req = new_request();
  if(req != undefined) {
    req.onreadystatechange = function() {
      if (req.readyState == 4) { // only if req is "loaded"
        if (req.status == 200) { // only if "OK"
          if(callback) callback(req.responseText);
        } else {
          alert("AJAX Error:\r\n" + req.statusText);
        }
      }
    }
    req.open("GET", url, true);
    req.send("");
  }
}

function setup_poll() {
   setTimeout('poll_server()', 1000);
}

function poll_server() {
  var nick = document.getElementById("nick").value;
  var admin = document.getElementById("admin").value;
  document.getElementById('status').innerHTML = 'Polling ('+(poll_count++)+')...';
  do_request('/pushstream/?nick=' + nick + '&admin=' + admin, got_update);
}

function got_update(txt) {
  document.getElementById('status').innerHTML = 'Got update.'
  if(document.getElementById("log").innerHTML != txt)
    document.getElementById("log").innerHTML = txt;
  setup_poll();
}

// This stuff gets executed once the document is loaded
$(function(){
  // Start up the long-pull cycle
  setup_poll();
  // Unobtrusively make submitting a message use send_message()
  $('#f').submit(send_message);
});

// We also send messages using AJAX
function send_message() {
  var nick = $('#nick').val();
  var admin = $('#admin').val();
  var message = $('#message').val();
  $('#log').load('/sendmessage', {
    nick:     nick,
    admin:    admin,
    action:   'ajaxchat',
    message:  message
  }, function() {
    $('#message').val('');
    $('#message').focus();
  });
  return false;
}

EOF

}

sub jquery {

return <<'EOF';

/* prevent execution of jQuery if included more than once */
if(typeof window.jQuery == "undefined") {
/*
 * jQuery 1.1.2 - New Wave Javascript
 *
 * Copyright (c) 2007 John Resig (jquery.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2007-02-28 12:03:00 -0500 (Wed, 28 Feb 2007) $
 * $Rev: 1465 $

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 6.748 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-72ae3ad1e6da )