One day, my boss said we needed a brand new UI, but luckily, I was prepared for it.
I’ve complained before that writing settings, CG, and other pages using TyranoScript is hard to understand, and it’s difficult to manage the page hierarchy. It would be better to just write it in HTML. So today, let’s analyze how to create a new page using HTML.
We’ll start with an existing “non-script” page, such as the default Load page, which can be found by doing a global search for displayLoad in the kag.menu.js file.
The core of creating a new page is the kag.html function, which can be found in kag.js:
The parameters of the html function are:
File name
Data passed in
Callback function
Observing displayLoad, it can be seen that loading the load page requires passing the page name load, an object containing archived content, and a callback function with HTML file content as a parameter. There is a flaw here, as the $(html) passed into the callback is not simply a “str” but an object processed by jQuery.
In the html function, $.loadText is used to fetch the local html file using Ajax, while functions such as $.templates and tmpl.render come from the open-source rendering engine jsrender.
In the load.html, we can see the use of a template:
However, I don’t intend to use jsrender as a template engine because I also want to incorporate Vue and take advantage of MVVM conveniently.
So, we can rewrite a simpler html function:
The htmlPure function eliminates the template engine rendering and directly passes the html string to the callback function. (You can choose to keep jsrender, but in order to avoid conflicts with Vue’s template, you need to modify Vue’s delimiters)
After adding the htmlPure function, we also need to create a new tag for creating the new page, which is named showExtendPage:
This is the approach to using custom pages. After replacing the script file with an html file, I felt liberated as I no longer need to worry about script callback navigation and hierarchy clearing, just call the showExtendPage tag to fade the page in.