template2.js 13.4 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

/*
	Sankore API
*/

function startEditing()
{
    modeEdit();
}

function stopEditing()
{
    modeView();
}

function initialize()
{
	
}

function checkResponse()
{
    checkWord();
}
/*
	/ END sankore api
*/

var sankoreLang = {
bmagnin's avatar
bmagnin committed
30
    view: "Display", 
Claudio Valerio's avatar
Claudio Valerio committed
31 32 33 34
    edit: "Edit",
    example: "a*long*,*long*time*ago*...",
    wgt_name: "Order words",
    reload: "Reload",
bmagnin's avatar
bmagnin committed
35 36 37
    slate: "slate",
    pad: "pad",
    none: "none",
unknown's avatar
unknown committed
38
    help: "Help",
39 40 41 42 43 44 45 46
    help_content: 
"<p><h2>Order words </h2> </p>" +
"<p><h3>Order words to reconstruct a correct sentence</h3></p>" +

"<p>The interactivity displays labels in a random order. Drag and drop words in correct order. If the result is correct, the area turns in green.</p> "+
"<p>“Reload” button resets the exercises. </p>" +

"<p> Enter the “Edit” mode to : </p>" +
bmagnin's avatar
bmagnin committed
47
"<ul> <li> choose the theme of the App : pad, slate, or none (by default : none), </li>" +
48 49 50 51 52 53
"<li> Determine the sentence and place separation between words. </li> </ul>" +

 "<p>To create a new exercise : </p>" +
"<ul><li>insert the desired sentence,</li>" +
"<li> To add spaces in the sentence, insert “*” between each word. Do not place a star at the beginning or the end of your sentence.</li></ul>" +
"<p>“Display” button comes back to the activity.</p>",
maximXbs's avatar
maximXbs committed
54
    theme: "Theme"
Claudio Valerio's avatar
Claudio Valerio committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
};

var word = "";
var curWord = "";

if(window.sankore){
    word = (sankore.preference("rightOrdWords", ""))?sankore.preference("rightOrdWords", ""):sankoreLang.example;
    curWord = (sankore.preference("currentOrdWords", ""))?sankore.preference("currentOrdWords", ""):"";
} else {
    word = sankoreLang.example;
}

// array of dom elements
var letters = [];

var editMode = false; // just a flag

unknown's avatar
unknown committed
72 73
var wgtState = false; // just another flag

Claudio Valerio's avatar
Claudio Valerio committed
74 75 76 77 78 79 80 81 82 83
// if use the "edit" button or rely on the api instead
var isSankore = false;
// whether to do window.resize or not (window = widget area)
var isBrowser = ( typeof( widget ) == "undefined" );

// hardcoded parameters, not very good
var input_width = 606;
var widget_padding = 0;
var min_view_width = 400;

maximXbs's avatar
maximXbs committed
84

Claudio Valerio's avatar
Claudio Valerio committed
85
$(document).ready(function(){
unknown's avatar
unknown committed
86
    if(window.sankore){
unknown's avatar
unknown committed
87 88 89
        if(sankore.preference("ord_words_style","")){
            changeStyle(sankore.preference("ord_words_style",""));
        } else
unknown's avatar
unknown committed
90
            changeStyle("3")
unknown's avatar
unknown committed
91 92
    } else 
        changeStyle("3")
Claudio Valerio's avatar
Claudio Valerio committed
93 94
    $("#wgt_display").text(sankoreLang.view);
    $("#wgt_edit").text(sankoreLang.edit);
unknown's avatar
unknown committed
95
    $("#wgt_help").text(sankoreLang.help);
maximXbs's avatar
maximXbs committed
96 97 98 99 100 101 102 103 104 105
    $("#help").html(sankoreLang.help_content);    
    $("#style_select option[value='1']").text(sankoreLang.slate);
    $("#style_select option[value='2']").text(sankoreLang.pad);
    $("#style_select option[value='3']").text(sankoreLang.none);
    var tmpl = $("div.inline label").html();
    $("div.inline label").html(sankoreLang.theme + tmpl)
    
    $("#style_select").change(function (event){
        changeStyle($(this).find("option:selected").val());
    })
unknown's avatar
unknown committed
106
    
Claudio Valerio's avatar
Claudio Valerio committed
107 108 109 110 111
    $("#wgt_display, #wgt_edit").click(function(event){
        if(this.id == "wgt_display"){
            if(!$(this).hasClass("selected")){                
                $(this).addClass("selected");
                $("#wgt_edit").removeClass("selected");
maximXbs's avatar
maximXbs committed
112
                $("#parameters").css("display","none");                
Claudio Valerio's avatar
Claudio Valerio committed
113 114 115 116 117 118 119 120
                $(this).css("display", "none");
                $("#wgt_edit").css("display", "block");
                modeView();
            }
        } else {            
            if(!$(this).hasClass("selected")){
                $(this).addClass("selected");
                $("#wgt_display").removeClass("selected");
maximXbs's avatar
maximXbs committed
121
                $("#parameters").css("display","block");                
Claudio Valerio's avatar
Claudio Valerio committed
122 123 124 125 126 127
                $(this).css("display", "none");
                $("#wgt_display").css("display", "block");
                modeEdit();
            }
        }
    });
unknown's avatar
unknown committed
128
    
Claudio Valerio's avatar
Claudio Valerio committed
129
    $("#wgt_name").text(sankoreLang.wgt_name);
unknown's avatar
unknown committed
130
    
unknown's avatar
unknown committed
131 132 133
    $("#wgt_help").click(function(){
        var tmp = $(this);
        if($(this).hasClass("open")){
maximXbs's avatar
maximXbs committed
134
            $(this).removeClass("help_pad").removeClass("help_wood")
unknown's avatar
unknown committed
135 136 137 138 139
            $("#help").slideUp("100", function(){
                tmp.removeClass("open");
                $("#ub-widget").show();
            });
        } else {            
maximXbs's avatar
maximXbs committed
140
            ($("#style_select").val() == 1)?$(this).removeClass("help_pad").addClass("help_wood"):$(this).removeClass("help_wood").addClass("help_pad");
unknown's avatar
unknown committed
141 142 143 144 145 146 147
            $("#ub-widget").hide();
            $("#help").slideDown("100", function(){
                tmp.addClass("open");
            });
        }
    });
    
Claudio Valerio's avatar
Claudio Valerio committed
148
    $("#wgt_reload").text(sankoreLang.reload).click(function(){
unknown's avatar
unknown committed
149 150 151 152 153 154 155 156
        if(wgtState)
            $("#wgt_display").trigger("click");
        else
        {
            $( "#mp_word" ).empty();
	
            // create new set of letters
            var letters;
unknown's avatar
unknown committed
157
            letters = shuffle( createWordLetters( word ) );
unknown's avatar
unknown committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    
            for( i in letters ){
                $("#mp_word").append( letters[i] );
            }
	
            // in sankore api there would be a function to check 
            // the answer, so no update parameter would be needed
            if( !isSankore ){
                $( "#mp_word" ).sortable( {
                    update: checkWord
                } );
            } else $( "#mp_word" ).sortable();

            // adjustWidth
            var totalLettersWidth = 0;
            for( i in letters ){
                var currentWidth = $( letters[i] ).outerWidth( true );
                totalLettersWidth += currentWidth;
            }
            totalLettersWidth += 1;

            var width = Math.max(
                totalLettersWidth,
                min_view_width
                );
	
            // shift the words to the right to center them
            if( width > totalLettersWidth ){
                $( "#mp_word" ).css( "margin-left", Math.round( (width - totalLettersWidth)/2 ) );
            }
            else{
                $( "#mp_word" ).css( "margin-left", 0 );
            }
        }
maximXbs's avatar
maximXbs committed
192
    });    
Claudio Valerio's avatar
Claudio Valerio committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    
})

/*
=================
createWordLetters
=================
returns array of dom elements
*/
function createWordLetters( word )
{
    var ch, el;
    var letters = [];
	
    if( word.indexOf( '*' ) != -1 )
    {
        var tmp = word.split( '*' );
        for( i in tmp )
        {
            ch = tmp[i];
            el = document.createElement( "div" );
            $(el).addClass( "letter" ).text( ch );
            letters.push( el );
        }
    }
    else
    {
        for( var i = 0; i < word.length; i++ )
        {
            ch = word.charAt( i );
            el = document.createElement( "div" );
            $(el).addClass( "letter" ).text( ch );
            letters.push( el );
        }
    }
    return letters;
}

//changing the style
function changeStyle(val){
unknown's avatar
unknown committed
233 234 235 236 237 238 239 240 241 242 243
    switch(val){
        case "1":
            $(".b_top_left").removeClass("btl_pad").removeClass("without_back");
            $(".b_top_center").removeClass("btc_pad").removeClass("without_back");
            $(".b_top_right").removeClass("btr_pad").removeClass("without_back");
            $(".b_center_left").removeClass("bcl_pad").removeClass("without_back");
            $(".b_center_right").removeClass("bcr_pad").removeClass("without_back");
            $(".b_bottom_right").removeClass("bbr_pad").removeClass("without_back");
            $(".b_bottom_left").removeClass("bbl_pad").removeClass("without_back");
            $(".b_bottom_center").removeClass("bbc_pad").removeClass("without_back");
            $("#wgt_reload").removeClass("pad_color").removeClass("pad_reload");
unknown's avatar
unknown committed
244
            $("#wgt_help").removeClass("pad_color").removeClass("pad_help");
unknown's avatar
unknown committed
245 246
            $("#wgt_edit").removeClass("pad_color").removeClass("pad_edit");
            $("#wgt_name").removeClass("pad_color");
maximXbs's avatar
maximXbs committed
247 248 249
            $("#wgt_display").addClass("display_wood");
            $("#style_select option:first").attr('selected',true);
            $("body, html").removeClass("without_radius").addClass("radius_ft");
unknown's avatar
unknown committed
250 251 252 253 254 255 256 257 258 259 260
            break;
        case "2":
            $(".b_top_left").addClass("btl_pad").removeClass("without_back");
            $(".b_top_center").addClass("btc_pad").removeClass("without_back");
            $(".b_top_right").addClass("btr_pad").removeClass("without_back");
            $(".b_center_left").addClass("bcl_pad").removeClass("without_back");
            $(".b_center_right").addClass("bcr_pad").removeClass("without_back");
            $(".b_bottom_right").addClass("bbr_pad").removeClass("without_back");
            $(".b_bottom_left").addClass("bbl_pad").removeClass("without_back");
            $(".b_bottom_center").addClass("bbc_pad").removeClass("without_back");
            $("#wgt_reload").addClass("pad_color").addClass("pad_reload");
unknown's avatar
unknown committed
261
            $("#wgt_help").addClass("pad_color").addClass("pad_help");
unknown's avatar
unknown committed
262 263
            $("#wgt_edit").addClass("pad_color").addClass("pad_edit");
            $("#wgt_name").addClass("pad_color");
maximXbs's avatar
maximXbs committed
264 265 266
            $("#wgt_display").removeClass("display_wood");
            $("#style_select option:first").next().attr('selected',true);
            $("body, html").removeClass("without_radius").removeClass("radius_ft");
unknown's avatar
unknown committed
267 268 269 270 271 272 273 274 275 276
            break;
        case "3":
            $(".b_top_left").addClass("without_back").removeClass("btl_pad");
            $(".b_top_center").addClass("without_back").removeClass("btc_pad");
            $(".b_top_right").addClass("without_back").removeClass("btr_pad");
            $(".b_center_left").addClass("without_back").removeClass("bcl_pad");
            $(".b_center_right").addClass("without_back").removeClass("bcr_pad");
            $(".b_bottom_right").addClass("without_back").removeClass("bbr_pad");
            $(".b_bottom_left").addClass("without_back").removeClass("bbl_pad");
            $(".b_bottom_center").addClass("without_back").removeClass("bbc_pad");
unknown's avatar
unknown committed
277
            $("#wgt_help").addClass("pad_color").addClass("pad_help");
unknown's avatar
unknown committed
278 279 280
            $("#wgt_reload").addClass("pad_color").addClass("pad_reload");
            $("#wgt_edit").addClass("pad_color").addClass("pad_edit");
            $("#wgt_name").addClass("pad_color");
maximXbs's avatar
maximXbs committed
281 282 283
            $("#wgt_display").removeClass("display_wood");
            $("#style_select option:last").attr('selected',true);
            $("body, html").addClass("without_radius").removeClass("radius_ft");
unknown's avatar
unknown committed
284
            break;
Claudio Valerio's avatar
Claudio Valerio committed
285 286 287 288 289 290 291 292 293 294 295
    }
}

/*
=====================
checkWord
=====================
scans the letters and checks 
if they are in the right order
*/
function checkWord()
unknown's avatar
unknown committed
296
{	
Claudio Valerio's avatar
Claudio Valerio committed
297 298 299 300 301 302 303 304 305
    var str = "";
    $( "#mp_word .letter" ).each( function(){
        str += $(this).text();
    });
    var w = word;
    while( w.indexOf( '*' ) != -1 )
    {
        w = w.replace( '*', '' );
    }
unknown's avatar
unknown committed
306
    //alert(str + " | " + w)
Claudio Valerio's avatar
Claudio Valerio committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    if( str == w ){
        $( "#mp_word .letter" ).addClass( "right" );
    //message( "Right!" );
    } else {
        $( "#mp_word .letter" ).removeClass( "right" );
    }
}

/*
==============
shuffle
==============
shuffles an array
*/
function shuffle( arr )
{
    var pos, tmp;
	
    for( var i = 0; i < arr.length; i++ )
    {
        pos = Math.round( Math.random() * ( arr.length - 1 ) );
        tmp = arr[pos];
        arr[pos] = arr[i];
        arr[i] = tmp;
    }
    return arr;
}



/*
==================
modeView()
==================
turns the widget into the view mode
*/
function modeView()
{
    if( editMode ){
unknown's avatar
unknown committed
346
        word = $( "#mp_word .wgt_cont" ).val();
Claudio Valerio's avatar
Claudio Valerio committed
347
    }
unknown's avatar
unknown committed
348 349
    
    wgtState = false;
Claudio Valerio's avatar
Claudio Valerio committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	
    // clean the previous word
    $( "#mp_word" ).empty();
	
    // create new set of letters
    var letters;
    if(window.sankore && curWord && !editMode)
        letters = createWordLetters( curWord );
    else
        letters = shuffle( createWordLetters( word ) );
    
    for( i in letters ){
        $("#mp_word").append( letters[i] );
    }
	
    // in sankore api there would be a function to check 
    // the answer, so no update parameter would be needed
    if( !isSankore ){
        $( "#mp_word" ).sortable( {
            update: checkWord
        } );
    } else $( "#mp_word" ).sortable();

    // adjustWidth
    var totalLettersWidth = 0;
    for( i in letters ){
        var currentWidth = $( letters[i] ).outerWidth( true );
        totalLettersWidth += currentWidth;
    }
    totalLettersWidth += 1;

    var width = Math.max(
        totalLettersWidth,
        min_view_width
        );
	
    // shift the words to the right to center them
    if( width > totalLettersWidth ){
        $( "#mp_word" ).css( "margin-left", Math.round( (width - totalLettersWidth)/2 ) );
    }
    else{
        $( "#mp_word" ).css( "margin-left", 0 );
    }
	
unknown's avatar
unknown committed
394
    checkWord();
Claudio Valerio's avatar
Claudio Valerio committed
395 396 397 398 399 400 401 402 403 404
}

/*
================
modeEdit
================
*/
function modeEdit()
{
    editMode = true;
unknown's avatar
unknown committed
405
    wgtState = true;
unknown's avatar
unknown committed
406 407
    $( "#mp_word").sortable( "destroy" );
    $( "#mp_word").css( "margin-left", 0 ).empty().append('<textarea class="wgt_cont">'+word+'</textarea>');
Claudio Valerio's avatar
Claudio Valerio committed
408 409 410

}

unknown's avatar
unknown committed
411 412
if (window.widget) {
    window.widget.onleave = function(){
maximXbs's avatar
maximXbs committed
413
        sankore.setPreference("ord_words_style", $("#style_select").find("option:selected").val());
unknown's avatar
unknown committed
414
        if($( "#mp_word .wgt_cont" ).val())
Claudio Valerio's avatar
Claudio Valerio committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
        {
            modeView();
            var str = "";
            $( "#mp_word .letter" ).each( function(){
                str += $(this).text() + "*";
            });        
            str = str.substr(0, str.length - 1);        
            sankore.setPreference("currentOrdWords", str);           
            modeEdit();
        }
        else{
            str = "";
            $( "#mp_word .letter" ).each( function(){
                str += $(this).text() + "*";
            });        
            str = str.substr(0, str.length - 1);        
            sankore.setPreference("currentOrdWords", str);
        }
        sankore.setPreference("rightOrdWords", word);
    }
435
}