From f31fba3d75136db72351f06e98387433d0e63be1 Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 13 Sep 2022 19:20:54 +0300 Subject: [PATCH 1/6] Add data renderer anchor --- dataRender/anchor.js | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 dataRender/anchor.js diff --git a/dataRender/anchor.js b/dataRender/anchor.js new file mode 100644 index 0000000..58b7510 --- /dev/null +++ b/dataRender/anchor.js @@ -0,0 +1,77 @@ +/** + * @name anchor + * @summary Renders the column data as HTML anchor (`a` tag) + * @author [Fedonyuk Anton](http://ensostudio.ru) + * @requires DataTables 1.10+ + * + * @param {string} type The anchor type: 'link'(by default), 'phone' or 'email' + * @param {object|function} attributes The attributes of the anchor tag or the + * callback function returning the tag attributes, the callback syntax: + * `function (mixed data, object|array row[, object meta]): object` + * @param {string|null} innerText The inner text of the anchor tag or `null` to + * set text by column `data` (by default) + * @returns {string} + * + * @example + * // Display `...` + * $('#example').DataTable({ + * columnDefs: [{ + * targets: 1, + * render: $.fn.dataTable.render.anchor() + * }] + * }); + * + * @example + * // Display `...` + * $('#example').DataTable({ + * columnDefs: [{ + * targets: 2, + * render: $.fn.dataTable.render.anchor('email', {'class': 'link'}) + * }] + * }); + */ +jQuery.fn.dataTable.render.anchor = function ( + type = 'link', + attributes = {}, + innerText = null +) { + return function (data, type, row, meta) { + // restriction only for table display rendering + if (type !== 'display') { + return data; + } + + if (innerText === null) { + innerText = data; + } + + if (attributes typeof 'function') { + var tagAttributes = attributes(data, row, meta); + } else { + var tagAttributes = attributes; + } + + if (!tagAttributes.href) { + switch (type) { + case 'mail': + tagAttributes.href = 'mailto:' + data; + break; + case 'phone': + tagAttributes.href = 'tel:' + data.replace(/[^+\d]+/g, ''); + break; + case 'link': + case default: + try { + tagAttributes.href = new URL(data); + } catch (e) { + tagAttributes.href = data; + } + } + } + + var $a = jQuery(''); + $a.attr(tagAttributes).text(innerText === null ? data : innerText).get(0).outerText; + + return $a[0]; + }; +}; From 129757a12dd47afa0d9ce1892ca5233f014983c8 Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 13 Sep 2022 19:30:13 +0300 Subject: [PATCH 2/6] Update anchor.js --- dataRender/anchor.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dataRender/anchor.js b/dataRender/anchor.js index 58b7510..0d8af55 100644 --- a/dataRender/anchor.js +++ b/dataRender/anchor.js @@ -69,9 +69,9 @@ jQuery.fn.dataTable.render.anchor = function ( } } - var $a = jQuery(''); - $a.attr(tagAttributes).text(innerText === null ? data : innerText).get(0).outerText; + var $a = jQuery(''); + $a.attr(tagAttributes).text(innerText === null ? data : innerText); - return $a[0]; + return $a[0].outerText; }; }; From 2199ba46489ec40ab7a0cf04e7de863c165463b5 Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 13 Sep 2022 19:31:36 +0300 Subject: [PATCH 3/6] Update anchor.js --- dataRender/anchor.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dataRender/anchor.js b/dataRender/anchor.js index 0d8af55..a3659a9 100644 --- a/dataRender/anchor.js +++ b/dataRender/anchor.js @@ -69,9 +69,9 @@ jQuery.fn.dataTable.render.anchor = function ( } } - var $a = jQuery(''); - $a.attr(tagAttributes).text(innerText === null ? data : innerText); + var anchorEl = jQuery(''); + anchorEl.attr(tagAttributes).text(innerText === null ? data : innerText); - return $a[0].outerText; + return anchorEl[0].outerText; }; }; From 908306c271ee292efb5365f1837ecdd2752abc05 Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 13 Sep 2022 19:34:11 +0300 Subject: [PATCH 4/6] Update anchor.js --- dataRender/anchor.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/dataRender/anchor.js b/dataRender/anchor.js index a3659a9..42b7384 100644 --- a/dataRender/anchor.js +++ b/dataRender/anchor.js @@ -45,32 +45,28 @@ jQuery.fn.dataTable.render.anchor = function ( innerText = data; } - if (attributes typeof 'function') { - var tagAttributes = attributes(data, row, meta); - } else { - var tagAttributes = attributes; - } + var attributes = attributes typeof 'function' ? attributes(data, row, meta) : attributes; - if (!tagAttributes.href) { + if (!attributes.href) { switch (type) { case 'mail': - tagAttributes.href = 'mailto:' + data; + attributes.href = 'mailto:' + data; break; case 'phone': - tagAttributes.href = 'tel:' + data.replace(/[^+\d]+/g, ''); + attributes.href = 'tel:' + data.replace(/[^+\d]+/g, ''); break; case 'link': case default: try { - tagAttributes.href = new URL(data); + attributes.href = new URL(data); } catch (e) { - tagAttributes.href = data; + attributes.href = data; } } } var anchorEl = jQuery(''); - anchorEl.attr(tagAttributes).text(innerText === null ? data : innerText); + anchorEl.attr(attributes).text(innerText); return anchorEl[0].outerText; }; From b2eacaade15804ff2eca53ef9724d43dd62721d7 Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 13 Sep 2022 19:37:33 +0300 Subject: [PATCH 5/6] Update anchor.js --- dataRender/anchor.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/dataRender/anchor.js b/dataRender/anchor.js index 42b7384..b0c0ad5 100644 --- a/dataRender/anchor.js +++ b/dataRender/anchor.js @@ -45,29 +45,28 @@ jQuery.fn.dataTable.render.anchor = function ( innerText = data; } - var attributes = attributes typeof 'function' ? attributes(data, row, meta) : attributes; + var attrs = attributes typeof 'function' ? attributes(data, row, meta) : attributes; - if (!attributes.href) { + if (!attrs.href) { switch (type) { case 'mail': - attributes.href = 'mailto:' + data; + attrs.href = 'mailto:' + data; break; case 'phone': - attributes.href = 'tel:' + data.replace(/[^+\d]+/g, ''); + attrs.href = 'tel:' + data.replace(/[^+\d]+/g, ''); break; case 'link': case default: try { - attributes.href = new URL(data); + attrs.href = new URL(data); } catch (e) { - attributes.href = data; + attrs.href = data; } } } - var anchorEl = jQuery(''); - anchorEl.attr(attributes).text(innerText); + var anchorEl = jQuery(''); - return anchorEl[0].outerText; + return anchorEl.attr(attrs).text(innerText)[0].outerText; }; }; From b926f434401b7f7b7fc2926bbbfccd939dbaf195 Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 13 Sep 2022 22:49:43 +0300 Subject: [PATCH 6/6] Update anchor.js --- dataRender/anchor.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dataRender/anchor.js b/dataRender/anchor.js index b0c0ad5..8b43deb 100644 --- a/dataRender/anchor.js +++ b/dataRender/anchor.js @@ -7,7 +7,7 @@ * @param {string} type The anchor type: 'link'(by default), 'phone' or 'email' * @param {object|function} attributes The attributes of the anchor tag or the * callback function returning the tag attributes, the callback syntax: - * `function (mixed data, object|array row[, object meta]): object` + * `function (mixed data, object|array row, object meta): object` * @param {string|null} innerText The inner text of the anchor tag or `null` to * set text by column `data` (by default) * @returns {string} @@ -35,7 +35,7 @@ jQuery.fn.dataTable.render.anchor = function ( attributes = {}, innerText = null ) { - return function (data, type, row, meta) { + return function (data, type, row, meta = {}) { // restriction only for table display rendering if (type !== 'display') { return data;