wcontainer.js 6.75 KB
Newer Older
unknown's avatar
unknown committed
1 2 3
var sankoreLang = {
    view: "Показать", 
    edit: "Изменить",
Claudio Valerio's avatar
Claudio Valerio committed
4
    example: "привет, это первое предложение. а это второе предложение. и снова привет, это третье предложение. добрый день, это четвертое предложение. привет, извини, но я последнее предложение."
unknown's avatar
unknown committed
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 30 31 32 33 34 35 36
};

// if use the "view/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" );

function wcontainer( containerID )
{
    // some protecred variables
    var thisInstance = this;
    this.editMode = false;
    var data = {}; // see setData and getData
	
    // widget size parameters
    this.minHeight = 100;
    this.minWidth = 400;
	
    // set to 0 for no max width restriction
    this.maxWidth = 0;
	
    // links to the elements of the widget
    this.elements = {};
	
    /*
	============
	create
	============
	- creates html base, inits this.elements, assings events
	*/
    this.create = function( containerID )
    {
Claudio Valerio's avatar
Claudio Valerio committed
37 38 39 40 41 42 43 44 45
        var html = 
        '<div id="mp_setup">' +
        '<div class="viewmode">' +
        '<button>' + sankoreLang.edit + '</button>' +
        '</div>' +
        '<div class="editmode">' +
        '<button>' + sankoreLang.view + '</button>' +
        '</div>' +
        '</div>' +
unknown's avatar
unknown committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        '<div id="mp_content">' +
        '<div class="viewmode" id="mp_view">' +
        '</div>' +
        '<div class="editmode" id="mp_edit">' +
        '</div>' +
        '</div>';
		
        var container = $( containerID );
		
        container.append( html );
        this.elements.edit = container.find( ".editmode" );
        this.elements.view = container.find( ".viewmode" );
        this.elements.container = container;
        this.elements.subcontainer = container.find( "#mp_content" );
        this.elements.containerView = this.elements.subcontainer.find( ".viewmode" );
        this.elements.containerEdit = this.elements.subcontainer.find( ".editmode" );
		
Claudio Valerio's avatar
Claudio Valerio committed
63
        container.find( ".viewmode button" ).click( function(){
unknown's avatar
unknown committed
64 65 66
            thisInstance.modeEdit();
        } );
		
Claudio Valerio's avatar
Claudio Valerio committed
67
        container.find( ".editmode button" ).click( function(){
unknown's avatar
unknown committed
68 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 98 99 100 101 102 103 104 105 106 107 108 109 110
            thisInstance.modeView();
        } );
    };
	
	
    /*
	===============
	setViewContent
	===============
	- assigns custom html to the viewmode container
	*/
    this.setViewContent = function( html )
    {
        this.elements.container.find( "#mp_content .viewmode" ).html( html );
    };
	
    /*
	===============
	setEditContent
	===============
	- assigns custom html to the editmode container
	*/
    this.setEditContent = function( html )
    {
        this.elements.container.find( "#mp_content .editmode" ).html( html );
    };
	
	
	
    /*
	=========================
	modeEdit and modeView
	=========================
	- switch the widget betweed modes
	* for customization extend onEditMode and onViewMode
	*/
    this.modeEdit = function()
    {
        this.onEditMode();
        this.editMode = true;
        this.elements.edit.removeClass( "hide" );
        this.elements.view.addClass( "hide" );
		
Claudio Valerio's avatar
Claudio Valerio committed
111
        this.adjustSize();
unknown's avatar
unknown committed
112 113 114 115 116 117 118 119
    };
    this.modeView = function()
    {
        this.onViewMode();
        this.editMode = false;
        this.elements.edit.addClass( "hide" );
        this.elements.view.removeClass( "hide" );
		
Claudio Valerio's avatar
Claudio Valerio committed
120
        this.adjustSize();
unknown's avatar
unknown committed
121 122 123
    };
	
	
Claudio Valerio's avatar
Claudio Valerio committed
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 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
	
    /*
	================
	adjustSize
	================
	- changes the widget size (window and container)
	*/
    this.adjustSize = function( width, height )
    {
        // retrieve the arguments
        if( arguments.length < 2 )
        {
            var s = ( this.editMode )? this.editSize() : this.viewSize();
            var width = s.w;
            var height = s.h;
        }
		
        // check for validity
        if( width + height == 0 )
            return;
		
        // add view/edit bar height
        if( !isSankore ){
            height += $( this.elements.container ).find( "#mp_setup" ).outerHeight();
        }
		
        // apply min and max restrictions
        width = Math.max( this.minWidth, width );
        height = Math.max( this.minHeight, height );
        if( this.maxWidth ){
            width = Math.min( width, this.maxWidth );
        }
		
        // if viewed as a widget, resize the window
        if( !isBrowser )
        {
            var dw = this.getData( "dw" );
            var dh = this.getData( "dh" );
			
            if( width == 0 ){
                width = widget.width;
            }
            if( height == 0 ){
                height = widget.height;
            }
            window.resizeTo( width + dw, height + dh );
        }
		
        // resize the container
        var params = {};
        if( width != 0 ){
            params.width = width;
        }
        if( height != 0 ){
            params.height = height;
        }
		
        this.elements.container.animate( params );
		
    };
	
unknown's avatar
unknown committed
185 186 187 188 189 190 191 192 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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    /*
	======================
	setData and getData
	======================
	- store some data inside
	*/
    this.setData = function( name, value ){
        data[name] = value;
    };
    this.getData = function( name ){
        if( typeof( data[name] ) == "undefined" ){
            return null;
        } else return data[name];
    };
	
	
    // redefinable methods
	
    /*
	==========================
	onEditMode and onViewMode
	==========================
	- these are called when the mode is being changed
	*/
    this.onEditMode = function(){
    //
    };
    this.onViewMode = function(){
    //
    };
	
    /*
	======================
	viewSize and editSize
	======================
	- calculate container size for the adjustSize method
	* they are likely to be redefined for each particular widget
	*/
    this.viewSize = function(){
        return {
            w: this.elements.containerView.outerWidth(),
            h: this.elements.containerView.outerHeight()
        };
    };
    this.editSize = function(){
        return {
            w: this.elements.containerEdit.outerWidth(),
            h: this.elements.containerEdit.outerHeight()
        };
    };
	
    /*
	=====================
	checkAnswer
	=====================
	- check if the exercise in the view mode was done right
	* redefine it for each particular widget
	*/
    this.checkAnswer = function()
    {
    //
    };
	
	
    // constructor end
	
    // if the constructor was called with a parameter,
    // call create() automatically
    if( arguments.length > 0 ){
        this.create( containerID );
    }
    this.setData( "dw", this.elements.container.outerWidth( true ) - this.elements.container.width() );
    this.setData( "dh", this.elements.container.outerHeight( true ) - this.elements.container.height() );
    window.winstance = thisInstance;
}