Passed
Push — master ( 189f04...13283a )
by Alexander
03:44
created

testcase_actions.js ➔ toggleExpandArrow   C

Complexity

Conditions 9

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 10
rs 6.6666
c 0
b 0
f 0
cc 9
1
Nitrate.TestCases = {};
0 ignored issues
show
Bug introduced by
The variable Nitrate seems to be never declared. If this is a global, consider adding a /** global: Nitrate */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
2
Nitrate.TestCases.Clone = {};
3
4
(function() {
5
  var TestCases = window.Nitrate.TestCases || {};
6
7
  TestCases.CasesSelection = function(options) {
8
    this.selectedCasesIds = options.selectedCasesIds || [];
9
    this.selectAll = options.selectAll;
10
11
    if (Object.prototype.toString.call(this.selectedCasesIds) !== '[object Array]') {
12
      throw new TypeError('selectedCasesIds must an object of Array.');
13
    }
14
    if (typeof this.selectAll !== 'boolean') {
15
      throw new TypeError('selectAll must be a boolean value.');
16
    }
17
  };
18
19
  TestCases.CasesSelection.prototype.empty = function() {
20
    return this.selectedCasesIds.length === 0 && !this.selectAll;
21
  };
22
23
  window.Nitrate.TestCases = TestCases;
24
}());
25
26
27
Nitrate.TestCases.Clone.on_load = function() {
28
  jQ('.js-cancel-button').bind('click', function() {
29
    window.history.go('-1');
30
  });
31
32
};
33
34
35
/*
36
 * Used for expanding test case in test plan page specifically
37
 *
38
 * Arguments:
39
 * options.caseRowContainer: a jQuery object referring to the container of the
40
 *                           test case that is being expanded to show more
41
 *                           information.
42
 * options.expandPaneContainer: a jQuery object referring to the container of
43
 *                              the expanded pane showing test case detail
44
 *                              information.
45
 */
46
function toggleExpandArrow(options) {
47
  var container = options.caseRowContainer;
48
  var content_container = options.expandPaneContainer;
49
  var blind_icon = container.find('img.blind_icon');
50
  if (content_container.css('display') === 'none') {
51
    blind_icon.removeClass('collapse').addClass('expand').attr('src', '/static/images/t1.gif');
52
  } else {
53
    blind_icon.removeClass('expand').addClass('collapse').attr('src', '/static/images/t2.gif');
54
  }
55
}
56
57
58
function toggleTestCaseContents(template_type, container, content_container, object_pk, case_text_version, case_run_id, callback) {
59
  if (typeof container === 'string') {
60
    var container = jQ('#' + container)[0];
61
  }
62
63
  if(typeof content_container === 'string') {
64
    var content_container = jQ('#' + content_container)[0];
65
  }
66
67
  jQ(content_container).toggle();
68
69
  if (jQ('#id_loading_' + object_pk).length) {
70
    var url = Nitrate.http.URLConf.reverse({ name: 'case_details', arguments: {id: object_pk} });
0 ignored issues
show
Bug introduced by
The variable Nitrate seems to be never declared. If this is a global, consider adding a /** global: Nitrate */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
71
    var parameters = {
72
      template_type: template_type,
73
      case_text_version: case_text_version,
74
      case_run_id: case_run_id
75
    };
76
77
    jQ.ajax({
0 ignored issues
show
Bug introduced by
The variable jQ seems to be never declared. If this is a global, consider adding a /** global: jQ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
78
      'url': url,
79
      'data': parameters,
80
      'success': function (data, textStatus, jqXHR) {
0 ignored issues
show
Unused Code introduced by
The parameter textStatus is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter jqXHR is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
81
        jQ(content_container).html(data);
82
      },
83
      'error': function (jqXHR, textStatus, errorThrown) {
0 ignored issues
show
Unused Code introduced by
The parameter textStatus is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter jqXHR is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter errorThrown is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
84
        html_failure();
85
      },
86
      'complete': function (jqXHR, textStatus) {
0 ignored issues
show
Unused Code introduced by
The parameter textStatus is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
87
        callback(jqXHR);
88
      }
89
    });
90
  }
91
92
  toggleExpandArrow({ caseRowContainer: jQ(container), expandPaneContainer: jQ(content_container) });
93
}
94
95
function changeTestCaseStatus(plan_id, case_ids, new_value, container) {
96
    case_ids.forEach(function(element) {
97
        jsonRPC('TestCase.update', [element, {case_status: new_value}], function(data) {});
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
98
    });
99
100
101
    var template_type = 'case';
102
    if (container.attr('id') === 'reviewcases') {
103
        template_type = 'review_case';
104
    }
105
106
    var parameters = {
107
        'a': 'initial',
108
        'from_plan': plan_id,
109
        'template_type': template_type,
110
    };
111
112
    // todo: #run_case_count, #case_count, #review_case_count
113
    // are no longer updated
114
    constructPlanDetailsCasesZone(container, plan_id, parameters);
115
116
    Nitrate.TestPlans.Details.reopenTabHelper(jQ(container));
0 ignored issues
show
Bug introduced by
The variable Nitrate seems to be never declared. If this is a global, consider adding a /** global: Nitrate */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
117
}
118
119
function toggleAllCheckBoxes(element, container, name) {
120
  if (element.checked) {
121
    jQ('#' + container).parent().find('input[name="' + name + '"]').not(':disabled').attr('checked', true);
122
  } else {
123
    jQ('#' + container).parent().find('input[name="'+ name + '"]').not(':disabled').attr('checked', false);
124
  }
125
}
126
127
function toggleAllCases(element) {
128
  //If and only if both case length is 0, remove the lock.
129
  if (jQ('div[id^="id_loading_"].normal_cases').length === 0 && jQ('div[id^="id_loading_"].review_cases').length === 0){
130
    jQ(element).removeClass('locked');
131
  }
132
133
  if (jQ(element).is('.locked')) {
134
    return false;
135
  } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
136
    jQ(element).addClass('locked');
137
    if (jQ(element).is('.collapse-all')) {
138
      element.title = "Collapse all cases";
139
      blinddownAllCases(element);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
140
    } else {
141
      element.title = "Expand all cases";
142
      blindupAllCases(element);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
143
    }
144
  }
145
}
146
147
function blinddownAllCases(element) {
148
  jQ('img.expand').each(function(e) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
149
    fireEvent(this, 'click');
150
  });
151
  if (element) {
152
    jQ(element)
153
      .removeClass('collapse-all').addClass('expand-all')
154
      .attr('src', '/static/images/t2.gif');
155
  }
156
}
157
158
function blindupAllCases(element) {
159
  jQ('.collapse').each(function(e) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
160
    fireEvent(this, 'click');
161
  });
162
163
  if (element) {
164
    jQ(element)
165
      .removeClass('expand-all').addClass('collapse-all')
166
      .attr('src', '/static/images/t1.gif');
167
  }
168
}
169
170
/*
171
 * Serialize selected cases' Ids.
172
 *
173
 * This function inherits the ability from original definition named
174
 * serializeCaseFromInputList, except that it also collects whehter all cases
175
 * are also selected even through not all of cases are displayed.
176
 *
177
 * Return value is an object of dictionary holding two properties. `selectAll'
178
 * is a boolean value indicating whether user select all cases.
179
 * `selectedCasesIds' is an array containing all selected cases' Ids the
180
 * current loaded set of cases.
181
 *
182
 * Whatever user selects all cases, above two properties appears always with
183
 * proper value.
184
 */
185
function serializeCaseFromInputList2(table) {
186
  var selectAll = jQ(table).parent().find('.js-cases-select-all input[type="checkbox"]')[0].checked;
187
  var case_ids = [];
188
  var checkedCases = jQ(table).find('input[name="case"]:checked');
189
  checkedCases.each(function(i, checkedCase) {
190
    if (typeof checkedCase.value === 'string') {
191
      case_ids.push(checkedCase.value);
192
    }
193
  });
194
  var selectedCasesIds = case_ids;
195
196
  return new Nitrate.TestCases.CasesSelection({
0 ignored issues
show
Bug introduced by
The variable Nitrate seems to be never declared. If this is a global, consider adding a /** global: Nitrate */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
197
    selectedCasesIds: selectedCasesIds,
198
    selectAll: selectAll
199
  });
200
}
201
202
function serializeCaseFromInputList(table) {
203
  var case_ids = [];
204
  jQ(table).parent().find('input[name="case"]:checked').each(function(i) {
0 ignored issues
show
Unused Code introduced by
The parameter i is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
205
    if (typeof this.value === 'string') {
206
      case_ids.push(this.value);
207
    }
208
  });
209
  return case_ids;
210
}
211
212
/*
213
 * Serialize criterias for searching cases.
214
 *
215
 * Arguments:
216
 * - form: the form from which criterias are searched
217
 * - table: the table containing all loaded cases
218
 * - serialized: whether to serialize the form data. true is default, if not
219
 *   passed.
220
 * - exclude_cases: whether to exclude all cases while serializing. For
221
 *   instance, when filter cases, it's unnecessary to collect all selected
222
 *   cases' IDs, due to all filtered cases in the response should be selected
223
 *   by default. Default to true if not passed.
224
 */
225
function serialzeCaseForm(form, table, serialized, exclude_cases) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
226
  if (typeof serialized != 'boolean') {
227
    var serialized = true;
228
  }
229
  if (exclude_cases === undefined) {
230
    var exclude_cases = false;
231
  }
232
  var data;
233
  if (serialized) {
234
    data = Nitrate.Utils.formSerialize(form);
0 ignored issues
show
Bug introduced by
The variable Nitrate seems to be never declared. If this is a global, consider adding a /** global: Nitrate */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
235
  } else {
236
    data = jQ(form).serialize();
237
  }
238
239
  if (!exclude_cases) {
240
    data['case'] = serializeCaseFromInputList(table);
241
  }
242
  return data;
243
}
244
245
/*
246
 * New implementation of serialzeCaseForm to allow to choose whether the
247
 * TestCases' Ids are necessary to be serialized.
248
 *
249
 * Be default if no value is passed to exclude_cases, not exclude them.
250
 */
251
function serializeCaseForm2(form, table, serialized, exclude_cases) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
252
  if (typeof serialized != 'boolean') {
253
    var serialized = true;
254
  }
255
  var data;
256
  if (serialized) {
257
    data = Nitrate.Utils.formSerialize(form);
0 ignored issues
show
Bug introduced by
The variable Nitrate seems to be never declared. If this is a global, consider adding a /** global: Nitrate */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
258
  } else {
259
    data = jQ(form).serialize();
260
  }
261
  var _exclude = exclude_cases === undefined ? false : exclude_cases;
262
  if (!_exclude) {
263
    data['case'] = serializeCaseFromInputList(table);
264
  }
265
  return data;
266
}
267
268
function toggleDiv(link, divId) {
269
  var link = jQ(link);
270
  var div = jQ('#' + divId);
271
  var show = 'Show All';
272
  var hide = 'Hide All';
273
  div.toggle();
274
  var text = link.html();
275
  if (text !== show) {
276
    link.html(show);
277
  } else {
278
    link.html(hide);
279
  }
280
}
281