PageSourceSearch

https://www.fooddive.com/static/job_graph/dive-form-fields.js

js fooddive.com collected 2026-09-24 08:39:56 UTC 7,772 bytes, 186 lines download raw bytes

1(function ($) {
2  var StrictMode;
3  $.fn.diveformify = function (publication_name, options) {
4    var settings = $.extend({
5      'company_type_field': $(this).find('[id^="dive_company_type"]'),
6      'job_function_field': $(this).find('[id^="dive_job_function"]'),
7      'job_level_field': $(this).find('[id^="dive_job_level"]'),
8      //TODO remove dive_ prefix from these values
9      'initial_values': {
10        'dive_company_type': '',
11        'dive_job_function': '',
12        'dive_job_level': ''
13      },
14      'strict_mode': false, // Tells diveformify whether or not to accept invalid drop-down fields
15      'placeholder_text': {
16        'company_type': '',
17        'job_function': '',
18        'job_level': ''
19      },
20      //------------Legacy fields
21      //TODO think about reorienting the settings on the fields
22      //You generally access properties based on each field, not the property at large
23      'compnay_type':{
24        "selector": '[id^="dive_company_type"]',
25        'initial': '',
26        'placeholder': ''
27       },
28      /// ETC
29      'missing_company_type_text': 'Please select a company type first',
30      'auto_unhide': true
31    }, options);
32    StrictMode = settings.strict_mode;
33    var pub_demographics = get_publication_demographics.call(dive_demographics_job_graph, publication_name);
34    // TODO: work on each form that has company Type
35    $(this).find(settings.company_type_field).parents('form').each(function(index, element) {
36      var $this = $(this);
37      var $company_type = $this.find(settings.company_type_field);
38      var $job_function = $this.find(settings.job_function_field);
39      var $job_level = $this.find(settings.job_level_field);
40
41      $company_type.change(function changeHandler(event) {
42        var company_type_demographic = find_in_array(pub_demographics, 'company_type', $company_type.val());
43        if (company_type_demographic) {
44          // TODO: preserve values
45          update_select_options($job_function, company_type_demographic.job_functions, settings.placeholder_text.job_function);
46          update_select_options($job_level, company_type_demographic.job_levels, settings.placeholder_text.job_level);
47          removePlaceholder($company_type, settings.placeholder_text.company_type);
48        }
49      });
50
51      $job_function.change(function (event) {
52        removePlaceholder($job_function, settings.placeholder_text.job_function);
53      });
54
55      $job_level.change(function (event) {
56        removePlaceholder($job_level, settings.placeholder_text.job_level);
57      });
58      set_initial_form_values($company_type, $job_function, $job_level);
59      if (settings.auto_unhide) {
60        // TODO: this assumes fields start hidden and that we want to unhide everything
61        //  above them.
62        $company_type.show().parents(":hidden").show();
63        $job_function.show().parents(":hidden").show();
64        $job_level.show().parents(":hidden").show();
65      }
66
67    });
68    return this;  // to allow for jQuery chaining
69
70    function removePlaceholder($selector){
71      if ($selector.val() !== '') {
72          $selector.find("option[value='']").remove();
73        }
74    }
75    function get_publication_demographics(publication_name) {
76      var matched_publication;
77      if (!publication_name) {
78        throw "diveformify was called without a publication_name";
79      }
80
81      matched_publication = find_in_array(this, 'publication_name', publication_name);
82      if ((!matched_publication) && (publication_name.indexOf(':') >
82 0)) {
83        // Coudn't find an exact match; try again after stripping everything after the first ':'
84        matched_publication = find_in_array(this, 'publication_name', publication_name.split(':')[0]);
85      }
86      if (!matched_publication) {
87        throw "Publication named '" + publication_name + "' couldn't be found in job graph";
88      }
89      return matched_publication.demographics;
90    }
91
92    // utility functions
93    function find_in_array(arr, key, val) {
94      // Find array element which has a key value of val -- case insensitive!
95      for (var ai, i = arr.length; i--;)
96        if ((ai = arr[i]) && ai[key].toLowerCase() == val.toLowerCase())
97          return ai;
98      return null;
99    }
100
101    function set_select_value(select, value_being_set) {
102      // creates the option if it doesn't exist and includes special logic about where to order
103      // the new element.
104      var $sel = $(select);
105      var select_options = optionsFromSelect($sel);
106      // If we're in "strict" mode, don't append a non-existing value to the drop-down list
107      // Otherwise, append the value if it doesn't exist
108      // The above comment means mismatched values (see test comments) not non-existing
109      // code currently accpets garbage in non-Strict mode
110      if (!StrictMode && !(select_options.includes(value_being_set))) {
111        // If the option doesn't already exist, we need to add it.
112        var $other_option = $sel.find("option[value='Other']:last");
113        var $new_option = $("<option/>").attr("value", value_being_set).text(value_being_set);
114        if ($other_option.length === 1) {
115          // Put it before "Other" option if there is one
116          $other_option.before($new_option);
117        } else {
118          // Otherwise put it at end of the list
119          $sel.append($new_option);
120        }
121      }
122      // now we know the option exists so we can just select it
123      if(strictlyAvailable(value_being_set, select_options)){
124        $sel.val(value_being_set);
125        $sel.change();
126      }
127      return $sel
128    }
129
130    function set_initial_form_values($company_type, $job_function, $job_level) {
131      update_select_options(
132        $company_type,
133        $.map(pub_demographics, function(demographic){ return demographic.company_type}),
134        settings.placeholder_text.company_type);
135
136        set_select_value($company_type, settings.initial_values.dive_company_type);
137        set_select_value($job_function, settings.initial_values.dive_job_function);
138        set_select_value($job_level, settings.initial_values.dive_job_level);
139
140      if ($company_type.val() === '') {
141        if (StrictMode) {
142          $job_function.append($("<option disabled selected value=''>").text(settings.missing_company_type_text));
143          $job_level.append($("<option disabled selected value=''>").text(settings.missing_company_type_text));
144        }
145        $job_function.prop("disabled", true);
146        $job_level.prop("disabled", true);
147      }
148    }
149
150    function valueIfNull(val, target){
151     return  target === undefined ? val : target;
152    }
153    function update_select_options(select, values, placeholder) {
154      var $select = $(select);
155
156      // save currently selected value (if any)
157      var original_value = $select.val();
158
159      // remove all options
160      $select.find("option").remove();
161
162      if (!original_value || !strictlyAvailable(original_value, values)) {
163        $select.append($("<option selected value=''>"+placeholder+"</option>"));
164        $select.addClass('default');
165      }
166
167      // add in new options from values array
168      for (var i = 0; i < values.length; i++) {
169        $select.append($("<option>").attr("value", values[i]).text(values[i]));
170      }
171
172      // Add 'original_value' back to the select field if it's not there after update
173      if (original_value) {
174        set_select_value($select, original_value);
175      }
176
177      $select.prop("disabled", false);  // enable field in case it was previously disabled
178    }
179    function optionsFromSelect($select_object){
180      return $.map($select_object.find('option'), function(x){ return x.value});
181    }
182    function strictlyAvailable(target, values){
183      return StrictMode && values.includes(target) || !StrictMode;
184    }
185  };
186}(jQuery));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.