Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ac_insert_row.js « js « assets - github.com/HuasoFoundries/phpPgAdmin6.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c90d192e7143a0efc61038fa60d0158fa71cba08 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/** globals jQuery */
var fkl_hasnext = false;
var fkl_hasprev = false;
jQuery.ppa = jQuery.ppa || {
  root: $('#root'),
};

/* hide the value list */
function hideAc() {
  jQuery.ppa.o = 0;
  let { fklist, fkbg } = jQuery.ppa || {};
  fklist.hide();
  fkbg.hide();
}
/* open/update the value list */
function autocomplete(event) {
  /* if pressing enter, fire a click on the selected line */
  if (event.keyCode == 13) {
    if (jQuery.ppa.i > 0) {
      jQuery.ppa.fklist.find('tr').eq(jQuery.ppa.i).click();
    }
    return false;
  } else if (event.keyCode == 38 || event.keyCode == 40) {
    /* ignoring 38:up and 40:down */
    return false;
  } else if (
    /* ignoring 9:tab, 37:left, 39:right, 16:shift, ctrl: 17, alt:18, 20:lockmaj */
    event.keyCode == 9 ||
    event.keyCode == 37 ||
    event.keyCode == 39 ||
    event.keyCode == 16 ||
    event.keyCode == 17 ||
    event.keyCode == 18 ||
    event.keyCode == 20
  ) {
    return true;
  } else if (event.keyCode == 27) {
    /* esc */
    hideAc();
  } else {
    /* request the list of possible values asynchronously */
    /* if we refresh because of a value update,
     * we reset back to offset 0 so we catch values
     * if list is smaller than 11 values */
    if (event.type == 'keyup') {
      jQuery.ppa.o = 0;
    }
    openlist(this);
  }

  return true;
}
/* enable/disable auto-complete feature */
function triggerAc(ac) {
  if (ac) {
    jQuery.ppa.attrs
      .bind('keyup.ac_action', autocomplete)
      .bind('focus.ac_action', autocomplete)
      .bind('keypress.ac_action', move)
      .addClass('ac_field');
  } else {
    jQuery.ppa.attrs.removeClass('ac_field').unbind('.ac_action');
  }
}

/* select the given index value and highlight it */
function selectVal(index) {
  if (index == jQuery.ppa.i) {
    return;
  }

  // we catch the header as well so it takes th index 0
  var trs = jQuery.ppa.fklist.find('tr');

  // change colors for unselected
  if (jQuery.ppa.i > 0) {
    trs.eq(jQuery.ppa.i).find('*').css({
      'background-color': '#fff',
      color: '',
    });
  }

  // change colors for newly selected
  trs.eq(index).find('*').css({
    'background-color': '#3d80df',
    color: '#fff',
  });

  jQuery.ppa.i = index;
}

function openlist(e) {
  var elt = jQuery(e);
  var attnum = elt.attr('id').match(/\d+/)[0];
  /* FIXME we only support the first FK constraint of the field */
  var conid = attrs['attr_' + attnum][0];

  var constr = constrs['constr_' + conid];

  // get the changed attribute position in the arrays
  for (i = 0; constr.pattnums[i] != attnum; i++) {}

  var datas = {
    fattpos: i,
    fvalue: e.value,
    database: database,
    'keys[]': constr.pattnums,
    'keynames[]': constr.pattnames,
    'fkeynames[]': constr.fattnames,
    f_table: constr.f_table,
    f_schema: constr.f_schema,
    offset: jQuery.ppa.o,
  };

  jQuery.ajax({
    url: subfolder + '/src/views/acinsert.php?server=' + server,
    type: 'post',
    data: datas,
    dataType: 'html',
    cache: false,
    contentType: 'application/x-www-form-urlencoded',
    success: function (ret) {
      jQuery.ppa.i = 0;
      jQuery.ppa.fkbg.show();

      jQuery.ppa.fklist
        .html(ret)
        .appendTo('#row_att_' + attnum)
        .css('width', elt.css('width'))
        .show();
      jQuery.ppa.numrow = jQuery.ppa.fklist.find('tr').length;
    },
  });
}

/* move the cursor down or up,
 * load available next/prev values if going out of bound */
function move(event) {
  /* selecting next value down.
   * if the list is closed, it will open next */
  if (event.keyCode == 40) {
    if (jQuery.ppa.fklist[0].style.display == 'block') {
      if (jQuery.ppa.i + 1 < jQuery.ppa.numrow) {
        selectVal(jQuery.ppa.i + 1);
      } else if (fkl_hasnext == true) {
        jQuery.ppa.o += 11;
        openlist(this);
      }
    } else {
      openlist(this);
    }
  } else if (event.keyCode == 38) {
    /* selecting prev value up */
    if (jQuery.ppa.i - 1 > 0) {
      selectVal(jQuery.ppa.i - 1);
    } else if (fkl_hasprev == true && jQuery.ppa.i == 1) {
      jQuery.ppa.o -= 11;
      openlist(this);
    } else {
      selectVal(jQuery.ppa.numrow - 1);
    }
  }
}

    event.keyCode == 37 ||
    event.keyCode == 39 ||
    event.keyCode == 16 ||
    event.keyCode == 17 ||
    event.keyCode == 18 ||
    event.keyCode == 20
  ) {
/* bind actions on values lines: hover for style change, click for select */

jQuery('tr.acline').on('mouseover', function () {
  selectVal(jQuery('table.ac_values tr').index(this));
});

jQuery('tr.acline').on('click', function () {
  var a = jQuery(this).find('td > a.fkval');

  for (i = 0; i < a.length; i++) {
    jQuery('input[name="values[' + a[i].name + ']"]').val(jQuery(a[i]).text());
  }
  hideAc();
});

jQuery(document).ready(function () {
  /* register some global value in the ppa namespace */
  jQuery.ppa = {
    fklist: jQuery('#fklist'),
    attrs: jQuery('input[id^=attr_]'), // select fields with FK
    fkbg: jQuery('#fkbg'),
    i: 0, // selected value indice
    o: 0, // offset when navigating prev/next
  };

  jQuery.ppa.fklist.on('click', '#fkprev', function () {
    jQuery.ppa.o -= 11;
    /* get the field that is the previous html elt from the #fklist
     * and trigger its focus to refresh the list AND actualy
     * focus back on the field */
    jQuery('#fklist').prev().focus();
  });

  jQuery.ppa.fklist.on('click', '#fknext', function () {
    jQuery.ppa.o += 11;
    /* get the field that is the previous html elt from the #fklist
     * and trigger its focus to refresh the list AND actualy
     * focus back on the field*/
    jQuery('#fklist').prev().focus();
  });

  /* close the list when clicking outside of it */
  jQuery.ppa.fkbg.click(function (e) {
    hideAc();
  });

  /* do not submit the form when selecting a value by pressing enter */
  jQuery.ppa.attrs.keydown(function (e) {
    if (e.keyCode == 13 && jQuery.ppa.fklist[0].style.display == 'block') {
      return false;
    }
  });

  /* enable/disable auto-complete according to the checkbox */
  triggerAc(
    jQuery('#no_ac').click(function () {
      triggerAc(this.checked);
    })[0].checked
  );
});