main.js 7.22 KB
Newer Older
1 2 3 4 5
function onTemplateLoadedCallback() {

}

function editRow(app, index) {
Claudio Valerio's avatar
Claudio Valerio committed
6 7 8 9 10
    var parameters = app.parameters;
    var row = $("<div/>");
    row.append("<div class='label'><div>"+fr.njin.i18n.transformation.row.label(index+1)+"</div></div>");
    var cards = $("<div class='cards'/>");
    row.append(cards);
11
	
Claudio Valerio's avatar
Claudio Valerio committed
12 13
    var beforecontent = parameters.value("#"+index+"before");
    var aftercontent = parameters.value("#"+index+"after");
14
	
Claudio Valerio's avatar
Claudio Valerio committed
15 16
    var before = $("<div rel='before'><div class='switch'><label>"+fr.njin.i18n.transformation.label.usePicture+"<input type='checkbox' name='switch'></label></div><div class='card'><div class='text'><div>"+( beforecontent !== undefined ? beforecontent : "" )+"</div></div><div class='picture'><div></div><div class='dropzone'><div>"+fr.njin.i18n.transformation.label.drop+"</div></div></div></div><div class='label'>"+fr.njin.i18n.transformation.label.before+"</div></div>");
    var after = $("<div rel='after'><div class='switch'><label>"+fr.njin.i18n.transformation.label.usePicture+"<input type='checkbox' name='switch'></label></div><div class='card'><div class='text'><div>"+( aftercontent !== undefined ? aftercontent : "" )+"</div></div><div class='picture'><div></div><div class='dropzone'><div>"+fr.njin.i18n.transformation.label.drop+"</div></div></div></div><div class='label'>"+fr.njin.i18n.transformation.label.after+"</div></div>");
17
	
Claudio Valerio's avatar
Claudio Valerio committed
18 19
    makeEditable(app, before, index);
    makeEditable(app, after, index);
20
	
Claudio Valerio's avatar
Claudio Valerio committed
21 22
    cards.append(before);
    cards.append(after);
23
	
Claudio Valerio's avatar
Claudio Valerio committed
24
    return row;
25 26 27
}

function cards(app, index) {
Claudio Valerio's avatar
Claudio Valerio committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    var parameters = app.parameters;
    var beforecard = $("<div class='part before closed'/>");
    var aftercard = $("<div class='part after'/>");
	
    var beforecontent = parameters.value("#"+index+"before");
    var aftercontent = parameters.value("#"+index+"after");
	
    var before = $("<div rel='before'><div class='card'><div class='text'><div>"+( beforecontent !== undefined ? beforecontent : "" )+"</div></div><div class='picture'><div></div></div></div></div>");
    var after = $("<div rel='after'><div class='card'><div class='text'><div>"+( aftercontent !== undefined ? aftercontent : "" )+"</div></div><div class='picture'><div></div></div></div></div>");
	
    var usePicture = parameters.value("#UsePicture"+index+"before") === "true"
    || parameters.value("#UsePicture"+index+"before") == true;
	
    if(usePicture) {
        before.addClass("usePicture");	
        var f = $.parseJSON(parameters.value("#Picture"+index+"before"));
        if(f !== null) {
            var $img = $('<img src="" class="uploadPic" title="" alt="" />').attr(f);
            before.find(".picture>div:eq(0)").append($img);	
        }
    }
	
    usePicture = parameters.value("#UsePicture"+index+"after") === "true"
    || parameters.value("#UsePicture"+index+"after") == true;
	
    if(usePicture) {
        after.addClass("usePicture");
        var f = $.parseJSON(parameters.value("#Picture"+index+"after"));
        if(f !== null) {
            var $img = $('<img src="" class="uploadPic" title="" alt="" />').attr(f);
            after.find(".picture>div:eq(0)").append($img);
        }
    }
	
    beforecard.append(before);
    aftercard.append(after);
	
    return [beforecard, aftercard];
66 67 68
}

function makeEditable(app, row, index) {
Claudio Valerio's avatar
Claudio Valerio committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    var parameters = app.parameters;
    var editable = row.find(".text>div").eq(0);
    var key = index+editable.parent().parent().parent().attr('rel');	
    editable.get(0).contentEditable = true;
    editable.bind('blur keyup paste', function(){
        parameters.value("#"+key, $(this).html());
    });
	
    var checkbox = row.find("input[name='switch']");
	
    function setSwicth() {
        var val = parameters.value("#UsePicture"+key) === "true"
        || parameters.value("#UsePicture"+key) === true;
        if(val) {
            row.addClass("usePicture");
        }else {
            row.removeClass("usePicture");
        }
        checkbox.attr("checked", val);
    }
	
    checkbox.change(function() {
        parameters.value("#UsePicture"+key, $(this).is(':checked'));
        setSwicth();
    });
    setSwicth();
	
    var dropzone = row.find(".dropzone");
    var pictureHolder = dropzone.parent().find(">div:eq(0)");
98
					
Claudio Valerio's avatar
Claudio Valerio committed
99 100 101 102 103 104 105 106
    function setPicture() {
        var f = $.parseJSON(parameters.value("#Picture"+key));
        if(f !== null) {
            var $img = $('<img src="" class="uploadPic" title="" alt="" />').attr(f);
            pictureHolder.empty();
            pictureHolder.append($img);
        }
    }
107
					
Claudio Valerio's avatar
Claudio Valerio committed
108 109 110 111 112
    app.utils.droppable(dropzone, function(f) {
        parameters.value("#Picture"+key, JSON.stringify(f));
        setPicture();
    });
    setPicture();
113 114 115
}

function reloadApp(app) {
Claudio Valerio's avatar
Claudio Valerio committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    var number = app.parameters.value("number");
	
    var scene = $("#scene");
    scene.empty();
	
    if(app.onEdit) {
        var editcards = $("#edit-cards");
        editcards.empty();
        for(var i=0 ; i<number ; i++) {
            editcards.append(editRow(app, i));
        }
        if(window.sankore)
            window.sankore.enableDropOnWidget(app.onEdit);
        return;
    }
	
    var table = $("<div id='table'/>");
    scene.append(table);
	
    var before = $("<div class='deck before'><div></div></div>");
    var box = $("<div class='box'><div></div></div>");
    var after = $("<div class='deck after'><div></div></div>");
	
    if(number === 0)
        before.addClass("empty");
    after.addClass("empty");
	
    table.append(before);
    table.append(box);
    table.append(after);
	
    box.find(">div").css("z-index", number+1);				
	
    var beforeDeck = before.find(">div");
    var afterDeck = after.find(">div");
    var beforeLeftPosition = box.find(">div").position().left - beforeDeck.position().left + 10 + 40; //10px de padding dans la box + 40px d'ombre dans l'image
	
    function makeCard(i, cards) {
        beforeDeck.append(cards[0]);
        afterDeck.append(cards[1]);
        cards[1].css("left", -beforeLeftPosition+"px");				
        cards[1].css("z-index", i);				
158
		
Claudio Valerio's avatar
Claudio Valerio committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        cards[0].click(function(e) {
            var c = $(this);
            c.removeClass("closed");
            c.unbind(e);
            setTimeout(function(){
                c.animate({
                    left : beforeLeftPosition
                }, 1000, function(){			
                    setTimeout(function(){
                        cards[1].animate({
                            left: 0
                        }, 1000, function(){
                            after.removeClass("empty");
                        });
                    }, 1000);
                    c.remove();	
                });
                if(i === 0) {
                    before.addClass("empty");
                }
            }, 3000);
180
			
Claudio Valerio's avatar
Claudio Valerio committed
181 182
        });
    }
183
		
Claudio Valerio's avatar
Claudio Valerio committed
184 185 186
    for(var i=number ; i>0 ; i--) {
        makeCard(i-1, cards(app, i-1));
    }
187
	
Claudio Valerio's avatar
Claudio Valerio committed
188 189
    if(window.sankore)
        window.sankore.enableDropOnWidget(app.onEdit);
190 191 192
}

function reloadCallback(parameter) {
Claudio Valerio's avatar
Claudio Valerio committed
193 194
    if(parameter === undefined || parameter.key === "number" )
        reloadApp(this);
195 196 197
}

$(document).ready(function(){
Claudio Valerio's avatar
Claudio Valerio committed
198 199 200 201 202 203 204 205 206
    var callbacks = {
        onTemplatesLoaded: onTemplateLoadedCallback,
        onEdit: reloadApp,
        onView: reloadApp
    };
    init(reloadCallback, {
        toolbar: toolbarTemplate, 
        parameters: parametersTemplate
    }, callbacks);
207
});