0
1
Fork 0
hedera-web-mindshore/package/usr/share/hedera-web/modules/home/home.js

144 lines
3.4 KiB
JavaScript
Executable File

Vn.Home = new Class
({
Extends: Vn.Module
,dateFormat: _('%a, %e %b %Y')
,activate: function ()
{
this.answer = new Htk.RadioGroup ();
var query = 'SELECT title, date_time, text, id FROM news '
+'ORDER BY priority, date_time DESC';
this.conn.execQuery (query, this.newsQueryDone.bind (this));
/* var query = 'SELECT question, @id := id FROM survey ORDER BY id DESC LIMIT 1; '
+'SELECT id, answer, votes FROM survey_answer WHERE survey_id = @id';
this.conn.execQuery (query, this.answersQueryDone.bind (this));
var voteButton = document.getElementById ('vote-button');
voteButton.addEventListener ('click', this.voteClicked.bind (this));
*/ }
,newsQueryDone: function (resultSet)
{
var newsColumn = document.getElementById ('news-column');
var res = resultSet.fetchResult ();
if (res)
while (res.next ())
{
var div = document.createElement ('div');
div.className = 'new';
var title = document.createElement ('h2');
title.appendChild (document.createTextNode (res.get ('title')));
div.appendChild (title);
var p = document.createElement ('p');
p.className = 'new-info';
div.appendChild (p);
var date = Vn.Date.strftime (res.get ('date_time'), this.dateFormat);
p.appendChild (document.createTextNode (date));
var text = document.createElement ('div');
text.className = 'new-text';
div.appendChild (text);
var html = '' + res.get ('text');
text.innerHTML = html;
var img = document.createElement ('img');
img.alt = '';
img.src = Vn.Config['image_dir'] + '/news/full/' + res.get ('id') + '.png';
div.appendChild (img);
newsColumn.appendChild (div);
}
}
,answersQueryDone: function (resultSet)
{
var value = resultSet.fetchValue ();
var question = document.getElementById ('question');
question.appendChild (document.createTextNode (value));
var res = resultSet.fetchResult ();
this.totalVotes = 0;
var answers = document.getElementById ('answers');
if (res)
while (res.next ())
{
var tr = document.createElement ('tr');
answers.appendChild (tr);
var td = document.createElement ('td');
td.className = 'radio';
tr.appendChild (td);
var radio = this.answer.createButton (res.get ('id'));
td.appendChild (radio);
var td = document.createElement ('td');
tr.appendChild (td);
var label = document.createElement ('label');
label.appendChild (document.createTextNode (res.get ('answer')));
td.appendChild (label);
var tr = document.createElement ('tr');
answers.appendChild (tr);
var td = document.createElement ('td');
tr.appendChild (td);
var td = document.createElement ('td');
tr.appendChild (td);
var text = document.createTextNode (res.get ('votes') + ' ' + _('votes'));
td.appendChild (text);
this.totalVotes += res.get ('votes');
}
this.refreshVotes ();
}
,refreshVotes: function ()
{
var totalNode = Vn.get ('total');
Vn.Node.setText (totalNode, this.totalVotes);
}
,voteClicked: function ()
{
if (this.answer.value)
{
var query = 'CALL survey_vote (#id)';
var batch = new Sql.Batch ();
batch.addParam ('id', this.answer);
this.conn.execQuery (query, this.voteDone.bind (this), batch);
}
else
alert (_('NoAnswerSelected'));
}
,voteDone: function (resultSet)
{
if (resultSet.fetchResult ())
{
this.totalVotes++;
this.refreshVotes ();
alert (_('ThanksForVote'));
}
}
});