-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3012053
commit cfea152
Showing
24 changed files
with
129 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 0 additions & 75 deletions
75
packages/migrations/src/migrations/bootstrap/chip/index.ts
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { Rule } from '@angular-devkit/schematics'; | ||
import type { AnyNode, Cheerio, CheerioAPI } from 'cheerio'; | ||
import { DomUpdate, getDomMigrationRule } from '../../../utils/dom-migration'; | ||
import { themeColors } from '../../../utils/constants'; | ||
|
||
export default function (): Rule { | ||
return getDomMigrationRule( | ||
new BadgeCheckToChipFilterUpdate(), | ||
new BadgeToChipDismissibleUpdate(), | ||
new BadgeToTagUpdate(), | ||
); | ||
} | ||
|
||
class BadgeCheckToChipFilterUpdate implements DomUpdate { | ||
selector = '.badge-check'; | ||
|
||
update($elements: Cheerio<AnyNode>, $: CheerioAPI) { | ||
$elements.each((_i, element) => { | ||
const $element = $(element); | ||
|
||
$element.removeClass('badge-check').addClass('chip-filter'); | ||
|
||
const $input = $element.children('.badge-check-input'); | ||
if ($input) $input.removeClass('badge-check-input').addClass('chip-filter-input'); | ||
|
||
const $label = $element.children('.badge-check-label'); | ||
if ($label) $label.removeClass('badge-check-label').addClass('chip-filter-label'); | ||
|
||
addChipTextClass($label); | ||
}); | ||
} | ||
} | ||
|
||
class BadgeToChipDismissibleUpdate implements DomUpdate { | ||
selector = '.badge'; | ||
|
||
update($elements: Cheerio<AnyNode>, $: CheerioAPI) { | ||
$elements.each((_i, element) => { | ||
const $element = $(element); | ||
|
||
// only update badge with close button | ||
const $closeBtn = $element.children('.btn-close'); | ||
if (!$closeBtn.length) { | ||
return; | ||
} | ||
|
||
addChipTextClass($element); | ||
|
||
$closeBtn.removeAttr('class').addClass('chip chip-dismissible'); | ||
|
||
const ariaLabel = $closeBtn.attr('aria-label'); | ||
if (ariaLabel) { | ||
$closeBtn.removeAttr('aria-label'); | ||
$closeBtn.append(`<span class="visually-hidden">${ariaLabel}</span>`); | ||
} | ||
|
||
$closeBtn.append($element.children()).data($element.data()); | ||
|
||
$element.replaceWith($closeBtn); | ||
}); | ||
} | ||
} | ||
|
||
class BadgeToTagUpdate implements DomUpdate { | ||
selector = '.badge'; | ||
|
||
bgClassRegex: RegExp = new RegExp(`^bg-(${themeColors.join('|')})$`); | ||
|
||
update($elements: Cheerio<AnyNode>, $: CheerioAPI) { | ||
$elements.each((_i, element) => { | ||
const $element = $(element); | ||
|
||
// do not update nested badges | ||
const $parent = $element.parent(); | ||
if ($parent.hasClass('tag')) { | ||
return; | ||
} | ||
|
||
$element.removeClass('badge').addClass('tag'); | ||
|
||
if ($element.hasClass('badge-sm')) $element.removeClass('badge-sm').addClass('tag-sm'); | ||
|
||
$element | ||
.attr('class') | ||
?.split(' ') | ||
.forEach(cssClass => { | ||
const [_, bgColor] = cssClass.match(this.bgClassRegex) ?? []; | ||
|
||
if (!bgColor) return; | ||
|
||
if (bgColor === 'active' || bgColor === 'yellow') $element.addClass('tag-yellow'); | ||
if (bgColor === 'white') $element.addClass('tag-white'); | ||
if (bgColor === 'info') $element.addClass('tag-info'); | ||
if (bgColor === 'success') $element.addClass('tag-success'); | ||
if (bgColor === 'danger') $element.addClass('tag-danger'); | ||
if (bgColor === 'warning') $element.addClass('tag-warning'); | ||
|
||
$element.removeClass(cssClass); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
function addChipTextClass($element: Cheerio<AnyNode>) { | ||
$element.children('span:not(.badge)').addClass('chip-text'); | ||
} |