mod-chat MDL-21534 Converted JS to YUI3 and new coding style also fixed several regressions.

This commit is contained in:
Sam Hemelryk 2010-02-15 05:56:36 +00:00
parent 98d3784e6d
commit 579a4976cc
8 changed files with 392 additions and 416 deletions

View file

@ -1,50 +0,0 @@
var waitFlag = false;
function empty_field_and_submit() {
if(waitFlag) {
return false;
}
waitFlag = true;
var input_chat_message = document.getElementById('input_chat_message');
document.getElementById('sendForm').chat_message.value = input_chat_message.value;
input_chat_message.value = '';
input_chat_message.className = 'wait';
document.getElementById('sendForm').submit();
enableForm();
return false;
}
function enableForm() {
var input_chat_message = document.getElementById('input_chat_message');
waitFlag = false;
input_chat_message.className = '';
input_chat_message.focus();
}
var timer = null
var f = 1; //seconds
function stop() {
clearTimeout(timer)
}
function start() {
timer = setTimeout("update()", f*1000);
YAHOO.util.Event.addListener(document.body, 'unload', stop);
}
function update() {
for(i=0; i<uidles.length; i++) {
el = document.getElementById(uidles[i]);
if (el != null) {
parts = el.innerHTML.split(":");
time = f + (parseInt(parts[0], 10)*60) + parseInt(parts[1], 10);
min = Math.floor(time/60);
sec = time % 60;
el.innerHTML = ((min < 10) ? "0" : "") + min + ":" + ((sec < 10) ? "0" : "") + sec;
}
}
timer = setTimeout("update()", f*1000);
}
function insert_redirect() {
parent.jsupdate.location.href = parent.jsupdate.document.anchors[0].href;
}

View file

@ -30,26 +30,31 @@ $context = get_context_instance(CONTEXT_MODULE, $cm->id);
//Get the user theme
$USER = $DB->get_record('user', array('id'=>$chatuser->userid));
$module = array(
'name' => 'mod_chat_js',
'fullpath' => '/mod/chat/gui_header_js/module.js',
'requires' => array('base', 'node')
);
$PAGE->requires->js_init_call('M.mod_chat_js.init', array(false), false, $module);
//Setup course, lang and theme
$PAGE->set_course($course);
$PAGE->requires->js('/mod/chat/gui_header_js/chat_gui_header.js', true);
$PAGE->set_pagelayout('embedded');
$PAGE->set_focuscontrol('input_chat_message');
$PAGE->set_cacheable(false);
echo $OUTPUT->header();
?>
<form action="../empty.php" method="post" target="empty" id="inputForm"
onsubmit="return empty_field_and_submit()" style="margin:0">
<input type="text" id="input_chat_message" name="chat_message" size="50" value="" />
<?php echo $OUTPUT->help_icon('chatting', get_string('helpchatting', 'chat'), 'chat', true); ?><br />
<input type="checkbox" id="auto" size="50" value="" checked="checked" /><label for="auto"><?php echo get_string('autoscroll', 'chat');?></label>
</form>
echo html_writer::start_tag('form', array('action'=>'../empty.php', 'method'=>'post', 'target'=>'empty', 'id'=>'inputForm', 'style'=>'margin:0'));
echo html_writer::empty_tag('input', array('type'=>'text', 'id'=>'input_chat_message', 'name'=>'chat_message', 'size'=>'50', 'value'=>''));
echo html_writer::empty_tag('input', array('type'=>'checkbox', 'id'=>'auto', 'checked'=>'checked', 'value'=>''));
echo html_writer::tag('label', array('for'=>'auto'), get_string('autoscroll', 'chat'));
echo $OUTPUT->help_icon('chatting', get_string('helpchatting', 'chat'), 'chat', true);
echo html_writer::end_tag('form');
<form action="insert.php" method="post" target="empty" id="sendForm">
<input type="hidden" name="chat_sid" value="<?php echo $chat_sid ?>" />
<input type="hidden" name="chat_message" />
</form>
<?php
echo $OUTPUT->footer();
?>
echo html_writer::start_tag('form', array('action'=>'insert.php', 'method'=>'post', 'target'=>'empty', 'id'=>'sendForm'));
echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'chat_sid', 'value'=>$chat_sid));
echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'chat_message', 'id'=>'insert_chat_message'));
echo html_writer::end_tag('form');
echo $OUTPUT->footer();

View file

@ -0,0 +1,73 @@
YUI.add('mod_chat_js', function(Y){
M.mod_chat_js = {
waitflag : false,
timer : null,
timeout : 1,
users : [],
init : function(Y, users) {
if (users) {
this.users = users;
this.start();
Y.one(document.body).on('unload', this.stop, this);
}
var inputform = Y.one('#inputForm');
if (inputform) {
inputform.on('submit', this.submit, this);
}
},
start : function() {
this.timer = setTimeout(function(self){
self.update();
}, this.timeout*1000, this);
},
stop : function() {
clearTimeout(this.timer);
},
update : function() {
for (var i in this.users) {
var el = Y.one('#uidle'+this.users[i]);
if (el) {
var parts = el.get('innerHTML').split(':');
var time = this.timeout + (parseInt(parts[0], 10)*60) + parseInt(parts[1], 10);
var min = Math.floor(time/60);
var sec = time % 60;
el.set('innerHTML', ((min < 10) ? "0" : "") + min + ":" + ((sec < 10) ? "0" : "") + sec);
}
}
this.start();
},
insert_redirect : function() {
parent.jsupdate.location.href = parent.jsupdate.document.anchors[0].href;
},
enable_form : function(el) {
this.waitflag = false;
el.set('className','');
el.focus();
},
submit : function(e) {
e.halt();
if(this.waitflag) {
return false;
}
this.waitflag = true;
var inputchatmessage = Y.one('#input_chat_message');
Y.one('#insert_chat_message').set('value', inputchatmessage.get('value'));
inputchatmessage.set('value', '');
inputchatmessage.addClass('wait');
Y.one('#sendForm').submit();
this.enable_form(inputchatmessage);
return false;
}
}
}, '2.0.0', {requires:['base','node']});

View file

@ -63,25 +63,25 @@ if (!$chatusers = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->gro
}
$uidles = Array();
$i = 0;
foreach ($chatusers as $chatuser) {
$uidles[$i] = 'uidle{$chatuser->id}';
$i++;
$uidles[] = $chatuser->id;
}
$PAGE->requires->data_for_js('uidles', $uidles);
$PAGE->requires->js('/mod/chat/gui_header_js/chat_gui_header.js');
$PAGE->requires->js_function_call('start', null, true);
ob_start();
echo $OUTPUT->header();
$module = array(
'name' => 'mod_chat_js',
'fullpath' => '/mod/chat/gui_header_js/module.js',
'requires' => array('base', 'node')
);
$PAGE->requires->js_init_call('M.mod_chat_js.init', array($uidles), false, $module);
/// Print user panel body
$timenow = time();
$stridle = get_string('idle', 'chat');
$strbeep = get_string('beep', 'chat');
echo '<div style="display: none"><a href="'.$refreshurl.'" id="refreshLink">Refresh link</a></div>';
echo '<table width="100%">';
$table = new html_table();
$table->width = '100%';
$table->data = array();
foreach ($chatusers as $chatuser) {
$lastping = $timenow - $chatuser->lastmessageping;
$min = (int) ($lastping/60);
@ -89,19 +89,25 @@ foreach ($chatusers as $chatuser) {
$min = $min < 10 ? '0'.$min : $min;
$sec = $sec < 10 ? '0'.$sec : $sec;
$idle = $min.':'.$sec;
echo '<tr><td width="35">';
echo "<a target=\"_blank\" onClick=\"return openpopup('/user/view.php?id=$chatuser->id&amp;course=$courseid','user$chatuser->id','');\" href=\"$CFG->wwwroot/user/view.php?id=$chatuser->id&amp;course=$courseid\">";
echo $OUTPUT->user_picture($chatuser, array('courseid'=>$courseid));
echo '</a></td><td valign="center">';
echo '<p><font size="1">';
echo fullname($chatuser).'<br />';
echo "<span class=\"dimmed_text\">$stridle <span name=\"uidles\" id=\"uidle{$chatuser->id}\">$idle</span></span>";
echo " <a href=\"users.php?chat_sid=$chat_sid&amp;beep=$chatuser->id\">$strbeep</a>";
echo '</font></p>';
echo '</td></tr>';
$row = array();
//$popupid = $OUTPUT->add_action_handler(new component_action('click', 'openpopup', array('url'=>'/user/view.php?id=$chatuser->id&amp;course=$courseid', 'name'=>'user'.$chatuser->id, 'options'=>'')));
$row[0] = $OUTPUT->user_picture($chatuser, array('courseid'=>$courseid, 'popup'=>true));
$row[1] = html_writer::start_tag('p');
$row[1] .= html_writer::start_tag('font', array('size'=>'1'));
$row[1] .= fullname($chatuser).'<br />';
$row[1] .= html_writer::tag('span', array('class'=>'dimmed_text'), $stridle . html_writer::tag('span', array('name'=>'uidles', 'id'=>'uidle'.$chatuser->id), $idle)).' ';
$row[1] .= html_writer::tag('a', array('href'=>new moodle_url('/mod/chat/gui_header_js/users.php', array('chat_sid'=>$chat_sid, 'beep'=>$chatuser->id))), $strbeep);
$row[1] .= html_writer::end_tag('font');
$row[1] .= html_writer::end_tag('p');
$table->data[] = $row;
}
// added 2 </div>s, xhtml strict complaints
echo '</table>';
ob_start();
echo $OUTPUT->header();
echo html_writer::tag('div', array('style'=>'display:none'), html_writer::tag('a', array('href'=>$refreshurl, 'id'=>'refreshLink'), 'Refresh link'));
echo $OUTPUT->table($table);
echo $OUTPUT->footer();
//