Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. // Provides control it.designfuture.qrcode.QRCode
  2. sap.ui.define([
  3. "sap/ui/core/Control",
  4. "./3rdparty/qrcode"
  5. ], function (Control, qrcode) {
  6. "use strict";
  7.  
  8. /**
  9. * Constructor for a new QRCode.
  10. *
  11. * @param {string} [sId] id for the new control, generated automatically if no id is given
  12. * @param {object} [mSettings] initial settings for the new control
  13. *
  14. * @class
  15. * QRCode Control to render a QR Code
  16. * @extends sap.ui.core.Control
  17. * @version 0.0.1
  18. *
  19. * @constructor
  20. * @public
  21. * @since 1.40
  22. * @name it.designfuture.qrcode.QRCode
  23. */
  24. var QRCodeControl = Control.extend("it.designfuture.qrcode.QRCode", {
  25.  
  26. __qrcode: undefined,
  27.  
  28. metadata : {
  29. library: 'it.designfuture.qrcode',
  30. properties: {
  31.  
  32. /**
  33. * QRCode's text
  34. */
  35. text : {type : "string", group : "Appearance", defaultValue : null},
  36.  
  37. /**
  38. * QRCode's width
  39. */
  40. width : {type : "int", group : "Appearance", defaultValue : 256},
  41.  
  42. /**
  43. * QRCode's height
  44. */
  45. height : {type : "int", group : "Appearance", defaultValue : 256},
  46.  
  47. /**
  48. * HTML color (black/#ffffff) of the dark part of the QRCode
  49. */
  50. colorDark : {type : "string", group : "Appearance", defaultValue : "#000000"},
  51.  
  52. /**
  53. * HTML color (white/#ffffff) of the dark part of the QRCode
  54. */
  55. colorLight : {type : "string", group : "Appearance", defaultValue : "#ffffff"},
  56.  
  57. /**
  58. * Error correction level 0/1/2/3
  59. */
  60. correctLevel : {type : "int", group : "Appearance", defaultValue : QRCode.CorrectLevel.H}
  61. },
  62. aggregations: {},
  63. events: {},
  64. },
  65.  
  66. init: function() {
  67. // Init all the things!
  68. },
  69.  
  70. onAfterRendering: function() {
  71. this.reDraw();
  72. },
  73.  
  74. //////////////////////////////////////////////
  75. // GETTER / SETTER
  76. //////////////////////////////////////////////
  77.  
  78. /*
  79. * Return the text of the QR Code
  80. * @public
  81. * @returns {string} Text of the QR Code
  82. */
  83. getText: function() {
  84. return this.getProperty("text");
  85. },
  86.  
  87. /*
  88. * Set a new text of the QR Code
  89. * @public
  90. * @param {string} sText - Text of the QR Code
  91. * @param {boolean} bSkipDraw - skip the redraw
  92. */
  93. setText: function(sText, bSkipDraw) {
  94. this.setProperty("text", sText, true);
  95. if( !bSkipDraw ) {
  96. this.reDraw();
  97. }
  98. },
  99.  
  100. /*
  101. * Return the width of the QR Code
  102. * @public
  103. * @returns {int} Width of the QR Code
  104. */
  105. getWidth: function() {
  106. return this.getProperty("width");
  107. },
  108.  
  109.  
  110. /*
  111. * Set the width of the QR Code
  112. * @public
  113. * @param {int} iWidth - Width of the QR Code
  114. * @param {boolean} bSkipDraw - skip the redraw
  115. */
  116. setWidth: function(iWidth, bSkipDraw) {
  117. if( isNaN(iWidth) ) {
  118. throw new Error("Width must be an intreger");
  119. }
  120. iWidth = parseInt(iWidth, 10);
  121. if( iWidth <= 0 ) {
  122. throw new Error("Value " + iWidth + " is not a valid width");
  123. }
  124.  
  125. this.setProperty("width", parseInt(iWidth, 10), true);
  126. if( !bSkipDraw ) {
  127. this.reDraw();
  128. }
  129. },
  130.  
  131.  
  132. /*
  133. * Return the height of the QR Code
  134. * @public
  135. * @returns {int} Height of the QR Code
  136. */
  137. getHeight: function() {
  138. return this.getProperty("height");
  139. },
  140.  
  141. /*
  142. * Set the height of the QR Code
  143. * @public
  144. * @param {int} iHeight - Height of the QR Code
  145. * @param {boolean} bSkipDraw - skip the redraw
  146. */
  147. setHeight: function(iHeight, bSkipDraw) {
  148. if( isNaN(iHeight) ) {
  149. throw new Error("Height must be an intreger");
  150. }
  151. iHeight = parseInt(iHeight, 10);
  152. if( iHeight <= 0 ) {
  153. throw new Error("Value " + iHeight + " is not a valid height");
  154. }
  155.  
  156.  
  157. this.setProperty("height", parseInt(iHeight, 10), true);
  158. if( !bSkipDraw ) {
  159. this.reDraw();
  160. }
  161. },
  162.  
  163. /*
  164. * Return the RGB dark color of the QR Code
  165. * @public
  166. * @returns {string} RGB dark color of the QR Code
  167. */
  168. getColorDark: function() {
  169. return this.getProperty("colorDark");
  170. },
  171.  
  172. /*
  173. * Set the RGB dark color of the QR Code
  174. * @public
  175. * @param {string} sColorDark - RGB dark color of the QR Code
  176. * @param {boolean} bSkipDraw - skip the redraw
  177. */
  178. setColorDark: function(sColorDark, bSkipDraw) {
  179. var isOk = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(sColorDark);
  180. if( !isOk ) {
  181. throw new Error("Value " + sColorDark + " is not a valid RGB format");
  182. }
  183.  
  184. this.setProperty("colorDark", sColorDark, true);
  185. if( !bSkipDraw ) {
  186. this.reDraw();
  187. }
  188. },
  189.  
  190. /*
  191. * Return the RGB light color of the QR Code
  192. * @public
  193. * @returns {string} RGB light color of the QR Code
  194. */
  195. getColorLight: function() {
  196. return this.getProperty("colorLight");
  197. },
  198.  
  199. /*
  200. * Set the RGB light color of the QR Code
  201. * @public
  202. * @param {string} sColorLight - RGB light color of the QR Code
  203. * @param {boolean} bSkipDraw - skip the redraw
  204. */
  205. setColorLight: function(sColorLight, bSkipDraw) {
  206. var isOk = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(sColorLight);
  207. if( !isOk ) {
  208. throw new Error("Value " + sColorLight + " is not a valid RGB format");
  209. }
  210.  
  211. this.setProperty("colorLight", sColorLight, true);
  212. if( !bSkipDraw ) {
  213. this.reDraw();
  214. }
  215. },
  216.  
  217. /*
  218. * Return the Error Correction Level of the QR Code
  219. * @public
  220. * @returns {int} Error Correction Level of the QR Code
  221. */
  222. getCorrectLevel: function() {
  223. return this.getProperty("correctLevel");
  224. },
  225.  
  226. /*
  227. * Set the Error Correction Level of the QR Code
  228. * @public
  229. * @param {int} iCorrectLevel - RGB light color of the QR Code
  230. * @param {boolean} bSkipDraw - skip the redraw
  231. */
  232. setCorrectLevel: function(iCorrectLevel, bSkipDraw) {
  233. if( isNaN(iCorrectLevel) ) {
  234. throw new Error("Value " + iCorrectLevel + " is not a valid integer for Error Correction Level");
  235. }
  236.  
  237. var correctLevel = parseInt(iCorrectLevel, 10);
  238. if( iCorrectLevel < 0 || iCorrectLevel > 3 ) {
  239. throw new Error("Value " + correctLevel + " is not a valid value for Error Correction Level (min 0, max 3)");
  240. }
  241.  
  242. this.setProperty("correctLevel", correctLevel, true);
  243. if( !bSkipDraw ) {
  244. this.reDraw();
  245. }
  246. },
  247.  
  248. //////////////////////////////////////////////
  249. // QRCODE METHODS
  250. //////////////////////////////////////////////
  251.  
  252. /*
  253. * Draw the QRCode
  254. * If the QR Code does not exist it creates a new one, if it already exist it just refresh it
  255. */
  256. reDraw: function() {
  257. var iCorrectLevel = this.getCorrectLevel() < 0 || this.getCorrectLevel() > 3 ? QRCode.CorrectLevel.H : this.getCorrectLevel();
  258. if( this.__qrcode ) {
  259. this.__qrcode._htOption.width = this.getWidth();
  260. this.__qrcode._htOption.height = this.getHeight();
  261. this.__qrcode._htOption.colorDark = this.getColorDark();
  262. this.__qrcode._htOption.colorLight = this.getColorLight();
  263. this.__qrcode._htOption.correctLevel = iCorrectLevel;
  264. if( this.getText() ) {
  265. this.__qrcode.makeCode( this.getText() );
  266. } else {
  267. this.__qrcode.clear();
  268. }
  269. } else {
  270. var oElement = this.getDomRef();
  271. if( oElement ) {
  272. this.__qrcode = new QRCode( this.getDomRef(), {
  273. text: this.getText(),
  274. width: this.getWidth(),
  275. height: this.getHeight(),
  276. colorDark: this.getColorDark(),
  277. colorLight: this.getColorLight(),
  278. correctLevel: iCorrectLevel
  279. });
  280. }
  281. }
  282. },
  283.  
  284. /*
  285. * Clear the QR Code
  286. */
  287. clear: function() {
  288. if( this.__qrcode ) {
  289. this.__qrcode.clear();
  290. }
  291. }
  292.  
  293. });
  294.  
  295.  
  296. /*
  297. * Override the exit method to free local resources and destroy
  298. * @public
  299. */
  300. QRCode.prototype.exit = function() {
  301. this.__qrcode.clear();
  302. this.__qrcode = undefined;
  303. };
  304.  
  305. return QRCodeControl;
  306.  
  307. }, /* bExport= */ true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement