text: /*
  Copyright 2013-2015 ASIAL CORPORATION
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
     http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  
  */
  (function() {
    'use strict';
    var module = angular.module('onsen');
  
    module.factory('SplitView', function($compile, RevealSlidingMenuAnimator, $onsen, $onsGlobal) {
      var SPLIT_MODE = 0;
      var COLLAPSE_MODE = 1;
      var MAIN_PAGE_RATIO = 0.9;
  
      var SplitView = Class.extend({
  
        init: function(scope, element, attrs) {
          element.addClass('onsen-sliding-menu');
  
          this._element = element;
          this._scope = scope;
          this._attrs = attrs;
  
          this._mainPage = angular.element(element[0].querySelector('.onsen-split-view__main'));
          this._secondaryPage = angular.element(element[0].querySelector('.onsen-split-view__secondary'));
  
          this._max = this._mainPage[0].clientWidth * MAIN_PAGE_RATIO;
          this._mode = SPLIT_MODE;
          this._doorLock = new ons._DoorLock();
  
          this._doSplit = false;
          this._doCollapse = false;
  
          $onsGlobal.orientation.on('change', this._onResize.bind(this));
  
          this._animator = new RevealSlidingMenuAnimator();
  
          this._element.css('display', 'none');
  
          if (attrs.mainPage) {
            this.setMainPage(attrs.mainPage);
          }
  
          if (attrs.secondaryPage) {
            this.setSecondaryPage(attrs.secondaryPage);
          }
  
          var unlock = this._doorLock.lock();
  
          this._considerChangingCollapse();
          this._setSize();
  
          setTimeout(function() {
            this._element.css('display', 'block');
            unlock();
          }.bind(this), 1000 / 60 * 2);
  
          scope.$on('$destroy', this._destroy.bind(this));
  
          this._clearDerivingEvents = $onsen.deriveEvents(this, element[0], ['init', 'show', 'hide', 'destroy']);
        },
  
        /**
         * @param {String} templateHTML
         */
        _appendSecondPage: function(templateHTML) {
          var pageScope = this._scope.$new();
          var pageContent = $compile(templateHTML)(pageScope);
  
          this._secondaryPage.append(pageContent);
  
          if (this._currentSecondaryPageElement) {
            this._currentSecondaryPageElement.remove();
            this._currentSecondaryPageScope.$destroy();
          }
  
          this._currentSecondaryPageElement = pageContent;
          this._currentSecondaryPageScope = pageScope;
        },
  
        /**
         * @param {String} templateHTML
         */
        _appendMainPage: function(templateHTML) {
          var pageScope = this._scope.$new();
          var pageContent = $compile(templateHTML)(pageScope);
  
          this._mainPage.append(pageContent);
  
          if (this._currentPage) {
            this._currentPageScope.$destroy();
          }
  
          this._currentPage = pageContent;
          this._currentPageScope = pageScope;
          this._currentPage[0]._show();
        },
  
        /**
         * @param {String} page
         */
        setSecondaryPage: function(page) {
          if (page) {
            $onsen.getPageHTMLAsync(page).then(function(html) {
              this._appendSecondPage(angular.element(html.trim()));
            }.bind(this), function() {
              throw new Error('Page is not found: ' + page);
            });
          } else {
            throw new Error('cannot set undefined page');
          }
        },
  
        /**
         * @param {String} page
         */
        setMainPage: function(page) {
          if (page) {
            $onsen.getPageHTMLAsync(page).then(function(html) {
              this._appendMainPage(angular.element(html.trim()));
            }.bind(this), function() {
              throw new Error('Page is not found: ' + page);
            });
          } else {
            throw new Error('cannot set undefined page');
          }
        },
  
        _onResize: function() {
          var lastMode = this._mode;
  
          this._considerChangingCollapse();
  
          if (lastMode === COLLAPSE_MODE && this._mode === COLLAPSE_MODE) {
            this._animator.onResized({
              isOpened: false,
              width: '90%'
            });
          }
  
          this._max = this._mainPage[0].clientWidth * MAIN_PAGE_RATIO;
        },
  
        _considerChangingCollapse: function() {
          var should = this._shouldCollapse();
  
          if (should && this._mode !== COLLAPSE_MODE) {
            this._fireUpdateEvent();
            if (this._doSplit) {
              this._activateSplitMode();
            } else {
              this._activateCollapseMode();
            }
          } else if (!should && this._mode === COLLAPSE_MODE) {
            this._fireUpdateEvent();
            if (this._doCollapse) {
              this._activateCollapseMode();
            } else {
              this._activateSplitMode();
            }
          }
  
          this._doCollapse = this._doSplit = false;
        },
  
        update: function() {
          this._fireUpdateEvent();
  
          var should = this._shouldCollapse();
  
          if (this._doSplit) {
            this._activateSplitMode();
          } else if (this._doCollapse) {
            this._activateCollapseMode();
          } else if (should) {
            this._activateCollapseMode();
          } else if (!should) {
            this._activateSplitMode();
          }
  
          this._doSplit = this._doCollapse = false;
        },
  
        _getOrientation: function() {
          if ($onsGlobal.orientation.isPortrait()) {
            return 'portrait';
          } else {
            return 'landscape';
          }
        },
  
        getCurrentMode: function() {
          if (this._mode === COLLAPSE_MODE) {
            return 'collapse';
          } else {
            return 'split';
          }
        },
  
        _shouldCollapse: function() {
          var c = 'portrait';
          if (typeof this._attrs.collapse === 'string') {
            c = this._attrs.collapse.trim();
          }
  
          if (c == 'portrait') {
            return $onsGlobal.orientation.isPortrait();
          } else if (c == 'landscape') {
            return $onsGlobal.orientation.isLandscape();
          } else if (c.substr(0, 5) == 'width') {
            var num = c.split(' ')[1];
            if (num.indexOf('px') >= 0) {
              num = num.substr(0, num.length - 2);
            }
  
            var width = window.innerWidth;
  
            return isNumber(num) && width < num;
          } else {
            var mq = window.matchMedia(c);
            return mq.matches;
          }
        },
  
        _setSize: function() {
          if (this._mode === SPLIT_MODE) {
            if (!this._attrs.mainPageWidth) {
              this._attrs.mainPageWidth = '70';
            }
  
            var secondarySize = 100 - this._attrs.mainPageWidth.replace('%', '');
            this._secondaryPage.css({
              width: secondarySize + '%',
              opacity: 1
            });
  
            this._mainPage.css({
              width: this._attrs.mainPageWidth + '%'
            });
  
            this._mainPage.css('left', secondarySize + '%');
          }
        },
  
        _fireEvent: function(name) {
          this.emit(name, {
            splitView: this,
            width: window.innerWidth,
            orientation: this._getOrientation()
          });
        },
  
        _fireUpdateEvent: function() {
          var that = this;
  
          this.emit('update', {
            splitView: this,
            shouldCollapse: this._shouldCollapse(),
            currentMode: this.getCurrentMode(),
            split: function() {
              that._doSplit = true;
              that._doCollapse = false;
            },
            collapse: function() {
              that._doSplit = false;
              that._doCollapse = true;
            },
            width: window.innerWidth,
            orientation: this._getOrientation()
          });
        },
  
        _activateCollapseMode: function() {
          if (this._mode !== COLLAPSE_MODE) {
            this._fireEvent('precollapse');
            this._secondaryPage.attr('style', '');
            this._mainPage.attr('style', '');
  
            this._mode = COLLAPSE_MODE;
  
            this._animator.setup(
              this._element,
              this._mainPage,
              this._secondaryPage,
              {isRight: false, width: '90%'}
            );
  
            this._fireEvent('postcollapse');
          }
        },
  
        _activateSplitMode: function() {
          if (this._mode !== SPLIT_MODE) {
            this._fireEvent('presplit');
  
            this._animator.destroy();
  
            this._secondaryPage.attr('style', '');
            this._mainPage.attr('style', '');
  
            this._mode = SPLIT_MODE;
            this._setSize();
  
            this._fireEvent('postsplit');
          }
        },
  
        _destroy: function() {
          this.emit('destroy');
  
          this._clearDerivingEvents();
  
          this._element = null;
          this._scope = null;
        }
      });
  
      function isNumber(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
      }
  
      MicroEvent.mixin(SplitView);
  
      return SplitView;
    });
  })();
  