aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/fiveui/injected/jquery-plugins.js
blob: 06c4b4ab7a140283b66cc35366da5a65884cf780 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Module     : injected/jquery-plugins.js
 * Copyright  : (c) 2011-2012, Galois, Inc.
 *
 * Maintainer :
 * Stability  : Provisional
 * Portability: Portable
 *
 * 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.
 */

if (typeof goog != 'undefined') {
  goog.provide('fiveui.jqueryPlugins');
}

/**
 * This module provides several useful jQuery plugins related to checking and reporting
 * UI consistency issues.
 *
 * @namespace
 */
fiveui.jqueryPlugins = fiveui.jqueryPlugins || {};

/**
 * Provide a short alias for fiveui.query along the lines of the jQuery $ alias.
 *
 * <p><pre>
 * EXAMPLES:
 *
 *   $5("p").hasText("foo") -> jQuery object containing paragraph elements
 *   containing the text "foo"
 * </pre></p>
 *
 * @const
 *
 */
var $5 = fiveui.query;

/**
 * Simple proof of concept plugin
 *
 * @return {!object} A modified jQuery object
 */
fiveui.jqueryPlugins.myPlugin = function () {
  return this.css("border-style", "solid").css("border-color", "red");
}

/**
 * Wrapper for the :contains('text') selector
 *
 * @param {!string} text Text to select for
 * @return {!object} A modified jQuery object
 */
fiveui.jqueryPlugins.hasText = function (text) {
  return this.filter(":contains('" + text + "')")
}

/**
 * Color checker plugin: filters for elements whose CSS color property is
 * not in the given set.
 *
 * Note: This is a special case of fiveui.jqueryPlugins.cssIsNot, i.e.
 *       $(..).notColorSet(set) == $(..).cssIsNot("color", set, fiveui.color.colorToHex)
 *       @see {fiveui.color.colorToHex}
 *
 * @param {Array.<string>} cset A set of allowable color strings
 * @return {!object} A modified jQuery object
 */
fiveui.jqueryPlugins.notColorSet = function (cset) {
  var allowable = {};
  for (var i = 0; i < cset.length; i += 1) { allowable[cset[i]] = true; } // array -> object
  return this.filter(function (index) {
    var color = fiveui.color.colorToHex($(this).css("color")); // .css("color") returns rgb(...)
    return !(color in allowable);
  });
}

/**
 * General CSS propetry checker plugin
 *
 * This plugin filters for elements whose CSS property `prop` is not a member
 * of the given array `cset`. The values checked are transformed using the
 * optional given function `fn`. This may be used to normalize values that the
 * browser returns so they can be compared to values in `cset`.
 *
 * @param {string} prop  CSS property selector
 * @param {string, Array.<string>} set allowable values (either a string or an array of strings)
 * @param {function(string):string=} fn Function to apply to return values of $(this).css(prop), fn defaults to the identity function.
 * @return{Object} jQuery object
 */
fiveui.jqueryPlugins.cssIsNot = function (prop, set, fn) {
  var allowable = {};
  fn = fn || function (x) { return x; }; // default is Id
  if (typeof set === "string") {
    allowable[fn(set)] = true;
  }
  else { // assume `set` is an array of strings
   for (var i = 0; i < set.length; i += 1) { allowable[fn(set[i])] = true; } // array -> object
  }
  return this.filter(function (index) {
    var cssProp = fn($(this).css(prop));
    return !(cssProp in allowable);
  });
}

/**
 * Send a report to FiveUI reporting a problem with each element in the
 * jQuery object.
 *
 * @param{string} msg Message to report
 */
fiveui.jqueryPlugins.report = function (msg) {
  this.each(function (i, elt) {
    report(msg, elt); // NOTE: this doesn't work. report() is not in scope here!
  });
}

/**
 * Visually highlight elements in the jQuery object (mostly for debugging purposes).
 *
 * @param {?string=} hint Highlighted border color, defaults to "red"
 * @return {!Object} A modified jQuery object
 */
fiveui.jqueryPlugins.highlight = function (hint) {
  hint = hint || "red"; // Default is "red"
  return this.css("background-color", "rgba(255, 0, 0, 0.3)")
             .css("border-style", "solid")
             .css("border-color", hint);
}

/**
 * Returns a list of css properties that element in the jQuery
 * object have. This is useful for analysis of a given page when
 * writing guielines.
 *
 * @param {!string} prop CSS property to be inspected
 * @param {bool=} log Boolean which enables console logging of the result; default is `false`.
 * @return {Object.<string, number>} A frequence map { "property": frequency }
 */
fiveui.jqueryPlugins.propDist = function (prop, log) {
  var res = {};
  log = log || false;
  this.each(function (i, elt) {
    var p = $(elt).css(prop);
    if (p in res) {
      res[p] += 1;
    }
    else {
      res[p] = 1;
    }
  });
  if (log) {
    console.log("Property distribution:");
    for (var p in res) {
      console.log("  " + p + ": " + res[p]);
    }
  }
  return res;
}

/**
 * Register the plugins. This adds methods to the jQuery.fn namespace.
 */
fiveui.jqueryPlugins.init = function () {
  for (fn in fiveui.jqueryPlugins) {
    f = fiveui.jqueryPlugins[fn];
    if (jQuery.isFunction(f) && fn != "init") {
      jQuery.fn[fn] = fiveui.jqueryPlugins[fn];
    }
  }
}
fiveui.jqueryPlugins.init();