Javascript
Check out the Styleguide page for everything on the site including grids and responsive tables.
Quick intro about Yo
The newely implemented Javascript uses a library called Yo version 1.0
what it does
Yo allows you to write scripts and pull dependencies from anywhere in the page while making you think in a modular way as you write your scripts
How to write a basic script
The are examples on the github repo but this will soon be changing to version 2. So here are some v1.0 examples:
Let's create a basic script with no dependencies which wraps all of your code in a scope away from the Body of the site, as in not Global.
<script>
});
What does this do?
copy and paste the yo block without the script tags into the console and you'll see Hello World outputted. One thing to pay attention to is the var world which is scoped only to the function it's within. This means the Global scope cannot access it!
But if I want all of this scoped code to be accessible without conflicting with other variables globally by accident, what do I do?
Let's see
<script>
});
});
Note: Hello loads and is dependent on world. World returns a string "Hello World" and when World has finished "Hello" then runs and console logs the result.
Also Note: The argument Cabbages in the hello script I could have named this world, but I wanted you to know you could call it anything you want.
So another example with a little extra returned just to get the full picture.
<script>
});
}
});
}
}
}
});
So whats going on here?
- 3 scripts are loading with their own namespace
- All 3 scripts have a namespace starting with cms, because if the scripts are in the CMS then it's good practice.
- 1 of the scripts has a longer namespace which ends as the same name of hello as the 1st script. With no conflict.
- cms.hello is dependent on the other 2 scripts so it waits before it runs.
- cms.long.namespace.hello returns a function for saying hello
- cms.component.stuff returns functions for:
- -- add - adds a and b
- -- subract - subtracts b from a
- -- addMonkeys - adds monkeys to the end of a string
The final output in the console when run should be:
You will also see some undefined output but there is no need to worry about that.
What scripts are available for you to use
- Functions
- Modal
- Overlay
Functions
Dependency name: "functions.all"
Description: Gives you access to all available functions shared
- debounce
- breakPoints
- isMobile
<script>
Yo.add('cms.scripty', ['functions.all'], function (funcAll) {
// DEBOUNCE
/* The debounce function is provided 2 parameters – a function and a Number. Declared a variable debounceTimer, which as the name suggests, is used to actually call the function, received as a parameter after an interval of 'delay' milliseconds. */
}, 50));
// BREAKPOINTS
console.log(funcAll.breakPoints.breakPoints);
}
}
// ISMOBILE
alert('isMobile: ' + funcAll.isMobile);
});
Modal
Dependency name: "components.modal"
Description: Allows access to the modal component and it's public methods
<script>
});
modal.show parameters
content | Can be a jQuery object or a Dom Element |
extraClass | For overriding the styles of the modal for that particular activation. Try to prefix it with "modal-" so things aren't confusing |
type | 2 options, undefined for centered Modal & 'full' for full screen modal. Type comes into play when you have a consistent modal type. So maybe you find your making a small modal asking a question with 2 buttons. We would then need to add that type to the SASS styling so you only have to reference the class name rather than repeatedly add the style to CMS pages. |
So lets see a working example
Lets create 2 button examples
- Will just copy the purple menu browse nav to a modal
- Will copy the purple menu browse nav to a modal and add a class to override it.
Let us duplicate the left purple menu browse navigation block into a modal and give it a class to override the modal.
<style>
}
</style>
<script>
Yo.add('cms.modalscripty1', ['components.modal'], function (modal) {
});
});
Test 1 simple opens a default modal and sends it an Element which is cloned
Test 2 opens a modal and sends a classname which overrides the styling. by default the width is 80% and height 50%. But if you need to make a small modal with options then construct it in the CMS with an ID and just point to it. All very simple.
Example modal styled with content
</div>
<style>
}
</style>
<script>
Yo.add('cms.modalscripty2', ['components.modal'], function (modal) {
});
});
});
});
Overlay
Dependency name: "components.overlay"
Description: Overlay element with public methods for show and hide
Z Index: 90
Copy and paste the script below and paste it into the console of your Chrome browser.
<script>
overlay.show();
});
Basically you show and hide the Overlay using the 2 functions, the above script mostly creates a fake button to be clicked on to hide the overlay.
Language
Dependency Name: "utils.language"
Description: For adding your own languages into the CMS via the global/languages template. In any new Modal you create in the CMS you may want to show the correct text for the chosen language. (ja, en, zh).
<script>
});
Overriding the language
If you want to show a specific language regardless of what the user has the language on, you can do the following
<script>
});