No Description

ons-if.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
  2. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  3. import _createClass from 'babel-runtime/helpers/createClass';
  4. import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
  5. import _inherits from 'babel-runtime/helpers/inherits';
  6. /*
  7. Copyright 2013-2015 ASIAL CORPORATION
  8. Licensed under the Apache License, Version 2.0 (the "License");
  9. you may not use this file except in compliance with the License.
  10. You may obtain a copy of the License at
  11. http://www.apache.org/licenses/LICENSE-2.0
  12. Unless required by applicable law or agreed to in writing, software
  13. distributed under the License is distributed on an "AS IS" BASIS,
  14. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. See the License for the specific language governing permissions and
  16. limitations under the License.
  17. */
  18. import onsElements from '../ons/elements';
  19. import orientation from '../ons/orientation';
  20. import platform from '../ons/platform';
  21. import BaseElement from './base/base-element';
  22. import contentReady from '../ons/content-ready';
  23. /**
  24. * @element ons-if
  25. * @category conditional
  26. * @tutorial vanilla/Reference/if
  27. * @description
  28. * [en]
  29. * Conditionally display content depending on the platform, device orientation or both.
  30. *
  31. * Sometimes it is useful to conditionally hide or show certain components based on platform. When running on iOS the `<ons-if>` element can be used to hide the `<ons-fab>` element.
  32. * [/en]
  33. * [ja][/ja]
  34. * @guide theming.html#cross-platform-styling-autostyling [en]Information about cross platform styling[/en][ja]Information about cross platform styling[/ja]
  35. * @example
  36. * <ons-page>
  37. * <ons-if orientation="landscape">
  38. * Landscape view!
  39. * </ons-if>
  40. * <ons-if platform="android">
  41. * This is Android.
  42. * </ons-if>
  43. * <ons-if platform="ios other">
  44. * This is not Android.
  45. * </ons-if>
  46. * </ons-page>
  47. */
  48. var IfElement = function (_BaseElement) {
  49. _inherits(IfElement, _BaseElement);
  50. /**
  51. * @attribute platform
  52. * @initonly
  53. * @type {string}
  54. * @description
  55. * [en]Space-separated platform names. Possible values are `"ios"`, `"android"`, `"windows"` and `"other"`.[/en]
  56. * [ja][/ja]
  57. */
  58. /**
  59. * @attribute orientation
  60. * @type {string}
  61. * @description
  62. * [en]Either `"portrait"` or `"landscape"`.[/en]
  63. * [ja]portraitもしくはlandscapeを指定します[/ja]
  64. */
  65. function IfElement() {
  66. _classCallCheck(this, IfElement);
  67. var _this = _possibleConstructorReturn(this, (IfElement.__proto__ || _Object$getPrototypeOf(IfElement)).call(this));
  68. contentReady(_this, function () {
  69. if (platform._getSelectedPlatform() !== null) {
  70. _this._platformUpdate();
  71. } else if (!_this._isAllowedPlatform()) {
  72. while (_this.childNodes[0]) {
  73. _this.childNodes[0].remove();
  74. }
  75. _this._platformUpdate();
  76. }
  77. });
  78. _this._onOrientationChange();
  79. return _this;
  80. }
  81. _createClass(IfElement, [{
  82. key: 'connectedCallback',
  83. value: function connectedCallback() {
  84. orientation.on('change', this._onOrientationChange.bind(this));
  85. }
  86. }, {
  87. key: 'attributeChangedCallback',
  88. value: function attributeChangedCallback(name) {
  89. if (name === 'orientation') {
  90. this._onOrientationChange();
  91. }
  92. }
  93. }, {
  94. key: 'disconnectedCallback',
  95. value: function disconnectedCallback() {
  96. orientation.off('change', this._onOrientationChange);
  97. }
  98. }, {
  99. key: '_platformUpdate',
  100. value: function _platformUpdate() {
  101. this.style.display = this._isAllowedPlatform() ? '' : 'none';
  102. }
  103. }, {
  104. key: '_isAllowedPlatform',
  105. value: function _isAllowedPlatform() {
  106. return !this.getAttribute('platform') || this.getAttribute('platform').split(/\s+/).indexOf(platform.getMobileOS()) >= 0;
  107. }
  108. }, {
  109. key: '_onOrientationChange',
  110. value: function _onOrientationChange() {
  111. if (this.hasAttribute('orientation') && this._isAllowedPlatform()) {
  112. var conditionalOrientation = this.getAttribute('orientation').toLowerCase();
  113. var currentOrientation = orientation.isPortrait() ? 'portrait' : 'landscape';
  114. this.style.display = conditionalOrientation === currentOrientation ? '' : 'none';
  115. }
  116. }
  117. }], [{
  118. key: 'observedAttributes',
  119. get: function get() {
  120. return ['orientation'];
  121. }
  122. }]);
  123. return IfElement;
  124. }(BaseElement);
  125. export default IfElement;
  126. onsElements.If = IfElement;
  127. customElements.define('ons-if', IfElement);