var Gallery = function() { var instance = this; this.stages = {}; this.initStatus = {}; this.storage = Storage; $('.backBtn').click(function() { Statistics.trigger(19, (new Gallery()).getCurrentStage()); (new Gallery()).backTo($(this).attr('_backTo'), $(this).attr('_backWhole') == 'true'); }); $('.closeBtn').click(function() { Statistics.trigger(35, (new Gallery()).getCurrentStage()); window.close(); }); this.currentHeader = $('[_header]', $('.wrap')); this.newHeader = this.currentHeader.clone(true, true); this.mask = $('#mask'); Gallery = function() { return instance; }; }; Gallery.prototype.addStage = function(name, config) { this.stages[name] = config; this.initStatus[name] = false; if (this.stages[name].deamon) { this.stages[name].deamon(); } return this; }; Gallery.prototype.prepare = function(name, context) { if (this.initStatus[name] === false) { if (this.stages[name].init !== undefined) { this.stages[name].init(); } this.initStatus[name] = true; } if (this.stages[name].prepare !== undefined) { this.stages[name].prepare(context); } }; Gallery.prototype.moveTo = function(name, whole, context, direction) { var directionFlag = (direction === '-' ? 'Go' : 'Back'), header, oldHeader, oldStageName = this.currentStage; if (arguments[1] === undefined) { whole = false; } if (whole) { this.newHeader.css('left', '').addClass('prepareTo' + directionFlag).show(); oldHeader = this.currentHeader; oldHeader.css('left', '').addClass('readyTo' + directionFlag); this.newHeader.prependTo('.wrap'); this.currentHeader = this.newHeader; this.newHeader = oldHeader; } this.fillHeader(this.currentHeader, name); $('.main[_stage=' + this.currentStage + ']').addClass('readyTo' + directionFlag); $('.main[_stage=' + name + ']').addClass('prepareTo' + directionFlag).show(); $('#cover').show(); $('.readyTo' + directionFlag + ',.prepareTo' + directionFlag).animate( {left: direction + '=340px'}, 'slow', '', function() { var newStage = (new Gallery()).getStages()[name]; oldStage = (new Gallery()).getStages()[oldStageName]; if ($(this).hasClass('readyTo' + directionFlag)) { $(this).removeClass('readyTo' + directionFlag).hide(); } if ($(this).hasClass('prepareTo' + directionFlag)) { $(this).removeClass('prepareTo' + directionFlag); } $(this).css('left', ''); if ($(this).hasClass(name)) { $('#cover').hide(); } //切换到之后执行 if ($(this).hasClass(name) && newStage.showed !== undefined) { newStage.showed(context); }; //送走的场景 if ($(this).hasClass(oldStageName) && oldStage.gone !== undefined) { oldStage.gone(context); } } ); this.prepare(name, context); this.currentStage = name; if (!this.stages[this.currentStage].temp) { this.storage.set('lastStage', this.currentStage); } return this; }; Gallery.prototype.goTo = function(name, whole, context) { return this.moveTo(name, whole, context, '-'); } Gallery.prototype.backTo = function(name, whole, context) { return this.moveTo(name, whole, context, '+'); } Gallery.prototype.initShow = function(name, context) { this.prepare(name, context); this.fillHeader($('[_header]'), name); $('.main[_stage!=' + name + ']').hide(); $('.main[_stage=' + name + ']').show(); this.currentStage = name; if (this.stages[name].showed !== undefined) { this.stages[name].showed(context); }; }; Gallery.prototype.fillHeader = function(header, name) { Filler.fill( header, this.stages[name].header, { prevStage: function(data, element) { if (data.prevStage === undefined) { element.hide(); } else { element.show(); } return data.prevStage; }, backWhole: function(data) { if (data.backWhole === undefined || data.backWhole === false) { return 'false'; } return 'true'; } } ); }; Gallery.prototype.getStages = function() { return this.stages; }; Gallery.prototype.getCurrentStage = function() { return this.currentStage; }; Gallery.prototype.showMask = function() { this.mask.show(); return this; }; Gallery.prototype.hideMask = function() { this.mask.hide(); return this; };