Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - knockoutjs customBindingHandler #24

Open
jmvtrinidad opened this issue May 5, 2016 · 0 comments
Open

Feature - knockoutjs customBindingHandler #24

jmvtrinidad opened this issue May 5, 2016 · 0 comments

Comments

@jmvtrinidad
Copy link

This is a simple binding handlers for knockout js.

    ko.bindingHandlers['jsDiffLib'] = {
            update: function (element, valueAccessor) {
                function getHtml(table){
                    var elems = [];
                    $(table).find("tbody>tr>td").each(function (item, elem) {
                        if(elem.className === "equal"){
                            elems.push("<span>" + elem.innerHTML + "</span>");
                        }
                        if(elem.className === "insert"){
                            elems.push("<ins>" + elem.innerHTML + "</ins>");
                        }
                        if(elem.className === "delete"){
                            elems.push("<del>" + elem.innerHTML + "</del>");
                        }
                    });
                    elems.splice(0, 1);
                    return elems.join("<br/>");
                }

                var options = valueAccessor();

                var base = difflib.stringAsLines("\n" + options.old()),
                    newtxt = difflib.stringAsLines("\n" + options.new()),
                    sm = new difflib.SequenceMatcher(base, newtxt),
                    opcodes = sm.get_opcodes();

                var currentTable = diffview.buildView({
                    baseTextLines: base,
                    newTextLines: newtxt,
                    opcodes: opcodes,
                    baseTextName: "Base Text",
                    newTextName: "New Text",
                    contextSize: null,
                    viewType: 1
                });
                var html = getHtml(currentTable);

                $(element).html(html);
            }
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant