Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

autostyle.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Copyright 2013-2015 ASIAL CORPORATION
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import onsPlatform from './platform';
  14. import util from './util';
  15. var autoStyleEnabled = true;
  16. // Modifiers
  17. var modifiersMap = {
  18. 'quiet': 'material--flat',
  19. 'light': 'material--flat',
  20. 'outline': 'material--flat',
  21. 'cta': '',
  22. 'large--quiet': 'material--flat large',
  23. 'large--cta': 'large',
  24. 'noborder': '',
  25. 'tappable': ''
  26. };
  27. var platforms = {};
  28. platforms.android = function (element) {
  29. var elementName = element.tagName.toLowerCase();
  30. if (!util.hasModifier(element, 'material')) {
  31. var oldModifier = element.getAttribute('modifier') || '';
  32. var newModifier = oldModifier.trim().split(/\s+/).map(function (e) {
  33. return modifiersMap.hasOwnProperty(e) ? modifiersMap[e] : e;
  34. });
  35. newModifier.unshift('material');
  36. element.setAttribute('modifier', newModifier.join(' ').trim());
  37. }
  38. var elements = ['ons-alert-dialog-button', 'ons-toolbar-button', 'ons-back-button', 'ons-button', 'ons-list-item', 'ons-fab', 'ons-speed-dial', 'ons-speed-dial-item', 'ons-tab'];
  39. // Effects
  40. if (elements.indexOf(elementName) !== -1 && !element.hasAttribute('ripple') && !element.querySelector('ons-ripple')) {
  41. if (elementName === 'ons-list-item') {
  42. if (element.hasAttribute('tappable')) {
  43. element.setAttribute('ripple', '');
  44. element.removeAttribute('tappable');
  45. }
  46. } else {
  47. element.setAttribute('ripple', '');
  48. }
  49. }
  50. };
  51. platforms.ios = function (element) {
  52. // Modifiers
  53. if (util.removeModifier(element, 'material')) {
  54. if (util.removeModifier(element, 'material--flat')) {
  55. util.addModifier(element, util.removeModifier(element, 'large') ? 'large--quiet' : 'quiet');
  56. }
  57. if (!element.getAttribute('modifier')) {
  58. element.removeAttribute('modifier');
  59. }
  60. }
  61. // Effects
  62. if (element.hasAttribute('ripple')) {
  63. if (element.tagName.toLowerCase() === 'ons-list-item') {
  64. element.setAttribute('tappable', '');
  65. }
  66. element.removeAttribute('ripple');
  67. }
  68. };
  69. var unlocked = {
  70. android: true
  71. };
  72. var getPlatform = function getPlatform(element, force) {
  73. if (autoStyleEnabled && !element.hasAttribute('disable-auto-styling')) {
  74. var mobileOS = onsPlatform.getMobileOS();
  75. if (platforms.hasOwnProperty(mobileOS) && (unlocked.hasOwnProperty(mobileOS) || force)) {
  76. return mobileOS;
  77. }
  78. }
  79. return null;
  80. };
  81. var prepare = function prepare(element, force) {
  82. var p = getPlatform(element, force);
  83. p && platforms[p](element);
  84. };
  85. var mapModifier = function mapModifier(modifier, element, force) {
  86. if (getPlatform(element, force)) {
  87. return modifier.split(/\s+/).map(function (m) {
  88. return modifiersMap.hasOwnProperty(m) ? modifiersMap[m] : m;
  89. }).join(' ');
  90. }
  91. return modifier;
  92. };
  93. var restoreModifier = function restoreModifier(element) {
  94. if (getPlatform(element) === 'android') {
  95. var modifier = element.getAttribute('modifier') || '';
  96. var newModifier = mapModifier(modifier, element);
  97. if (!/(^|\s+)material($|\s+)/i.test(modifier)) {
  98. newModifier = 'material ' + newModifier;
  99. }
  100. if (newModifier !== modifier) {
  101. element.setAttribute('modifier', newModifier.trim());
  102. return true;
  103. }
  104. }
  105. return false;
  106. };
  107. export default {
  108. isEnabled: function isEnabled() {
  109. return autoStyleEnabled;
  110. },
  111. enable: function enable() {
  112. return autoStyleEnabled = true;
  113. },
  114. disable: function disable() {
  115. return autoStyleEnabled = false;
  116. },
  117. prepare: prepare,
  118. mapModifier: mapModifier,
  119. getPlatform: getPlatform,
  120. restoreModifier: restoreModifier
  121. };