/
ivanby
/
Compote
Обзор
Документация
Войти
/
ivanby
/
Compote
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
1
CI/CD
Аналитика
Безопасность
master
compote/src/props_code-setlist.js
120 строк
5 KB
Иван Быков
Обновление README, изменения в compote
07 июл 2026, 09:24
07 июл 2026, 09:24
85b2e12
Код
Авторство
О чём код?
/**@replace '$$_compotes_comp_name__setlist_template_name_' : '${compotes[comp_name].setlist.template_name}', '$$_compotes_comp_name__setlist_template_props_' : '${compotes[comp_name].setlist.template_props}', '$$_setlist_' : '${setlist}' */ // $this -- псевдомассив $this.$$_setlist_ = { template: '$$_compotes_comp_name__setlist_template_name_', template_props: $$_compotes_comp_name__setlist_template_props_ }; $this.length = 0; // эти помощники принимают пропсы, возвращают пропсы // т.е. эти методы для реального удаления и создания элементов списка // array стиль $this.splice = (idxs, cntrm, ...items) => this.setlist_.splice($this, idxs, cntrm, ...items); $this.slice = (idx, idxe) => this.setlist_.slice($this, idx, idxe); $this.push = (...items) => this.setlist_.push($this, ...items); $this.pop = () => this.setlist_.pop($this); $this.unshift = (...items) => this.setlist_.unshift($this, ...items); $this.shift = () => this.setlist_.shift($this); // особые (не array стиль) $this.remove = (idx, cnt) => this.setlist_.remove($this, idx, cnt); $this.clean = () => this.setlist_.remove($this, 0, $this.childs.length); $this.insert = (idx, ...items) => this.setlist_.insert($this, idx, ...items); // -- эту функцию следует перенести в this.setlist_ // особые (не array стиль) // эти методы имеют два режима работы -- с простым обменом пропсами, что быстрее // и с реальными удалением/вставкой из/в DOM элементов с последующим пересчетом // индексов -- за это отвечает последний параметр -- force // сильный режим может понадобится для эффектов анимации, например // обычный -- быстрее для сортирки $this.swap = (idx1, idx2, force) => { if( idx1 !== idx2 && idx1 < $this.length && idx2 < $this.length && idx1 >= 0 && idx2 >= 0 ) { if(force) { if(idx1 < idx2) { const itm1 = $this.remove(idx1); const itm2 = $this.remove(idx2-1); $this.insert(idx1, ...itm2); $this.insert(idx2, ...itm1); } else { const itm2 = $this.remove(idx2); const itm1 = $this.remove(idx1-1); $this.insert(idx2, ...itm1); $this.insert(idx1, ...itm2); } } else { const p1 = $this[idx1].props(); if(p1.$index !== undefined) p1.$index = idx2; const p2 = $this[idx2].props(); if(p2.$index !== undefined) p2.$index = idx1; $this[idx1].props(p2); $this[idx2].props(p1); } return $this.length; } }; $this.up = (idx, force) => $this.swap(idx, idx-1, force); $this.down = (idx, force) => $this.swap(idx, idx+1, force); $this.top = (idx, force) => $this.swap(idx, 0, force); $this.end = (idx, force) => $this.swap(idx, $this.length-1, force); // -- эту функцию следует перенести в this.setlist_ $this.lift = (idx, toIdx, force) => { if(force) return $this.insert(toIdx, ...$this.remove(idx)); if(idx > toIdx) for(let i = idx; i > toIdx; i--) $this.up(i); else if(idx < toIdx) for(let i = idx; i < toIdx; i++) $this.down(i); return $this.length; }; // -- эту функцию следует перенести в this.setlist_ // сортировка -- если условие тру -- делает перестановку $this.swap // слабый и сильный режим, модифицирует также $this.childs через $this.swap // не повторяет интерфейс родного метода массивов sort! тут нет -1, 0, 1, только тру и фолс // но сработает, если cmpFn вернет -1, 1, и не сработает, если 0 $this.sort = (cmpFn, force) => { const length = $this.length; if (typeof cmpFn !== 'function') { throw new TypeError('setlist component sort() method requires a compare function'); } for (let i = 0; i < length - 1; i++) { let mIdx = i; for (let j = i + 1; j < length; j++) { if ( cmpFn($this[j], $this[mIdx]) ) mIdx = j; } if (mIdx !== i) $this.swap(i, mIdx, force); } return $this; }; const $izba_props = $this.props; $this.props = prps => this.setlist_.props($this, prps, '$$_setlist_', $izba_props);