ColorPalette.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dijit.ColorPalette"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dijit.ColorPalette"] = true;
  8. dojo.provide("dijit.ColorPalette");
  9. dojo.require("dijit._Widget");
  10. dojo.require("dijit._Templated");
  11. dojo.require("dojo.colors");
  12. dojo.require("dojo.i18n");
  13. dojo.require("dojo.string");
  14. dojo.require("dijit._PaletteMixin");
  15. dojo.requireLocalization("dojo", "colors", null, "ROOT,ar,az,bg,ca,cs,da,de,el,es,fi,fr,he,hr,hu,it,ja,kk,ko,nb,nl,pl,pt,pt-pt,ro,ru,sk,sl,sv,th,tr,zh,zh-tw");
  16. dojo.declare("dijit.ColorPalette",
  17. [dijit._Widget, dijit._Templated, dijit._PaletteMixin],
  18. {
  19. // summary:
  20. // A keyboard accessible color-picking widget
  21. // description:
  22. // Grid showing various colors, so the user can pick a certain color.
  23. // Can be used standalone, or as a popup.
  24. //
  25. // example:
  26. // | <div dojoType="dijit.ColorPalette"></div>
  27. //
  28. // example:
  29. // | var picker = new dijit.ColorPalette({ },srcNode);
  30. // | picker.startup();
  31. // palette: [const] String
  32. // Size of grid, either "7x10" or "3x4".
  33. palette: "7x10",
  34. // _palettes: [protected] Map
  35. // This represents the value of the colors.
  36. // The first level is a hashmap of the different palettes available.
  37. // The next two dimensions represent the columns and rows of colors.
  38. _palettes: {
  39. "7x10": [["white", "seashell", "cornsilk", "lemonchiffon","lightyellow", "palegreen", "paleturquoise", "lightcyan", "lavender", "plum"],
  40. ["lightgray", "pink", "bisque", "moccasin", "khaki", "lightgreen", "lightseagreen", "lightskyblue", "cornflowerblue", "violet"],
  41. ["silver", "lightcoral", "sandybrown", "orange", "palegoldenrod", "chartreuse", "mediumturquoise", "skyblue", "mediumslateblue","orchid"],
  42. ["gray", "red", "orangered", "darkorange", "yellow", "limegreen", "darkseagreen", "royalblue", "slateblue", "mediumorchid"],
  43. ["dimgray", "crimson", "chocolate", "coral", "gold", "forestgreen", "seagreen", "blue", "blueviolet", "darkorchid"],
  44. ["darkslategray","firebrick","saddlebrown", "sienna", "olive", "green", "darkcyan", "mediumblue","darkslateblue", "darkmagenta" ],
  45. ["black", "darkred", "maroon", "brown", "darkolivegreen", "darkgreen", "midnightblue", "navy", "indigo", "purple"]],
  46. "3x4": [["white", "lime", "green", "blue"],
  47. ["silver", "yellow", "fuchsia", "navy"],
  48. ["gray", "red", "purple", "black"]]
  49. },
  50. // templateString: String
  51. // The template of this widget.
  52. templateString: dojo.cache("dijit", "templates/ColorPalette.html", "<div class=\"dijitInline dijitColorPalette\">\n\t<table class=\"dijitPaletteTable\" cellSpacing=\"0\" cellPadding=\"0\">\n\t\t<tbody dojoAttachPoint=\"gridNode\"></tbody>\n\t</table>\n</div>\n"),
  53. baseClass: "dijitColorPalette",
  54. buildRendering: function(){
  55. // Instantiate the template, which makes a skeleton into which we'll insert a bunch of
  56. // <img> nodes
  57. this.inherited(arguments);
  58. // Creates <img> nodes in each cell of the template.
  59. // Pass in "customized" dijit._Color constructor for specified palette and high-contrast vs. normal mode
  60. this._preparePalette(
  61. this._palettes[this.palette],
  62. dojo.i18n.getLocalization("dojo", "colors", this.lang),
  63. dojo.declare(dijit._Color, {
  64. hc: dojo.hasClass(dojo.body(), "dijit_a11y"),
  65. palette: this.palette
  66. })
  67. );
  68. }
  69. });
  70. dojo.declare("dijit._Color", dojo.Color, {
  71. // summary:
  72. // Object associated with each cell in a ColorPalette palette.
  73. // Implements dijit.Dye.
  74. // Template for each cell in normal (non-high-contrast mode). Each cell contains a wrapper
  75. // node for showing the border (called dijitPaletteImg for back-compat), and dijitColorPaletteSwatch
  76. // for showing the color.
  77. template:
  78. "<span class='dijitInline dijitPaletteImg'>" +
  79. "<img src='${blankGif}' alt='${alt}' class='dijitColorPaletteSwatch' style='background-color: ${color}'/>" +
  80. "</span>",
  81. // Template for each cell in high contrast mode. Each cell contains an image with the whole palette,
  82. // but scrolled and clipped to show the correct color only
  83. hcTemplate:
  84. "<span class='dijitInline dijitPaletteImg' style='position: relative; overflow: hidden; height: 12px; width: 14px;'>" +
  85. "<img src='${image}' alt='${alt}' style='position: absolute; left: ${left}px; top: ${top}px; ${size}'/>" +
  86. "</span>",
  87. // _imagePaths: [protected] Map
  88. // This is stores the path to the palette images used for high-contrast mode display
  89. _imagePaths: {
  90. "7x10": dojo.moduleUrl("dijit.themes", "a11y/colors7x10.png"),
  91. "3x4": dojo.moduleUrl("dijit.themes", "a11y/colors3x4.png")
  92. },
  93. constructor: function(/*String*/alias, /*Number*/ row, /*Number*/ col){
  94. this._alias = alias;
  95. this._row = row;
  96. this._col = col;
  97. this.setColor(dojo.Color.named[alias]);
  98. },
  99. getValue: function(){
  100. // summary:
  101. // Note that although dijit._Color is initialized with a value like "white" getValue() always
  102. // returns a hex value
  103. return this.toHex();
  104. },
  105. fillCell: function(/*DOMNode*/ cell, /*String*/ blankGif){
  106. var html = dojo.string.substitute(this.hc ? this.hcTemplate : this.template, {
  107. // substitution variables for normal mode
  108. color: this.toHex(),
  109. blankGif: blankGif,
  110. alt: this._alias,
  111. // variables used for high contrast mode
  112. image: this._imagePaths[this.palette].toString(),
  113. left: this._col * -20 - 5,
  114. top: this._row * -20 - 5,
  115. size: this.palette == "7x10" ? "height: 145px; width: 206px" : "height: 64px; width: 86px"
  116. });
  117. dojo.place(html, cell);
  118. }
  119. });
  120. }