o2system /
gear
| 1 | /* |
||
| 2 | * This file is part of the O2System Framework package. |
||
| 3 | * |
||
| 4 | * For the full copyright and license information, please view the LICENSE |
||
| 5 | * file that was distributed with this source code. |
||
| 6 | * |
||
| 7 | * @author Steeve Andrian Salim |
||
| 8 | * @copyright Copyright (c) Steeve Andrian Salim |
||
| 9 | */ |
||
| 10 | // ------------------------------------------------------------------------ |
||
| 11 | View Code Duplication | (function () { |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
| 12 | var pre = document.getElementsByTagName('pre'), |
||
| 13 | preLen = pre.length; |
||
| 14 | for (var i = 0; i < preLen; i++) { |
||
| 15 | pre[i].innerHTML = '<span class="line-number"></span>' + pre[i].innerHTML + '<span class="clear-both"></span>'; |
||
| 16 | var linesNum = pre[i].innerHTML.split(/\n/).length; |
||
| 17 | for (var number = 0; number < linesNum; number++) { |
||
| 18 | var lineNumber = pre[i].getElementsByTagName('span')[0]; |
||
| 19 | lineNumber.innerHTML += '<span>' + (number + 1) + '</span>'; |
||
| 20 | } |
||
| 21 | } |
||
| 22 | })(); |
||
| 23 | |||
| 24 | function gearGetSelectionText() { |
||
| 25 | var selectedText = "" |
||
| 26 | if (window.getSelection) { // all modern browsers and IE9+ |
||
| 27 | selectedText = window.getSelection().toString() |
||
| 28 | } |
||
| 29 | return selectedText; |
||
| 30 | } |
||
| 31 | |||
| 32 | function gearCopySelectionText() { |
||
| 33 | var isCopied // var to check whether execCommand successfully executed |
||
| 34 | try { |
||
| 35 | isCopied = document.execCommand("copy") // run command to copy selected text to clipboard |
||
| 36 | } catch (e) { |
||
| 37 | isCopied = false |
||
| 38 | } |
||
| 39 | return isCopied; |
||
| 40 | } |
||
| 41 | |||
| 42 | function gearSelectCode() { |
||
| 43 | if (document.selection) { |
||
| 44 | var range = document.body.createTextRange(); |
||
| 45 | range.moveToElementText(document.getElementById('gear-code-container')); |
||
| 46 | range.select(); |
||
| 47 | } else if (window.getSelection) { |
||
| 48 | var range = document.createRange(); |
||
|
0 ignored issues
–
show
Comprehensibility
Naming
Best Practice
introduced
by
The variable
range already seems to be declared on line 44. Consider using another variable name or omitting the var keyword.
This check looks for variables that are declared in multiple lines. There may be several reasons for this. In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs. If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared. Loading history...
|
|||
| 49 | range.selectNode(document.getElementById('gear-code-container')); |
||
| 50 | window.getSelection().addRange(range); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | function gearCopyCode() { |
||
| 55 | var selected = gearGetSelectionText() // call gearGetSelectionText() to see what was selected |
||
| 56 | if (selected.length > 0) { // if selected text length is greater than 0 |
||
| 57 | var isCopied = gearCopySelectionText() // copy user selected text to clipboard |
||
|
0 ignored issues
–
show
|
|||
| 58 | alert('Copied to clipboard'); |
||
|
0 ignored issues
–
show
Debugging Code
Best Practice
introduced
by
|
|||
| 59 | } |
||
| 60 | } |