Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Suitelet hack to load a custom html page within a Netsuite Form. Created by Adolfo Garza (borncorp)
- */
- var ENCODEMODULE, RUNTIMEMODULE, UIMODULE, URLMODULE, DEPLOYMENT_URL; //I like loading my modules as globals so I can access them whenever I want
- /**
- *@NApiVersion 2.x
- *@NScriptType Suitelet
- *@NModuleScope Public
- */
- define(["N/encode", "N/runtime", 'N/ui/serverWidget', 'N/url'], runSuitelet);
- //********************** MAIN FUNCTION **********************
- function runSuitelet(encode, runtime, ui, url){
- ENCODEMODULE= encode;
- RUNTIMEMODULE= runtime;
- UIMODULE = ui;
- URLMODULE = url;
- var returnObj = {};
- returnObj.onRequest = execute;
- return returnObj;
- }
- function execute(context){
- DEPLOYMENT_URL = getDeploymentURL();
- try {
- if (context.request.method == 'GET') {
- var form = getInjectableForm();
- context.response.writePage(form);
- return;
- }
- return;
- } catch (e) {
- log.error("ERROR", e.toString());
- context.response.write(e.toString());
- }
- return;
- }
- function getInjectableForm(){
- //*********** Create Form ***********
- var form = UIMODULE.createForm({
- title: ' '
- });
- var bodyAreaField = form.addField({
- id : 'custpage_bodyareafield',
- type : UIMODULE.FieldType.INLINEHTML,
- label : 'Body Area Field'
- });
- //*********** Prepare HTML and scripts to Inject ***********
- var body = getBody();
- clientCode = clientCode.replace('$PAGEBODY$', body).replace('$DEPLOYMENT_URL$', DEPLOYMENT_URL);
- var base64ClientCode = toBase64(clientCode);
- var scriptToInject = 'console.log(\'Added bottom script element\');';
- scriptToInject += "eval( atob(\'" + base64ClientCode + "\') );";
- //*********** Injecting HTML and scripts into an the Body Area Field ***********
- bodyAreaField.defaultValue = '<script>var script = document.createElement(\'script\');script.setAttribute(\'type\', \'text/javascript\');script.appendChild(document.createTextNode(\"' + scriptToInject + '\" ));document.body.appendChild(script);</script>';
- return form;
- }
- /**
- * Gets HTML that will be injected into the Suitelet. Use an HTML minifier tool to achieve this one string output.
- * @returns {string} HTML String
- */
- function getBody(){
- return "<div style='width:100%' class='container-fluid'> " +
- //Replace with your HTML
- "<div id='container' style='min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto'></div>"
- + "</div>";
- }
- var clientCode = 'run(); ' + function run() {
- console.log('Running client code');
- //*********** GLOBAL VARIABLES ***********
- $ = jQuery;
- DEPLOYMENT_URL = '$DEPLOYMENT_URL$';
- THISURL = $(location).attr('href');
- //*********** After DOM loads run this: ***********
- $(function() {
- injectHeaderScripts(); //Loads Libraries that will be placed on header (Optional)
- $(window).bind('load', injectHTML); //Replaces Suitelet's body with custom HTML once the window has fully loaded(Required)
- waitForLibraries(['Highcharts'], runCustomFunctions); //Runs additional logic after required libraries have loaded (Optional)
- });
- return;
- //*********** HELPER FUNCTIONS ***********
- /**
- * Loads Libraries that will be placed on header (Optional)
- */
- function injectHeaderScripts(){
- console.log('loadHeaderLibraries START');
- loadjscssfile( "https://code.jquery.com/jquery-3.1.1.min.js" );
- loadjscssfile( "https://code.highcharts.com/highcharts.js");
- loadjscssfile("https://code.highcharts.com/modules/exporting.js");
- console.log('loadHeaderLibraries END');
- //*********** HELPER FUNCTION ***********
- function loadjscssfile(filename) {
- var filetype = filename.substr(filename.lastIndexOf('.') + 1).toLowerCase();
- if (filetype == "js") { //if filename is a external JavaScript file
- var fileref = document.createElement('script')
- fileref.setAttribute("type", "text/javascript")
- fileref.setAttribute("src", filename)
- } else if (filetype == "css") { //if filename is an external CSS file
- var fileref = document.createElement("link")
- fileref.setAttribute("rel", "stylesheet")
- fileref.setAttribute("type", "text/css")
- fileref.setAttribute("href", filename)
- }
- if (typeof fileref != "undefined"){
- document.getElementsByTagName("head")[0].appendChild(fileref)
- }
- console.log(filename + ' plugin loaded');
- }
- }
- function runCustomFunctions() {
- console.log('clientFunctions START');
- var DEPLOYMENT_URL = '$DEPLOYMENT_URL$';
- buildChart();
- function buildChart(){
- // Build the chart
- Highcharts.chart('container', {
- chart: {
- plotBackgroundColor: null,
- plotBorderWidth: null,
- plotShadow: false,
- type: 'pie'
- },
- title: {
- text: 'Browser market shares January, 2015 to May, 2015'
- },
- tooltip: {
- pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
- },
- plotOptions: {
- pie: {
- allowPointSelect: true,
- cursor: 'pointer',
- dataLabels: {
- enabled: false
- },
- showInLegend: true
- }
- },
- series: [{
- name: 'Brands',
- colorByPoint: true,
- data: [{
- name: 'Microsoft Internet Explorer',
- y: 56.33
- }, {
- name: 'Chrome',
- y: 24.03,
- sliced: true,
- selected: true
- }, {
- name: 'Firefox',
- y: 10.38
- }, {
- name: 'Safari',
- y: 4.77
- }, {
- name: 'Opera',
- y: 0.91
- }, {
- name: 'Proprietary or Undetectable',
- y: 0.2
- }]
- }]
- });
- }
- }
- function waitForLibraries(libraries, functionToRun){
- var loadedLibraries = 0;
- for (var i in libraries) {
- var library = libraries[i];
- if(eval('typeof ' + library)!='undefined'){
- loadedLibraries++;
- }
- }
- window.setTimeout(function(){
- if(loadedLibraries != libraries.length){
- waitForLibraries(libraries, functionToRun);
- } else{
- console.log(library + ' loaded');
- functionToRun();
- }
- },500);
- }
- function injectHTML(){
- var html = ' $PAGEBODY$ '; //This string will be replaced by the Suitelet
- jQuery("#main_form")[0].outerHTML = html;
- }
- }
- /**
- * Gets deployment URL. Useful for sending POST requests to this same suitelet.
- * @returns {string} Deployment URL
- */
- function getDeploymentURL(){
- return URLMODULE.resolveScript({
- scriptId: RUNTIMEMODULE.getCurrentScript().id,
- deploymentId: RUNTIMEMODULE.getCurrentScript().deploymentId,
- returnExternalUrl: false,
- });
- }
- function toBase64(stringInput){
- return ENCODEMODULE.convert({
- string: stringInput,
- inputEncoding: ENCODEMODULE.Encoding.UTF_8,
- outputEncoding: ENCODEMODULE.Encoding.BASE_64
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement